Dies ist eine alte Version des Dokuments!
- GC-Base.ps1
############################################################################################################### # Language : PowerShell 4.0 # Filename : GC-Base-Module.ps1 # Autor : Maik Grosse # Description : Some helper functions # Repository : https://github.com/Meerkateo/powershell-scripts/blob/main/GC-Base-Module.ps1 # Sources : ############################################################################################################### Function Convert-GcIntToIPv4 { [CmdletBinding()] param ( [Parameter(HelpMessage = 'Integer value: [-Integer 3232235777] (3232235777 = 192.168.1.1)')] [System.UInt64]$Integer = 3232235777 ) Begin { $Octets = @() } Process { $Octets += [System.String]([System.Math]::Truncate($Integer / [System.Math]::Pow(256, 3))) $Octets += [System.String]([System.Math]::Truncate(($Integer % [System.Math]::Pow(256, 3)) / [System.Math]::Pow(256, 2))) $Octets += [System.String]([System.Math]::Truncate(($Integer % [System.Math]::Pow(256, 2)) / 256)) $Octets += [System.String]([System.Math]::Truncate($Integer % 256)) [System.Net.IPAddress]$IPv4Address = $Octets -join "." } End { $IPv4Address } } Function Convert-GcIPv4CIDR { [CmdletBinding()] param ( [Parameter(HelpMessage = 'CIDR like /24 without "/": [-CIDR 24]')] [ValidateRange(0, 32)] [System.UInt16]$CIDR = 24 ) Begin { $BitLine = ('1' * $CIDR).PadRight(32, '0') $BitMask = New-Object -TypeName System.Text.StringBuilder } Process { for($i = 0; $i -lt 32; $i += 8) { $8Bit = $BitLine.Substring($i, 8) $null = $BitMask.Append('{0}.' -f [System.Convert]::ToInt32($8Bit, 2)) } $IPv4Address = [System.Net.IPAddress]$BitMask.ToString().TrimEnd('.') } End { $IPv4Address } } Function Convert-GcIPv4SubnetMask { [CmdletBinding()] param ( [Parameter(HelpMessage = 'IPv4 subnet mask: [-IPv4Address 255.255.255.0]')] [System.Net.IPAddress]$IPv4Address = [System.Net.IPAddress]"255.255.255.0" ) Begin { $Octets = $IPv4Address.ToString().Split(".") | ForEach-Object -Process { [System.Convert]::ToString($_, 2) } } Process { $BitLine = ($Octets -join "").TrimEnd("0") $CIDR = $BitLine.Length } End { $CIDR } } Function Convert-GcIPv4ToInt { [CmdletBinding()] param ( [Parameter(HelpMessage = 'IP address: [-IPv4Address 192.168.1.1]')] [System.Net.IPAddress]$IPv4Address = [System.Net.IPAddress]"192.168.1.1" ) Begin { $Octets = $IPv4Address.ToString() -split "\." | ForEach-Object -Process { [System.Convert]::ToUInt32($_) } } Process { [System.UInt64]$Integer = ( ($Octets[0] * [System.Math]::Pow(256, 3)) + ($Octets[1] * [System.Math]::Pow(256, 2)) + ($Octets[2] * 256) + ($Octets[3]) ) } End { $Integer } } Function Get-GcIPv4 { [CmdletBinding(DefaultParameterSetName = 'IPv4Address')] param ( [ValidatePattern('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:3[0-2]|2[0-9]|1[0-9]|[1-9])$')] [Parameter(HelpMessage = 'IPv4: [-IPv4 "192.168.1.1/24"]', ParameterSetName = 'IPv4')] [String]$IPv4 = "192.168.1.1/24", [Parameter(HelpMessage = 'IP address: [-IPv4Address 192.168.1.1]', ParameterSetName = 'IPv4Address')] [System.Net.IPAddress]$IPv4Address = [System.Net.IPAddress]"192.168.1.1", [Parameter(HelpMessage = 'CIDR like /24 without "/": [-CIDR 24]')] [ValidateRange(0, 32)] [System.Byte]$CIDR = 24 ) Begin { switch ($PSCmdlet.ParameterSetName) { "IPv4" { if ($IPv4 -match "^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\/(3[0-2]|2[0-9]|1[0-9]|[1-9])$") { $CIDR = [System.Byte]$Matches[2] $IPv4Address = [System.Net.IPAddress]$Matches[1] } } } $IPv4SubnetMask = [System.Net.IPAddress](Convert-GcIPv4CIDR -CIDR $CIDR) } Process { $IPv4NetworkAddress = [System.Net.IPAddress]($IPv4Address.Address -band $IPv4SubnetMask.Address) [System.UInt64]$Integer = (Convert-GcIPv4ToInt -IPv4Address $IPv4Address) if ($Integer - 1 -ge 0) { $PreviousIPv4Address = [System.Net.IPAddress](Convert-GcIntToIPv4 -Integer ($Integer - 1)) } if ($Integer + 1 -le [System.Math]::Pow(2, 32)) { $NextIPv4Address = [System.Net.IPAddress](Convert-GcIntToIPv4 -Integer ($Integer + 1)) } $Integer = (Convert-GcIPv4ToInt -IPv4Address $IPv4NetworkAddress) $IPv4FirstHostAddress = [System.Net.IPAddress](Convert-GcIntToIPv4 -Integer ($Integer + 1)) $HostCount = [System.Math]::Pow(2, 32 - $CIDR) $IPv4BroadcastAddress = [System.Net.IPAddress](Convert-GcIntToIPv4 -Integer ($Integer + $HostCount - 1)) $IPv4LastHostAddress = [System.Net.IPAddress](Convert-GcIntToIPv4 -Integer ($Integer + $HostCount - 2)) if ($Integer - $HostCount -ge 0) { [System.UInt64]$PreviousInteger = $Integer - $HostCount $IPv4PreviousAddress = [System.Net.IPAddress](Convert-GcIntToIPv4 -Integer $PreviousInteger) $IPv4PreviousNetworkAddress = [System.Net.IPAddress]($IPv4PreviousAddress.Address -band $IPv4SubnetMask.Address) $IPv4PreviousFirstHostAddress = [System.Net.IPAddress](Convert-GcIntToIPv4 -Integer ($PreviousInteger + 1)) $IPv4PreviousBroadcastAddress = [System.Net.IPAddress](Convert-GcIntToIPv4 -Integer ($PreviousInteger + $HostCount - 1)) $IPv4PreviousLastHostAddress = [System.Net.IPAddress](Convert-GcIntToIPv4 -Integer ($PreviousInteger + $HostCount - 2)) } if ($Integer + $HostCount -le [System.Math]::Pow(2, 32)) { [System.UInt64]$NextInteger = $Integer + $HostCount $IPv4NextAddress = [System.Net.IPAddress](Convert-GcIntToIPv4 -Integer $NextInteger) $IPv4NextNetworkAddress = [System.Net.IPAddress]($IPv4NextAddress.Address -band $IPv4SubnetMask.Address) $IPv4NextFirstHostAddress = [System.Net.IPAddress](Convert-GcIntToIPv4 -Integer ($NextInteger + 1)) $IPv4NextBroadcastAddress = [System.Net.IPAddress](Convert-GcIntToIPv4 -Integer ($NextInteger + $HostCount - 1)) $IPv4NextLastHostAddress = [System.Net.IPAddress](Convert-GcIntToIPv4 -Integer ($NextInteger + $HostCount - 2)) } } End { $Result = @{ Address = $IPv4Address CIDR = $CIDR SubnetMask = $IPv4SubnetMask } if ($CIDR -eq 31) { $Result["FirstHost"] = $IPv4NetworkAddress $Result["LastHost"] = $IPv4BroadcastAddress } elseif ($CIDR -lt 31) { $Result["Broadcast"] = $IPv4BroadcastAddress $Result["FirstHost"] = $IPv4FirstHostAddress $Result["LastHost"] = $IPv4LastHostAddress $Result["Network"] = $IPv4NetworkAddress } if ($Integer - $HostCount -ge 0) { $Result["Previous"] = @{} if ($CIDR -eq 31) { $Result["Previous"]["FirstHost"] = $IPv4PreviousNetworkAddress $Result["Previous"]["LastHost"] = $IPv4PreviousBroadcastAddress } elseif ($CIDR -lt 31) { $Result["Previous"]["Broadcast"] = $IPv4PreviousBroadcastAddress $Result["Previous"]["FirstHost"] = $IPv4PreviousFirstHostAddress $Result["Previous"]["LastHost"] = $IPv4PreviousLastHostAddress $Result["Previous"]["Network"] = $IPv4PreviousNetworkAddress } if ($Integer - 1 -ge 0) { $Result["Previous"]["Address"] = $PreviousIPv4Address } $Result["Previous"] = [System.Collections.SortedList]$Result["Previous"] } if ($Integer + $HostCount -le [System.Math]::Pow(2, 32)) { $Result["Next"] = @{} if ($CIDR -eq 31) { $Result["Next"]["FirstHost"] = $IPv4NextNetworkAddress $Result["Next"]["LastHost"] = $IPv4NextBroadcastAddress } elseif ($CIDR -lt 31) { $Result["Next"]["Broadcast"] = $IPv4NextBroadcastAddress $Result["Next"]["FirstHost"] = $IPv4NextFirstHostAddress $Result["Next"]["LastHost"] = $IPv4NextLastHostAddress $Result["Next"]["Network"] = $IPv4NextNetworkAddress } if ($Integer + 1 -le [System.Math]::Pow(2, 32)) { $Result["Next"]["Address"] = $NextIPv4Address } $Result["Next"] = [System.Collections.SortedList]$Result["Next"] } [System.Collections.SortedList]$Result } } Function New-GcHostname { [CmdletBinding()] param ( [Parameter(HelpMessage = 'Delimiter: [-Delimiter "-"]')] [String]$Delimiter = "-", [Parameter(HelpMessage = 'Host name length: [-Length 11]')] [System.UInt16]$Length = 11, [Parameter(HelpMessage = 'Host role: [-Role "pc: Physical client"]')] [ValidateSet( "ap: Access Point", "as: Application Server", "am: Asset Management", "ca: Certificate Authority", "cd: Content dictionary", "cl: Cluster", "cn: Container Node", "cm: Container Management", "co: Collabora Online", "cs: Customer Server", "db: Database", "dm: Document Management", "dc: Domain Controller", "fs: File server", "fw: Firewall", "gs: Game server", "gw: Gateway", "hv: Hypervisor", "mp: Mail proxy", "mr: Mail relay", "mx: Mail server", "mh: Management host", "mc: Media client", "ms: Media server", "md: Mobile device", "mp: Mobile phone", "nm: Network monitoring", "pm: Password Management", "pc: Physical client", "ps: Physical server", "pr: Printer", "px: Proxy server", "rd: Remote Desktop", "rs: Remote support", "rt: Remote Terminal", "sw: Switch", "vc: Virtual client", "vs: Virtual server", "ws: Web server" )] [String]$Role = "pc: Physical client", [Parameter(HelpMessage = 'Copy value as string to clipboard: [-Clipboard \$true]')] [Boolean]$Clipboard = $true ) Begin { [String]$Abbreviation = "" if ($Role -match "^(.*)\:.*$") { $Abbreviation = $Matches[1] } $AbbreviationLength = $Abbreviation.Length + $Delimiter.Length if ($Length -le $AbbreviationLength) { $Length = 0 } else { $Length -= $AbbreviationLength } } Process { $Id = -join ((48..57) + (97..102) | Get-Random -Count $Length | ForEach-Object {[char]$_}) $Hostname = "$Abbreviation$Delimiter$Id" } End { if ($Clipboard) { Set-Clipboard -Value $Hostname } $Hostname } } Function New-GcNetAdapterName { [CmdletBinding(DefaultParameterSetName = 'IPv4Address')] param ( [ValidatePattern('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:3[0-2]|2[0-9]|1[0-9]|[1-9])$')] [Parameter(HelpMessage = 'IPv4: [-IPv4 "192.168.1.0/24"]', ParameterSetName = 'IPv4')] [String]$IPv4 = "192.168.1.0/24", [Parameter(HelpMessage = 'IPv4-Address: [-IPv4Address 192.168.1.0]', ParameterSetName = 'IPv4Address')] [System.Net.IPAddress]$IPv4Address = [System.Net.IPAddress]"192.168.1.0", [Parameter(HelpMessage = 'CIDR like /24 without "/": [-CIDR 24]')] [ValidateRange(0, 32)] [System.Byte]$CIDR = 24, [Parameter(HelpMessage = 'Interface role: [-Role "L: LAN"]')] [ValidateSet( "D: DMZ", "L: LAN", "N: NAT", "T: TEAM", "W: WAN" )] [String]$Role = "L: LAN", [Parameter(HelpMessage = 'Interface type: [-Type "P: Physical"]')] [ValidateSet( "P: Physical", "V: Virtual" )] [String]$Type = "P: Physical", [Parameter(HelpMessage = 'Delimiter: [-Delimiter "I"]')] [String]$Delimiter = "I", [Parameter(HelpMessage = 'Copy value as string to clipboard: [-Clipboard \$true]')] [Boolean]$Clipboard = $true ) Begin { switch ($PSCmdlet.ParameterSetName) { "IPv4" { if ($IPv4 -match "^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\/(3[0-2]|2[0-9]|1[0-9]|[1-9])$") { $CIDR = [System.Byte]$Matches[2] $IPv4Address = [System.Net.IPAddress]$Matches[1] } } } [String]$Networkname = "" if ($Type -match "^(.*)\:.*$") { $Networkname = $Matches[1] } if ($Role -match "^(.*)\:.*$") { $Networkname = "$Networkname$($Matches[1])" } } Process { $Octets = $IPv4Address.GetAddressBytes() for($i = 0; $i -lt 4; $i++) { $Octet = $Octets[$i].ToString("000") $Networkname = "$Networkname$Octet$Delimiter" } $CIDR = $CIDR.ToString("00") $Networkname = "$Networkname$CIDR" } End { if ($Clipboard) { Set-Clipboard -Value $Networkname } $Networkname } } Function New-GcPassword { # https://www.seobility.net/de/wiki/ASCII-Code [CmdletBinding()] param ( [Parameter(HelpMessage = 'Use capital letters: [-CapitalLetters \$true]')] [Boolean]$CapitalLetters = $true, [Parameter(HelpMessage = 'Use digits: [-Digits \$true]')] [Boolean]$Digits = $true, [Parameter(HelpMessage = 'Use special characters: [-SpecialCharacters \$false]')] [Boolean]$SpecialCharacters, [Parameter(HelpMessage = 'Password length: [-Length 16]')] [System.UInt16]$Length = 16, [Parameter(HelpMessage = 'Copy value as string to clipboard: [-Clipboard \$true]')] [Boolean]$Clipboard = $true ) Begin { $Characters = (97..122) If ($CapitalLetters) { $Characters += (65..90) } If ($Digits) { $Characters += (48..57) } If ($SpecialCharacters) { $Characters += (33..46) } } Process { $Password = -join ($Characters | Get-Random -Count $Length | ForEach-Object -Process { [char]$_ }) } End { if ($Clipboard) { Set-Clipboard -Value $Password } $Password } } Function New-GcUsername { [CmdletBinding()] param ( [Parameter(HelpMessage = 'User name length: [-Length 8]')] [System.UInt16]$Length = 8, [Parameter(Helpmessage = 'User name prefix: [-Prefix ""]')] [String]$Prefix = "", [Parameter(Helpmessage = 'User name suffix: [-Suffix ""]')] [String]$Suffix = "", [Parameter(HelpMessage = 'User role: [-Role "u: User"]')] [ValidateSet( "a: Administrator", "s: Service", "u: User" )] [String]$Role = "u: User", [Parameter(HelpMessage = 'Copy value as string to clipboard: [-Clipboard \$true]')] [Boolean]$Clipboard = $true ) Begin { If ($Prefix.Length -eq 0) { if ($Role -match "^(.*)\:.*$") { $Prefix = $Matches[1] } } [System.UInt16]$PreSufLength = $Prefix.Length + $Suffix.Length if ($Length -le $PreSufLength) { $Length = 0 } else { $Length -= $PreSufLength } } Process { $Username = -join ((48..57) + (97..102) | Get-Random -Count $Length | ForEach-Object {[char]$_}) $Username = "$Prefix$Username$Suffix" } End { if ($Clipboard) { Set-Clipboard -Value $Username } $Username } } Function New-GcVpnName { [CmdletBinding()] param ( [Parameter(HelpMessage = 'Delimiter: [-Delimiter "-"]')] [String]$Delimiter = "-", [Parameter(HelpMessage = 'VPN-Name length: [-Length 12]')] [System.UInt16]$Length = 12, [Parameter(HelpMessage = 'User role: [-Role "c2s: Client-to-Site"]')] [ValidateSet( "c2s: Client-to-Site", "s2s: Site-to-Site" )] [String]$Role = "c2s: Client-to-Site", [Parameter(HelpMessage = 'Copy value as string to clipboard: [-Clipboard \$true]')] [Boolean]$Clipboard = $true ) Begin { [String]$Abbreviation = "" if ($Role -match "^(.*)\:.*$") { $Abbreviation = $Matches[1] } [System.UInt16]$AbbreviationLength = $Abbreviation.Length + $Delimiter.Length if ($Length -le $AbbreviationLength) { $Length = 0 } else { $Length -= $AbbreviationLength } } Process { $Id = -join ((48..57) + (97..102) | Get-Random -Count $Length | ForEach-Object {[char]$_}) $VpnName = "$Abbreviation$Delimiter$Id" } End { if ($Clipboard) { Set-Clipboard -Value $VpnName } $VpnName } } Function New-GcVRF { [CmdletBinding()] param ( [Parameter(HelpMessage = 'Use capital letters: [-CapitalLetters \$false]')] [Boolean]$CapitalLetters = $false, [Parameter(HelpMessage = 'Use digits: [-Digits \$false]')] [Boolean]$Digits = $false, [Parameter(HelpMessage = 'VRF (virtual routing and forwarding) identifier length: [-Length 8]')] [System.Int16]$Length = 8, [Parameter(HelpMessage = 'Copy value as string to clipboard: [-Clipboard \$true]')] [Boolean]$Clipboard = $true ) Begin { $Characters = (97..122) If ($CapitalLetters) { $Characters += (65..90) } If ($Digits) { $Characters += (48..57) } } Process { $VRF = -join ($Characters | Get-Random -Count $Length | ForEach-Object -Process { [char]$_ }) } End { if ($Clipboard) { Set-Clipboard -Value $VRF } $VRF } }