reggiep Posted March 13, 2015 Posted March 13, 2015 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
Steve21 Posted March 13, 2015 Posted March 13, 2015 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 1
reggiep Posted March 13, 2015 Author Posted March 13, 2015 Not silly at all. I really rushed through this to get it onto a system quickly but then now have time to give it a bit more thought!
reggiep Posted March 13, 2015 Author Posted March 13, 2015 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!
JRowley Posted March 13, 2015 Posted March 13, 2015 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.
tommej Posted March 13, 2015 Posted March 13, 2015 So I have this now thanks... - - - Updated - - - And now my remote computers will not run unsigned powershell scripts so i may have to do this with vbs instead! Noooo self sign that bad boy! Signing PowerShell Scripts - Scott Hanselman 1
Arthur Posted March 13, 2015 Posted March 13, 2015 (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 March 13, 2015 by Arthur
Arthur Posted March 14, 2015 Posted March 14, 2015 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now