Jump to content

Recommended Posts

Posted

I have created this script but I'm not convinced that this is the shortest way to do the if statement, can anyone suggest how this could be shortened?

 

function getipthirdoctet {
ipconfig | where-object {$_ –match “IPv4 Address”} | foreach-object{$_.Split(“.")[-2]} 
}
$ipaddress = getipthirdoctet

if ($ipaddress -eq 1)
   {
   $ipaddress =01 
   }
Elseif ($ipaddress -eq 2)
   {
   $ipaddress =02 
   }
Elseif ($ipaddress -eq 3)
   {
   $ipaddress =03 
   } 
Elseif ($ipaddress -eq 4)
   {
   $ipaddress =04 
   }
Elseif ($ipaddress -eq 5)
   {
   $ipaddress =05
   }
Elseif ($ipaddress -eq 6)
   {
   $ipaddress =06
   }
Elseif ($ipaddress -eq 7)
   {
   $ipaddress =07
   } 
Elseif ($ipaddress -eq 8)
   {
   $ipaddress =08 
   }
Elseif ($ipaddress -eq 9)
   {
   $ipaddress =09
   }

 

Thanks

Posted

Silly question, and my assumptions may be miles off, but why not just do a count on the length of the return, and if it = 1, add 0 + var etc.

 

aka 10 = length 2, so ignore, 3 = length 1 = 01

 

Steve

  • Thanks 1
Posted

So I have this now thanks...

 

function getipthirdoctet {

ipconfig | where-object {$_ –match “IPv4 Address”} | foreach-object{$_.Split(“.")[-2]}

}

$ipaddress = getipthirdoctet

 

if ($ipaddress.length -eq 1)

{

$ipaddress ='0'+$ipaddress

}

 

 

- - - Updated - - -

 

And now my remote computers will not run unsigned powershell scripts so i may have to do this with vbs instead!

Posted

I haven't really done any powershell but can't you just do this

 

$ipaddress = getipthirdoctet

if ($ipaddress < 10)
$ipaddress = 0 + $ipaddress

 

Since all numbers above 9 don't need the prefix. Is this right?

 

Edit: Whelp there's a couple of ways to do it.

Posted (edited)

Here's my attempt... :)

 

function Get-IPThirdOctet {
   [CmdletBinding()]
   Param (
       [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
       [ValidateNotNullOrEmpty()]
       [string]$ip
   )

   $3rd = ([ipaddress]$ip).GetAddressBytes()[2]
   "{0:D2}" -f [int]$3rd

}

function Get-IP {
   [CmdletBinding()]
   Param (
       [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
       [ValidateNotNullOrEmpty()]
       [string]$ComputerName
   )

   $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $ComputerName | Where { $_.IPEnabled }
   ForEach ($Network in $Networks) {

       $IPAddress = $Network.IpAddress[0]

       $Object = New-Object -Type PSObject
     # $Object | Add-Member -MemberType NoteProperty -Name ComputerName -Value $ComputerName.ToUpper()
       $Object | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
       $Object
   }

}

Get-IP -ComputerName $env:COMPUTERNAME | Select -ExpandProperty IPAddress | Get-IPThirdOctet

 

It will add a leading zero to third octets < 10.

Edited by Arthur
Posted
can anyone suggest how this could be shortened?

If the PCs have Windows 8.x you could use the Get-NetIPAddress cmdlet to simplify the script down to a couple of lines. e.g.

 

$ip = (Get-NetIPAddress -AddressState Preferred | Where PrefixOrigin -eq "DHCP" | Select -First 1).IPv4Address
"{0:D2}" -f ([iPAddress]$ip).GetAddressBytes()[2]

 

http://a.pomf.se/xtoprh.png

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...