]> git.ardis.lu - ArdisUtilities/commitdiff
Add Get-DocumentationIP
authorArdis Lu <ardisglu@gmail.com>
Mon, 4 Jul 2022 08:36:26 +0000 (01:36 -0700)
committerArdis Lu <ardisglu@gmail.com>
Mon, 4 Jul 2022 08:36:26 +0000 (01:36 -0700)
ArdisUtilities/ArdisUtilities.psd1
ArdisUtilities/ArdisUtilities.psm1

index 97e6b6fdb5857d63caa16271f7bfb4c659f6aed6..12d74443a55b7918497755aeecd7bdbae44fac50 100644 (file)
@@ -4,7 +4,7 @@
   GUID              = '57a6bea3-0ea5-44b3-95d2-71d122772f1c'
   Author            = 'Ardis Lu'
   Description       = 'My personal utility functions.'
-  FunctionsToExport = @('Invoke-RandomFile', 'ConvertTo-Base64String', 'Get-StringHash', 'Get-SizeDurationRatio', 'Get-ExtendedFileProperty')
+  FunctionsToExport = @('Invoke-RandomFile', 'ConvertTo-Base64String', 'Get-StringHash', 'Get-SizeDurationRatio', 'Get-ExtendedFileProperty', 'Get-DocumentationIP')
   CmdletsToExport   = @()
   VariablesToExport = @()
   AliasesToExport   = @()
index 3e0ec27d6eccc957fb336dca9c0f4e6867e10e5b..9b9959bed8af039391c9d4bfe66a341b4bd15ec4 100644 (file)
@@ -410,3 +410,56 @@ function Get-ExtendedFileProperty {
     }
   }
 }
+
+function Get-DocumentationIP {
+  <#
+  .SYNOPSIS
+    Returns an IP address that is suitable for use in documentation.
+  
+  .DESCRIPTION
+    Returns a random IP address from the reserved address blocks described in RFC 5737 (for IPv4) or RFC 3849 (for IPv6).
+  
+  .PARAMETER IPVersion
+    The IP version(s) allowed for the IP address. Valid values are "4", "6", or "Both". The default is "4".
+  
+  .EXAMPLE
+    PS> Get-DocumentationIP
+    203.0.113.194
+  
+  .EXAMPLE
+    PS> Get-DocumentationIP -IPVersion 6
+    2001:0DB8:164E:C268:CBD9:91FE:A7A3:392A
+
+  .LINK
+    https://datatracker.ietf.org/doc/html/rfc5737
+
+  .LINK
+    https://datatracker.ietf.org/doc/html/rfc3849
+  #>
+
+  [OutputType([String])]
+  [CmdletBinding()]
+  param(
+    [Parameter(ValueFromPipeline)]
+    [ValidateSet('4', '6', 'Both')]
+    [String]$IPVersion = '4'
+  )
+
+  begin {
+    $ipv4Prefixes = @('192.0.2.', '198.51.100.', '203.0.113.') # CIDR ranges: 192.0.2.0/24, 198.51.100.0/24, and 203.0.113.0/24 
+    $ipv6Prefix = '2001:0DB8:' # CIDR range: 2001:DB8::/32
+  }
+
+  process {
+    if ($IPVersion -eq 'Both') {
+      $IPVersion = ('4', '6' | Get-Random)
+    }
+
+    if ($IPVersion -eq '4') {
+      ($ipv4Prefixes | Get-Random) + (Get-Random -Maximum 255)
+    }
+    else {
+      $ipv6Prefix + (Get-Random -Maximum 65535 -Count 6 | ForEach-Object { '{0:X}' -f $_ } | Join-String -Separator ':')
+    }
+  }
+}
This page took 0.048384 seconds and 4 git commands to generate.