DHCP Scope Name

So, I am currently starting my first script.

It is already proving extremely useful and pulling all the information that I require except for 2 things:

  1. I would like to see the AD OU of the computer (as per properties, object)
  2. I would like to see the Superscope name from DHCP (Scope [1.2.3.4] Anywhere Student VLAN
Here is my script so far, which is working, but getting these 2 added would sell Powershell scripting to me!

$name = Read-Host -Prompt “What is the name of the computer?”
Get-WmiObject -Class win32_computersystem -ComputerName $name | select username,name,description,domain,manufacturer,model,PrimaryOwnerName,TotalPhysicalMemory
Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $name | Select IPAddress | Where-Object {$_.IPaddress -like “10.*”}
Get-WmiObject Win32_bios -ComputerName $name | Select serialnumber

There are two different cmdlets you’ll want to read up on:

Help Get-ADcomputer -Full

and

Help Get-DHCPServerv4Scope -Full

Thank you, I have been reading through those and testing different parts. The main issue that I cannot resolve is the following:

Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $name | Select IPAddress | Where-Object {$_.IPaddress -like “10.*”}

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?”

 

 

If you know the Second Octs, you could use a switch to specify the location name, example below.

[pre]
$All = @()
Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $name | Select IPAddress | Where-Object {$.IPaddress -like “10.*”} |
ForEach-Object {
$IP = ($
.IPAddress).Split(‘.’)

switch ($IP[1])
{
49 { $Location = “Canada” }

default { $Location = “Other” }
}

$obj = New-Object –typename PSObject
$obj | Add-Member –membertype NoteProperty –name ‘Computer’ –value (“$name”) –PassThru |
Add-Member –membertype NoteProperty –name ‘IPAddress’ –value (“$($_.IPAddress)”) –PassThru |
Add-Member –membertype NoteProperty –name ‘Location’ –value (“$Location”)
$All += $obj
}

$All

[/pre]

I’d recomend to tread an IP address as IP address … I think it’s more professional, easier to read and to understand and probably more reliable … :wink:

([IPADDRESS]‘192.168.2.1’).getAddressbytes()[1]

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.

[pre]

$All = @()
Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $name | Select IPAddress | Where-Object {$_.IPaddress -like “10.*”} |
ForEach-Object {

$IPLoc = ([IPADDRESS]$_.IPaddress.Trim()).getAddressbytes()[1]

switch ($IPLoc){
49 { $Location = “Canada” }

default { $Location = “Other” }
}

$obj = New-Object –typename PSObject
$obj | Add-Member –membertype NoteProperty –name ‘Computer’ –value (“$name”) –PassThru |
Add-Member –membertype NoteProperty –name ‘IPAddress’ –value (“$($_.IPAddress)”) –PassThru |
Add-Member –membertype NoteProperty –name ‘Location’ –value (“$Location”)
$All += $obj
}

$All

[/pre]

 

This is almost perfect. It is working, except when I run it, it displays all of the script before output?

Like this:

PS H:\> #$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('.')
switch ($IP[1]) { 41 { $Location = "Canada" }
default { $Location = "Other" } }
$obj = New-Object –typename PSObject $obj | Add-Member –membertype NoteProperty –name 'Computer' –value ("$name") –PassThru | Add-Member –membertype NoteProperty –name 'IPAddress' –value ("$($_.IPAddress)") –PassThru | Add-Member –membertype NoteProperty –name 'Location' –value ("$Location") $All += $obj }
$All
Computer IPAddress Location -------- --------- -------- xxxx 10.41.xx.xx fe80::d453:2bd6:8d19:64dc Canada
PS H:\>

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 :slight_smile:

How are you running the code MikeDavidson1975 ?

If you are running the code for example in the PowerShell ISE, run the code it will display like you said all the code and then the result.

If you save the code to a file and then run the code it will only display the result.

You could also add a CLS at the top of the code if you want also.

Thanks Adam, this is working great now!

My bad. Too long sat in front of screen - forgot to save, rookie mistake. Now I just need to input 200 subnets…thanks again

Turn this into an advanced function and you could feed your subnets and other parameters as a file or variable saved from another query.

 

glad you got this working!

So from the following:

Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $name | Select IPAddress | Where-Object {$_.IPaddress -like "10.*"} | ForEach-Object { $IP = ($_.IPAddress).Split('.')
switch ($IP[1]) { 41 { $Location = "Oakpark" }
default { $Location = "Other" } }
$obj = New-Object –typename PSObject $obj | Add-Member –membertype NoteProperty –name 'Computer' –value ("$name") –PassThru | Add-Member –membertype NoteProperty –name 'IPAddress' –value ("$($_.IPAddress)") –PassThru | Add-Member –membertype NoteProperty –name 'Location' –value ("$Location") $All += $obj }
$All
Is it possible to break this into 2nd and 3rd octets, so that I could get results as follows?:
x.62.100.x = Town A Staff Subnet
x.62.200.x = Town A Student Subnet
x.64.100.x = Town B Staff Subnet
etc...

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-Module ActiveDirectory

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

[string]$ADFilter = ‘OperatingSystem -Like “Windows Server” -and enabled -eq $true’

$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 = @()

foreach ($Computer in $Computers)
{
Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | Select IPAddress | Where-Object {$_.IPaddress -like “10.*”} |
ForEach-Object {

$IPLoc = ([IPADDRESS]$_.IPaddress.Trim()).getAddressbytes()[1]
switch ($IPLoc){
62 { $Location = “A” }
64 { $Location = “B” }
default { $Location = “Other” }
}

$IPUser = ([IPADDRESS]$_.IPaddress.Trim()).getAddressbytes()[2]
switch ($IPUser){
100 { $User = “Staff” }
200 { $User = “Student” }
default { $User = “Other” }
}

$obj = New-Object –typename PSObject
$obj | Add-Member –membertype NoteProperty –name ‘Computer’ –value (“$name”) –PassThru |
Add-Member –membertype NoteProperty –name ‘IPAddress’ –value (“$($_.IPAddress)”) –PassThru |
Add-Member –membertype NoteProperty –name ‘Location’ –value (“$Location”) –PassThru |
Add-Member –membertype NoteProperty –name ‘User’ –value (“$User”)
$All += $obj
}
}
return $All
} Get-NetworkLocationUser -Computers $Computers

[/pre]

Is this what you are looking for? Turned the code into a function also.

[pre]

CLS

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

Import-Module ActiveDirectory

[string]$ADFilter = ‘OperatingSystem -Like “Windows Server” -and enabled -eq $true’

$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 = @()

foreach ($Computer in $Computers)
{
Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | Select IPAddress | Where-Object {$_.IPaddress -like “10.*”} |
ForEach-Object {

$IPLoc = ([IPADDRESS]$_.IPaddress.Trim()).getAddressbytes()[1]
switch ($IPLoc){
62 { $Location = “A” }
64 { $Location = “B” }
default { $Location = “Other” }
}

$IPUser = ([IPADDRESS]$_.IPaddress.Trim()).getAddressbytes()[2]
switch ($IPUser){
100 { $User = “Staff” }
200 { $User = “Student” }
default { $User = “Other” }
}

$obj = New-Object –typename PSObject
$obj | Add-Member –membertype NoteProperty –name ‘Computer’ –value (“$name”) –PassThru |
Add-Member –membertype NoteProperty –name ‘IPAddress’ –value (“$($_.IPAddress)”) –PassThru |
Add-Member –membertype NoteProperty –name ‘Location’ –value (“$Location”) –PassThru |
Add-Member –membertype NoteProperty –name ‘User’ –value (“$User”)
$All += $obj
}
}
return $All
} Get-NetworkLocationUser -Computers $Computers

[/pre]

Its getting there…

[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
[pre]
$IPLoc = ([IPADDRESS]$_.IPaddress.Trim()).getAddressbytes()[1] switch ($IPLoc){ 41 { $Location = "A" } 64 { $Location = "B" } default { $Location = "Other" } }
$IPUser = ([IPADDRESS]$_.IPaddress.Trim()).getAddressbytes()[2] switch ($IPUser){ 101 { $User = "Staff" } 200 { $User = "Student" } default { $User = "Other" } }
[/pre]

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

Hi Adam

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('.')
switch ($IP[1]) { 41 { $Location = "xxx" } 101 { $Location = "Staff VLAN" }
default { $Location = "Other" } }
switch ($IP[2]) { 41 { $Location2 = "xxx" } 101 { $Location2 = "Staff VLAN" }
default { $Location2 = "Other" } }
$obj = New-Object –typename PSObject $obj | Add-Member –membertype NoteProperty –name 'Computer' –value ("$name") –PassThru | Add-Member –membertype NoteProperty –name 'IPAddress' –value ("$($_.IPAddress)") –PassThru | Add-Member –membertype NoteProperty –name 'Location' –value ("$Location") Add-Member –membertype NoteProperty –name 'Location2' –value ("$Location2") $All += $obj }
$All

Missing –PassThru |at the end of the line for $Location.