Solved

Powershell script to know which computers have static IP address

Posted on 2015-02-22
12
526 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
  • 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 250 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 250 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Don’t let your business fall victim to the coming apocalypse – use our Survival Guide for the Fax Apocalypse to identify the risks and signs of zombie fax activities at your business.
When it comes to security, there are always trade-offs between security and convenience/ease of administration. This article examines some of the main pros and cons of using key authentication vs password authentication for hosting an SFTP server.
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now