Skip to content

Instantly share code, notes, and snippets.

@nobesnickr
Last active February 19, 2021 21:18
Show Gist options
  • Save nobesnickr/e9b1610eec1732d6e486620e8118415e to your computer and use it in GitHub Desktop.
Save nobesnickr/e9b1610eec1732d6e486620e8118415e to your computer and use it in GitHub Desktop.
PowerShell Commands to Reserve IP Address on Windows Network
function Sandbox {
param(
[Parameter(Mandatory)]
[string]$Name
)
$yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', 'Allows setting a delay'
$no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', 'Does not allow setting a delay'
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice('Set Delay?', 'Would you like to set a delay?', $options, 0)
switch ($result)
{
0 {
$DelayinSeconds = Read-Host -Prompt 'How manys seconds to delay until server starts?'
$message = "Server name $Name will be delayed by $DelayInSeconds seconds."
}
1 {
$message = "Server name $Name will not be delayed."
}
}
Write-Output $message
}
function Get-UInt32FromIPAddress
{
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ipaddress]
$IPAddress
)
$bytes = $IPAddress.GetAddressBytes()
if ([BitConverter]::IsLittleEndian)
{
[Array]::Reverse($bytes)
}
return [BitConverter]::ToUInt32($bytes, 0)
}
function Get-IPAddressFromUInt32
{
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[UInt32]
$UInt32
)
$bytes = [BitConverter]::GetBytes($UInt32)
if ([BitConverter]::IsLittleEndian)
{
[Array]::Reverse($bytes)
}
return New-Object ipaddress(,$bytes)
}
function Get-SubnetAddresses
{
# Converts an IPv4 subnet address in CIDR notation (ie, 192.168.0.0/24) into a collection of [ipaddress] objects.
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$Subnet
)
$ipaddress = $null
Write-Verbose "Subnet: $Subnet"
# Validating the string format here instead of in a ValidateScript block allows us to use the
# $ipaddress and $matches variables without having to perform the parsing twice.
if ($Subnet -notmatch '^(?<Address>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/(?<Mask>\d{1,2})$')
{
throw "Subnet address '$Subnet' does not match the expected CIDR format (example: 192.168.0.0/24)"
}
if (-not [ipaddress]::TryParse($matches['Address'], [ref]$ipaddress))
{
throw "Subnet address '$Subnet' contains an invalid IPv4 address."
}
Write-Output "Subnet Matches: $matches"
$ipParts = $matches['Address'].Split(".")
Write-Output "IP Parts: $ipParts"
$maskDecimal = [int]$matches['Mask']
Write-Output "Mask Decimal: $maskDecimal"
if ($maskDecimal -gt 30)
{
throw "Subnet address '$Subnet' contains an invalid subnet mask (must be less than or equal to 30)."
}
$hostBitCount = 32 - $maskDecimal
Write-Output "Host Bit Count: $hostBitCount"
$netMask = [UInt32]0xFFFFFFFFL -shl $hostBitCount
Write-Output "NetMask: $netMask"
$hostMask = -bnot $netMask
Write-Output "HostMask: $hostMask"
$networkAddress = (Get-UInt32FromIPAddress -IPAddress $ipaddress) -band $netMask
Write-Output "Network Address: $networkAddress"
$broadcastAddress = $networkAddress -bor $hostMask
Write-Output "Broadcast Address: $broadcastAddress"
$ips = @();
Write-Output "Network Address: $(Get-IPAddressFromUInt32 -UInt32 $networkAddress)"
Write-Output "Broadcast Address: $(Get-IPAddressFromUInt32 -UInt32 $broadcastAddress)"
$loopAddr = $nulll;
for ($address = $networkAddress + 1; $address -lt $broadcastAddress; $address++)
{
$loopAddr = Get-IPAddressFromUInt32 -UInt32 $address
Write-Output "IP Address From UInt32: $loopAddr"
$ips += $loopAddr
}
$ips | ForEach-Object {
Write-Output "Looped Addr: $_"
}
}
function CreateCredentials
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0, HelpMessage = "The Username for the Users Account to run as")]
[ValidateNotNullOrEmpty()]
[String]
$Username,
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 1, HelpMessage = "The Password for the Users Account")]
[ValidateNotNullOrEmpty()]
[String]
$Password
)
$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
Write-Verbose "Secure Password: $securePassword"
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
Write-Verbose "Creds: $credential"
# Start-Process Notepad.exe -Credential $credential
}
#Get-SubnetAddresses 10.251.206.0/24
CreateCredentials phil temp123 -Verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment