Solved

Powershell script to know which computers have static IP address

Posted on 2015-02-22
12
1,025 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 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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