Solved

Powershell script to know which computers have static IP address

Posted on 2015-02-22
12
681 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

821 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