This will give me back an IP address, but what I am not sure how to do is to take the 2nd octet of the output (such as 49) and have a table that will output this octet as a location name.
Even if it meant that the IP address was displayed and I was then prompted to enter it on a new line such as :
$IPAddress = Read-Host -Prompt “What is the IP Address?”
I agree Olaf, also I had to add the Trim to the line as $_.IPaddress / Convert error (System.string to System.net.Ipaddress). Spaces in the string cause the error.
I agree, but this is more used in regard to asset management so each 2nd octet is a different geographical location, more used to know which office to ring based upon IP address
Turned the code into a function so you can pass in the computer objects, comment out some ways to do that by inputting static objects or reading Active Directory itself. If you are going to use Active Directory module, uncomment out the import and confirm you are able to use the Cmdlet (RSAT installed), Hope this is what you are looking for,
[pre]
CLS
## Import-ModuleActiveDirectory
Input static computer obejcts
$Computers = @(“Computer1”,“Computer2”)
Using Active Directory to pull computer objects, filter can be adjusted along with using the property Name or DNSHostName
$ADResults = (Get-ADComputer -Filter $ADFilter -Properties Name,DNSHostName | Select-Object Name,DNSHostName | Sort-Object Name).Name ## Can switch between Name or DNSHostName
function Get-NetworkLocationUser
{
Param ($Computers)
$All = @()
$ADResults = (Get-ADComputer -Filter $ADFilter -Properties Name,DNSHostName | Select-Object Name,DNSHostName | Sort-Object Name).Name ## Can switch between Name or DNSHostName
function Get-NetworkLocationUser
{
Param ($Computers)
$All = @()
[pre]Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Net.IPAddress".
At \\opvx341\homeshares$\Mike.Davidson\Powershell Scripts\test2.ps1:22 char:1
+ $IPLoc = ([IPADDRESS]$_.IPaddress.Trim()).getAddressbytes()[1]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastExceptionCannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Net.IPAddress".
At \\opvx341\homeshares$\Mike.Davidson\Powershell Scripts\test2.ps1:29 char:1
+ $IPUser = ([IPADDRESS]$_.IPaddress.Trim()).getAddressbytes()[2]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
Computer IPAddress Location User
-------- --------- -------- ----
tgnx2244 10.41.101.81 fe80::d453:2bd6:8d19:64dc Other Other
[/pre]
I have changed the following as well, so Location should = A, User should = Staff
It is nearly working. Changed octet 2 and 3 values to match my IP but still getting issues:
Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Net.IPAddress".
At \\xxx\Powershell Scripts\test2.ps1:22 char:1
+ $IPLoc = ([IPADDRESS]$_.IPaddress.Trim()).getAddressbytes()[1]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Net.IPAddress".
At \\xxx\Powershell Scripts\test2.ps1:29 char:1
+ $IPUser = ([IPADDRESS]$_.IPaddress.Trim()).getAddressbytes()[2]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
Computer IPAddress Location User
tgnx2244 10.41.101.81 fe80::d453:2bd6:8d19:64dc Other Other
Thanks for the help, I am still getting an issue even after changing octets 2 and 3 to match my IP:
Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Net.IPAddress".
At \\xxx\Powershell Scripts\test2.ps1:22 char:1
+ $IPLoc = ([IPADDRESS]$_.IPaddress.Trim()).getAddressbytes()[1]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Net.IPAddress".
At \\xxx\Powershell Scripts\test2.ps1:29 char:1
+ $IPUser = ([IPADDRESS]$_.IPaddress.Trim()).getAddressbytes()[2]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
Computer IPAddress Location User
-------- --------- -------- ----
tgnx2244 10.41.101.81 fe80::d453:2bd6:8d19:64dc Other Other
I’ve been trying your script with some success, but i’m not pulling my hair out and have stripped it back to a very basic script. I have tested all parts individually as working and have included the stripped down script. By the way, the PC I am querying has IP 10.41.101.x
This is the response:
What is the name of the computer?: tgnx2244
cmdlet Add-Member at command pipeline position 1
Supply values for the following parameters:
InputObject:
Here is the script:
cls
$name = Read-Host -Prompt "What is the name of the computer?"
$All = @()
Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $name | Select IPAddress | Where-Object {$_.IPaddress -like "10.*"} |
ForEach-Object {
$IP = ($_.IPAddress).Split('.')