compare two string variables to determine if it is the same - powershell

I have $loopback_address is 192.168.1.1/24 and $source_address is 192.168.1.1. I wrote a PowerShell script to split the loopback address and compare it to the source address as below:
$Loopback_ip = ($loopback_address -split '/')
# so my $loopback_ip[0] is now equal to 192.168.1.1 which should be the
# same as the source_address.
if ($loopback_ip[0] -eq $source_address) {
Write-Host "same"
} else {
Write-Host "different"
}
The result should be "same" but it is "different".

Related

Validate and compare incoming parameters in powershell script

I'm just learning the basics of powershell and have a task - create pwsh script which accepts 3 incoming parameters (all are mandatory):
first parameter, value address_1, it's IP address with the format x.x.x.x
second parameter, value address_2, it's IP address with the format x.x.x.x
third parameter, value mask, value in the format x.x.x.x or xx (255.0.0.0 or 8)
This script should check address_1 and address_2 belong to the same network or not. Results in output console, yes or no. As I mentioned before incoming parameters not allow to accept not valid arguments, it should show error.
Can someone explain, how I can do that. I will be very grateful for your help.
As per my comment. This stuff already exists for years now, thus no need to try and write this from scratch, unless it's a homework assignment, or you are pushing yourself to learn to do it.
Search is your friend.
'powershell ipv4 address range to cidr'
The first hit in the results...
https://www.kittell.net/code/powershell-ipv4-range
...and the author's examples:
# IPv4 Range
function New-IPRange ($start, $end)
{
# created by Dr. Tobias Weltner, MVP PowerShell
$ip1 = ([System.Net.IPAddress]$start).GetAddressBytes()
[Array]::Reverse($ip1)
$ip1 = ([System.Net.IPAddress]($ip1 -join '.')).Address
$ip2 = ([System.Net.IPAddress]$end).GetAddressBytes()
[Array]::Reverse($ip2)
$ip2 = ([System.Net.IPAddress]($ip2 -join '.')).Address
for ($x=$ip1; $x -le $ip2; $x++)
{
$ip = ([System.Net.IPAddress]$x).GetAddressBytes()
[Array]::Reverse($ip)
$ip -join '.'
}
}
# IPv4 Range - Example
New-IPRange 192.168.10.10 192.168.10.20
# broadcast IPv4 address from a CIDR range
function Get-Broadcast ($addressAndCidr)
{
$addressAndCidr = $addressAndCidr.Split("/")
$addressInBin = (New-IPv4toBin $addressAndCidr[0]).ToCharArray()
for($i=0;$i -lt $addressInBin.length;$i++)
{
if($i -ge $addressAndCidr[1])
{
$addressInBin[$i] = "1"
}
}
[string[]]$addressInInt32 = #()
for ($i = 0;$i -lt $addressInBin.length;$i++)
{
$partAddressInBin += $addressInBin[$i]
if(($i+1)%8 -eq 0)
{
$partAddressInBin = $partAddressInBin -join ""
$addressInInt32 += [Convert]::ToInt32($partAddressInBin -join "",2)
$partAddressInBin = ""
}
}
$addressInInt32 = $addressInInt32 -join "."
return $addressInInt32
}
# IPv4 Broadcast - Example
Get-Broadcast 192.168.10.10/27
# detect if a specified IPv4 address is in the range
function Test-IPinIPRange ($Address,$Lower,$Mask)
{
[Char[]]$a = (New-IPv4toBin $Lower).ToCharArray()
if($mask -like "*.*")
{
[Char[]]$b = (New-IPv4toBin $Mask).ToCharArray()
}
else
{
[Int[]]$array = (1..32)
for($i=0;$i -lt $array.length;$i++)
{
if($array[$i] -gt $mask){$array[$i]="0"}else{$array[$i]="1"}
}
[string]$mask = $array -join ""
[Char[]]$b = $mask.ToCharArray()
}
[Char[]]$c = (New-IPv4toBin $Address).ToCharArray()
$res = $true
for($i=0;$i -le $a.length;$i++)
{
if($a[$i] -ne $c[$i] -and $b[$i] -ne "0")
{
$res = $false
}
}
return $res
}
# IPv4 In Range - Example
Write-Output "`r`nTest If IP In Range - 192.168.23.128/25"
Test-IPinIPRange "192.168.23.200" "192.168.23.12" "255.255.255.128"
Write-Output "`r`nTest If IP In Range - 192.168.23.127/24"
Test-IPinIPRange "192.168.23.127" "192.168.23.12" "24"
# convert an IPv4 address to a Bin
function New-IPv4toBin ($ipv4)
{
$BinNum = $ipv4 -split '\.' | ForEach-Object {[System.Convert]::ToString($_,2).PadLeft(8,'0')}
return $binNum -join ""
}
# IPv4 To Bin - Example
Write-Output "`r`nIP To Bin"
New-IPv4toBin 192.168.10.10
# convert a Bin to an IPv4 address
function New-IPv4fromBin($addressInBin)
{
[string[]]$addressInInt32 = #()
$addressInBin = $addressInBin.ToCharArray()
for ($i = 0;$i -lt $addressInBin.length;$i++)
{
$partAddressInBin += $addressInBin[$i]
if(($i+1)%8 -eq 0)
{
$partAddressInBin = $partAddressInBin -join ""
$addressInInt32 += [Convert]::ToInt32($partAddressInBin -join "",2)
$partAddressInBin = ""
}
}
$addressInInt32 = $addressInInt32 -join "."
return $addressInInt32
}
# IPv4 From Bin - Example
Write-Output "`r`nIP From Bin - 192.168.23.250"
New-IPv4fromBin "11000000101010000001011111111010"
Write-Output "`r`nIP From Bin - 192.168.10.10"
New-IPv4fromBin "11000000101010000000101000001010"
# CIDR To IPv4 Range - Example
Write-Output "`r`nIP CIDR to Range"
New-IPRange "192.168.23.120" (Get-Broadcast "192.168.23.120/25")
You of course can refactor the above with the Validate code already provided to you by the others.
Some of the documentation can seem over-whelming on frist read, so here's a working framework to study and get you started. The [ValidatePattern()] and [ValidateScript()] attributes validate IPv4 address format and valid value range and errors will be thrown if the conditions they specify aren't met.
Perform you domain comparision in the Process block and branch conditionally on the result. I leave that to you.
Function AddressTest
{
Param(
[Parameter(Mandatory,Position = 0)]
[ValidatePattern('^(\d{1,3}\.){3}\d{1,3}$')]
[ValidateScript({[Int[]](($_.Split('.')) -le 255).Count -eq 4})]
[String]$address_1,
[Parameter(Mandatory,Position = 1)]
[ValidatePattern('^(\d{1,3}\.){3}\d{1,3}$')]
[ValidateScript({[Int[]](($_.Split('.')) -le 255).Count -eq 4})]
[String]$address_2,
[Parameter(Mandatory,Position = 2)]
[ValidatePattern('^((\d{1,3}\.){3})?\d{1,3}$')]
[ValidateScript({(($_ -match '^\d+$') -and ([Int]$_ -le 255)) -or (($_.Split('.') -le 255).Count -eq 4)})]
[String]$Mask
)
Process
{
echo $address_1
echo $address_2
echo $mask
}
}
Read the documentation at the links others provided in the commnents while picking apart the code to understand how it works.
This is an example of how to use regex validation patterns to test if the two ip address and netmask are valid.
param (
[Parameter(Mandatory, Position=0)][string] $ip1,
[Parameter(Mandatory, Position=1)] [string] $ip2,
[Parameter(Mandatory, Position=2)] [string] $mask
)
# you can use [Parameter(Mandatory, Position=0)][IPAddress] $ip1 as input instead of string
# ipaddress param can accept partial ip's like 192.168 and will convert it to 192.0.0.168
# string with test would probably be better
function IsValidIPv4 ($ip) {
return ($ip -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]?)$' -and [bool]($ip -as [ipaddress]))
}
# Validate IP's as actual IPv4
if (isValidIPv4 $ip1){
write-host "$($ip1) IS a valid IPv4 Address"
} else {
write-host "$($ip1) is not a valid IPv4 Address" -ForegroundColor Red
}
if (isValidIPv4 $ip2){
write-host "$($ip2) IS a valid IPv4 Address"
} else {
write-host "$($ip2) is not a valid IPv4 Address" -ForegroundColor Red
}
if (isValidIPv4 $mask){
write-host "$($mask) IS a valid IPv4 Address"
} else {
write-host "$($mask) is not a valid netmask" -ForegroundColor Red
}
Then check with the netmask that ip1 and ip2 are in the same network
Note :
As pointed out in my comments above
you can use [Parameter(Mandatory, Position=0)][IPAddress] $ip1 as input instead of string
ipaddress param can accept partial ip's like 192.168 and will convert it to 192.0.0.168 so this will cause incorrect validation - DON'T USE IT
I find a answer, script, with strange validation pattern:
param (
[parameter(Mandatory = $true, Position = 0)]
[Net.IPAddress]
$ip1,
[parameter(Mandatory = $true, Position = 1)]
[Net.IPAddress]
$ip2,
[parameter(Mandatory = $true, Position = 2)]
[alias("SubnetMask")]
[Net.IPAddress]
$mask
)
if (($ip1.address -band $mask.address) -eq ($ip2.address -band $mask.address)) { $true } else { $false }
Working correct:
./script.ps1 -ip1 192.168.228.110 -ip2 192.168.228.190 -mask 255.255.255.128
But when I use network prefix it always give a true.
./script.ps1 -ip1 192.168.228.110 -ip2 192.168.228.190 -mask 30
How I can modify script, to working with prefix?

PowerShell If statement not equating properly

What am I doing wrong here?
Why do the 2 variables not equal each other?
When I run this script
$temp1 = "#{Dhcp=Disabled}"
$temp2 = Get-NetIPInterface My_Ethernet | select Dhcp
write-host ""
write-host "1" $temp1
write-host "2" $temp2
write-host ""
if ($temp2 -eq $temp1){
write-host "IP address is Static "
}
Else {
write-host "IP address is Not Static"
}
I get this result
1 #{Dhcp=Disabled}
2 #{Dhcp=Disabled}
IP address is Not Static
With the helpful suggestion from Mathias this is now working as expected
$temp1 = "Disabled"
$temp2 = Get-NetIPInterface My_Ethernet | select Dhcp
write-host ""
write-host ""
write-host "1" $temp1
write-host "2" $temp2.dhcp
write-host ""
write-host ""
if ($temp2.dhcp -eq $temp1){
write-host "IP address is Static "
}
Else {
write-host "IP address is Not Static"
}
Just to complement your own effective solution:
Since your intent was to compare a property's value to another value, select -ExpandProperty Dhcp would have returned that value directly (see the docs for Select-Object):
if ((Get-NetIPInterface My_Ethernet | select -ExpandProperty Dhcp) -eq $temp1) { # ...
However, it would be much simpler to use direct property access, using ., the member-access operator:
if ((Get-NetIPInterface My_Ethernet).Dhcp -eq $temp1) { # ...
Note that .Dhcp would work even if your Get-NetIPInterface call returned multiple objects, in which case an array of values is returned, courtesy of PowerShell's member-access enumeration feature.[1]
Finally, note that Write-Host is typically the wrong tool to use, unless the intent is to write to the display only, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, or redirect it to a file. To output a value, use it by itself; e.g. $value, instead of Write-Host $value (or use Write-Output $value); see this answer.
To explicitly print only to the display but with rich formatting, use Out-Host, given that the .ToString() stringification that Write-Host uses on its input is often unhelpful - see this post.
[1] Note that PowerShell's comparison operators such as -eq exhibit filtering behavior when their LHS is an array (collection); that is, instead of returning $true or $false, they then return the sub-array of matching elements - see about_Comparison_Operators.

Usage of hashtable in PowerShell to replace objects

Let's say I have the following scenario:
An IP address is linked to a particular string and I want to match an action.
Example:
IP address: 1.1.1.1
String: "Home"
IP address: 5.5.5.5
String: "Work"
if($IP -eq 1.1.1.1)
{
#Do something with "Home"
}
What would I be needing to use to have 'pretty' code instead of multiple if loops ?
The easiest thing to do here is to create a lookup Hashtable where IP's are the keys, and the corresponding strings are the values:
$lookupIP = #{
'1.1.1.1' = 'Home'
'5.5.5.5' = 'Work'
# etcetera
}
Now, if you have an ip in a variable, simply do
$ip = '1.1.1.1'
# Do something with the corresponding string
Write-Host "$ip will do something with $($lookupIP[$ip])"
If you like, you can add a test first to see if the $ip can be found in the lookup table:
if ($lookupIP.ContainsKey($ip)) {
Write-Host "$ip will do something with $($lookupIP[$ip])"
}
else {
Write-Warning "IP '$ip' was not found in the hashtable.."
}

Powershell - only if statement executes despite input

I'm trying to start my Powershell portfolio with and easy script that calculates Network ID or BroadcastID depending on input from the user. That being said, I can only get my if statement to run. I am unsure what I am missing here. I have tried searching this as I know I cannot be the only one to have this issue, but I am unable to find it. Any education on my error would be appreciated as it seems like a basic flaw.
Thanks!
#prompt for IP and subnet mask
$ip = Read-Host -Prompt "Enter your IP address"
$mask = Read-Host - Prompt "Enter your subnet mask"
[String]$UserDecision = Read-Host -Prompt "Enter N if you would like to calculate your Network ID or B if you would like to calculate your Broadcast address."
$splitmask=$mask.split(".")
$wildcard="$(255 - $splitmask[0]).$(255 - $splitmask[1]).$(255 - $splitmask[2]).$(255 - $splitmask[3])"
# ip and mask variable to ip addresses
$ip = [ipaddress] $ip
$mask = [ipaddress] $mask
#determine networkID
function CalculateNetID {
$networkID = [ipaddress] ($ip.Address -band $mask.Address)
#print NetworkID to console
echo "The Network id is $($networkID.IPAddressToString)"
}
function CalculateBroadcastID {
$networkID = [ipaddress] ($ip.Address -band $mask.Address)
#convert wildcard to IP addresses
$wildcard= [ipaddress] $wildcard
$broadcast = [ipaddress] $($wildcard.Address -bor $NetworkID.Address)
#print broadcast to console
echo "The Broadcast id is $broadcast"
}
if ($UserDecision -eq "N" -or "n"){
CalculateNetID
}
elseif($UserDecision -eq "B" -or "b"){
CalculateBroadcastID
}
else{
echo "Please retry and enter the character associated with the ID you would like to calculate"
}
Here is your problem:
$false -or 'n' # => True
As you can see, this is what's happening in your first if statement, the correct syntax for your condition should be:
if($UserDecision -eq "N" -or $UserDecision -eq "n") {
Same thing applies for the elseif.
Note that, PowerShell comparison operators are not case-sensitive by default, so the -or conditions could be just removed:
if($UserDecision -eq "n") {
You might also want to consider using a switch statement instead of chained if, elseif, else:
switch($UserDecision) {
N { CalculateNetID }
B { CalculateBroadcastID }
Default {
"Please retry and enter the character associated with the ID you would like to calculate"
}
}

Nested hashtable export in PowerShell?

I'm currently trying to create a easy to read document containing all devices on are network (3k+). Currently I have all my data within nested hashtables like so:
$devices = #{"hostname" = #{"Mac Address" = #{"IP Address" = "True or False"}}}
It stores the hostname of the device in $devices. Within the $hostname there is a hashtable containing all MAC addresses associated with that hostname. Within the MAC address there is a hashtable containing all IPs associated with that MAC address.
I've already created part of the script that creates the hashtable and stores the data. I have ran into a road block with exporting the data into a CSV that can be read in Excel with the format of.
Hostname, Mac Address, IP Address
server1, MM.MM.MM.SS.SS.SS , 1.1.1.1
1.1.1.2
MM.MM.MN.SS.SS.SA , 1.1.1.3
server2, MM.MM.MB.SS.SS.ST , 1.2.3.1
, 1.5.2.1
and so on.
Edit:
foreach ($hostname in $devices.Keys) {
echo $hostname
foreach ($Macs in $devices.$hostname.Keys) {
echo $Macs
foreach ($IPs in $devices.$hostname.$Macs.Keys) {
echo $IPs
}
}
}
Create custom objects in your innermost loop, collect the output in a variable, then export the data:
$csv = foreach ($hostname in $devices.Keys) {
foreach ($MAC in $devices.$hostname.Keys) {
foreach ($IP in $devices.$hostname.$Macs.Keys) {
[PSCustomObject]#{
'Hostname' = $hostname
'MAC Address' = $MAC
'IP Address' = $IP
}
}
}
}
$csv | Export-Csv 'C:\path\to\output.csv' -NoType
If you want output exactly like your example (which I wouldn't recommend) you need to keep track of the previous $hostname and $MAC and create blank object properties in case those match the respective current value.