Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell script to know which computers have static IP address

Posted on 2015-02-22
12
Medium Priority
?
1,388 Views
Last Modified: 2015-03-25
Trying to compile a powershell script which will list all computers in a domain with there name and IP addresses, which are on static IP address...
thanks.
0
Comment
Question by:Leo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
12 Comments
 
LVL 8

Author Comment

by:Leo
ID: 40625563
after listing addresses for four computers.....it keeps posting errors...

"Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The
argument is null or empty. Provide an argument that is not null or empty, and
then try the command again.
At line:2 char:83
+ ...  -ComputerName $computer.DNSHostName -Query $wmiQuery)
+                    ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindi
   ngValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
   Shell.Commands.GetWmiObjectCommand"
0
 
LVL 53

Assisted Solution

by:Will Szymkowski
Will Szymkowski earned 1000 total points
ID: 40626214
This would be a 2 step process. Use the below command.

First need to get all of the IP address from all machines in Active Directory
This get all computers in AD that has a starting IP scheme of 192.168.x.x
Import-module activedirectory
Get-ADComputer -filter * -properties IPv4Address | ? {$_.IPv4Address -like "192.168.*" } | select name, IP |
Export-Csv "c:\NameAndIP.csv" -nti 

Open in new window


Once you have your List of Computers and IP Addresses you can check the DHCP status using the following script.
Make sure that the column headings for the csv file are like below...
ComputerName   IPAddress
comp1                    192.168.1.2
comp2                    192.168.1.3  
etc
Import-module activedirectory
$Computers = import-csv "NameAndIP.csv"
ForEach ($computer in $Computers) {
$computer.ComputerName
$computer.IPAddress
Get-WMIObject -class Win32_NetworkAdapterConfiguration -ComputerName $computer.ComputerName | ? { $_.DHCPEnabled -eq $false } | select computer.ComputerName, computer.IPAddress, DHCPEnabled |
Export-csv "c:\DHPCDisabled.csv -NoTypeInformation -Append
}

Open in new window


Will.
0
 
LVL 8

Author Comment

by:Leo
ID: 40627106
Hi Will, thanks for that, if i want to add another range of IP class to be included in it....
like 10.0.0.1,192.168.....should i just add it like this: {$_.IPv4Address -like "192.168.*" "10.0.0.1" }
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Author Comment

by:Leo
ID: 40627108
or run it twice with two sets of IP range?
0
 
LVL 53

Expert Comment

by:Will Szymkowski
ID: 40627114
No do it this way instead.

{$_.IPv4Address -like "192.168.*" -and $_.IPv4Address -like "10.0.0.*" }

That should do it.

Will.
0
 
LVL 8

Author Comment

by:Leo
ID: 40627156
Thanks, when i run the second part of script, the Powershell keeps thinking but doesnt export any file....
NameAndIP file which it generates in the first part is empty....0kb

This is what i am running....

Import-module activedirectory
Get-ADComputer -filter * -properties IPv4Address | ? {$_.IPv4Address -like "152.98.*" -and $_.IPv4Address -like "10.10.*" } | select name, IP |
Export-Csv "c:\Test\NameAndIP.csv" -nti



Import-module activedirectory
$Computers = import-csv "c:\Test\NameAndIP.csv"
ForEach ($computer in $Computers) {
$computer.ComputerName
$computer.IPAddress
Get-WMIObject -class Win32_NetworkAdapterConfiguration -ComputerName $computer.ComputerName | ? { $_.DHCPEnabled -eq $false } | select computer.ComputerName, computer.IPAddress, DHCPEnabled |
Export-csv "c:\Test\DHPCDisabled.csv -NoTypeInformation -Append
}
0
 
LVL 53

Expert Comment

by:Will Szymkowski
ID: 40627293
First script should be select Name, IPv4Address (not IP). Once you have that info you need to construct your CSV file for the second script like I have listed above.

The second script uses WMI to connect to each computer to check with the network adapter config setting. This needs to connect to each individual machine in the CSV, which could take awhile.

Other than that it should work without issue. I just tested this in my lab.

Will.
0
 
LVL 8

Author Comment

by:Leo
ID: 40627341
Ok thanks, but when i run the first script, i should see computers Names, and IP addresses in "NameAndIP.csv" file?
for me its blank.
0
 
LVL 8

Author Comment

by:Leo
ID: 40627546
Do i have to define domain name in the first part of script?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 1000 total points
ID: 40639403
Hi, I think the -and should be -or on this section
? {$_.IPv4Address -like "152.98.*" -and $_.IPv4Address -like "10.10.*" } |

So it becomes
? {$_.IPv4Address -like "152.98.*" -or $_.IPv4Address -like "10.10.*" } |

Make that change and see what you get.

Rob.
0

Featured Post

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Make the most of your online learning experience.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question