PowerShell Script to Find Machine IP Address IPv4

Hi All I have something I am trying to automate using PowerShell. I am pretty new to PowerShell and only know enough to do 1 liners at this point and am hoping to get some help and experience from you expert scripters.

The situation is we buy new machines from a Manufacturer (shipped with a name of GoldSerialNumber (SerialNumber is the actual serial number of the machine). We want a way to automate both renaming the machines with a specific site code as the prefix followed with serial number. The way we want to identify the site code is by using the Subnet each machine is connected to once shipped and plugged into the network. Lastly we want to move the machines from the default ad group they are put in to a specified OU based on the site.

I know this is over my head and am looking for help creating this script or if someone has a script that can do it and don’t mind me using it for my situation I would appreciate that too. At this point I am just trying to get started by identifying the Subnet only of a remote computer using PowerShell.

I think I can get the info using the Get-WmiObject -Class "win32_networkadapterconfiguration commandlet but I have not been able to figure out the rest of the command and syntax to pull just the subnet.

Thanks for any insight, guidance, or scripts that I can use to accomplish this task.

Welcome to the forums. What you want to do in PowerShell should be a pretty doable task even if you are new. I think the best thing about PowerShell is its discoverability. The commands (many times referred to as cmdlets) use a distinct naming convention of verb-noun which make them easily searchable using Get-Command. Combine that with outstanding documentation accessible from the command Get-Help and you will be able to find most if not all of what you want. For example:

Task A: You need to change the computer name.
Step 1: Search for a command

Get-Command -noun computer

CommandType     Name                                               Version    Source                                                         
-----------     ----                                               -------    ------                                                         
Cmdlet          Add-Computer                                 Microsoft.PowerShell.Management                                
Cmdlet          Checkpoint-Computer                          Microsoft.PowerShell.Management                                
Cmdlet          Remove-Computer                              Microsoft.PowerShell.Management                                
Cmdlet          Rename-Computer                              Microsoft.PowerShell.Management                                
Cmdlet          Restart-Computer                             Microsoft.PowerShell.Management                                
Cmdlet          Restore-Computer                             Microsoft.PowerShell.Management                                
Cmdlet          Stop-Computer                                Microsoft.PowerShell.Management      
  1. Based on those results I bet Rename-Computer is what you want so look at the documentation.
Get-Help Rename-Computer -ShowWindow
  1. The documentation will show examples of how to use the command along with all the parameters, inputs and outputs.

Task B: Change the OU of a computer
Step 1: Search for a command.

Get-Command -Verb Move -noun *AD*

CommandType     Name                                               Version    Source                                                         
-----------     ----                                               -------    ------                                                         
Alias           Move-AdlStoreItem                                  1.2.8      Az.DataLakeStore                                               
Cmdlet          Move-ADDirectoryServer                       ActiveDirectory                                                
Cmdlet          Move-ADDirectoryServerOperationMasterRole    ActiveDirectory                                                
Cmdlet          Move-ADObject                                ActiveDirectory  

Step 2: I think Move-ADObject is what you need so look at the documentation which has examples:

Get-Help Move-ADObject -ShowWindow

Hopefully this will get you started. Combine this method with Google, and come back with some code and someone should be able to help you out.


Thank you for the insight it was most helpful with this project and definitely got me on the right track.

Now I have something to work with and hope the experts here can help get me across the finish line.

I think I now have most if not all of the components together and just need to figure out how to put in place condition based actions that work.

Here is the code I have put together so for each piece does what I expect when run individually but I cannot get it to complete the renaming when run together but don’t get an error.

#Synopsis This script is intendted to finish configuring newly imaged machines. 
#The items it needs to do are as follows.
# 1. identify subnet the machine is connected to
# 2. identify the hardware type (i.e. laptop or desktop)
# 3. rename the machine with the proper site prefix, hardware type initial (i.e. L or D) and serial number.
# 4. I will be adding a portion to add the newly configured and rename machine to the domain and proper OU.

function Get-HardwareType {
#Computer Name Prefix based on Site Code and machine type Laptop
$prefixahd = "AHD"
#Determines Machine Hardware Type (i.e. Laptop, Desktop, etc...)
$hardwaretype = Get-WMIObject -Class Win32_ComputerSystem -Property PCSystemType
# Create a Variable for New Machine Name when renaming the computer Laptop (adding L after site code and before serial number)
$NewNameahdLap = "$($prefixahdlap)L$((gwmi win32_bios).serialnumber)"
# Create a Variable for New Machine Name when renaming the computer Desktop (adding D after site code and before serial number)
$NewNameahdDkt = "$($prefixahdlap)D$((gwmi win32_bios).serialnumber)"
# Create a variable that Gathers NIC IP information
$nic = gwmi -computer . -class "win32_networkadapterconfiguration" | Where-Object {$_.defaultIPGateway -ne $null}
# Create a Variable that contains the IV4 IP Address
$IP = $nic.ipaddress | select-object -first 1

# Condition for IP Subnet 10.0.0.x and finds laptop hardwaretype
    If ($IP -like '10.0.0.*' -and $hardwaretype -eq '2')
# Action to take if condition is satisfied rename the machine to name in variable (i.e. AHDLxxxxxx where xxxxx is Serial Number)
        {Rename-Computer -ComputerName LOCALHOST -NewName $NewNameahdLap}

Pleas help me figure out how to make this work for the first phase on the code. Thanks for any and all direction and help in this matter.

Wow, that’s a great start! I have some suggestions and questions. You originally said,

Don’t you know the subnet for your network already? Isn’t a server doing DHCP? Are you assuming /24 CIDR since you example was 10.0.0.*? How will you be running this script, while physically logged into the machine or via remoting?
Regardless of those questions here are a few suggestions.

  1. I’d probably not put this in one function. I’d recommend just a straight script file. You can title it Get-Hardware.ps1. If you leave it as a function, make sure you close out your curly brace at the end and actually call the function.
  1. This will produce an object with a property called PCSystemType. To call it use the property dereference operator like this:
If ($IP -like '10.0.0.*' -and $hardwaretype.PCSystemType -eq '2')
  1. When you get to your 4th requirement

Take a look at all the ActiveDirectory commands available to you.

Get-Command -Module ActiveDirectory

I think New-ADComputer and Add-ADGroupMember will probably be particularly helpful.