?
Solved

Powershell script to know which computers have static IP address

Posted on 2015-02-22
12
Medium Priority
?
1,178 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
Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

762 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