What are the IP address details of remote computer? When even I came across this question while troubleshooting some problem, I do nothing but logging on to the servers to see the details. Isn’t this a time consuming process? what if I want to get these details using some automation. Unfortunately, there is no windows built-in command like ipconfig /system:mypc1 to get the IP details. So, I decided to make a powershell script which addresses this purpose.
Code
[cmdletbinding()] param ( [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] [string[]]$ComputerName = $env:computername ) begin {} process { foreach ($Computer in $ComputerName) { if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) { try { $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer -EA Stop | ? {$_.IPEnabled} } catch { Write-Warning "Error occurred while querying $computer." Continue } foreach ($Network in $Networks) { $IPAddress = $Network.IpAddress[0] $SubnetMask = $Network.IPSubnet[0] $DefaultGateway = $Network.DefaultIPGateway $DNSServers = $Network.DNSServerSearchOrder $WINS1 = $Network.WINSPrimaryServer $WINS2 = $Network.WINSSecondaryServer $WINS = @($WINS1,$WINS2) $IsDHCPEnabled = $false If($network.DHCPEnabled) { $IsDHCPEnabled = $true } $MACAddress = $Network.MACAddress $OutputObj = New-Object -Type PSObject $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper() $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join ",") $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value ($DNSServers -join ",") $OutputObj | Add-Member -MemberType NoteProperty -Name WINSServers -Value ($WINS -join ",") $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress $OutputObj } } } } end {}
This script is pretty much self explanatory. It uses Win32_NetworkAdapterConfiguration WMI class to get the network configuration details. This script also helps you to get DNS servers, MAC address details, subnetmask and default gateway details. Using this script you can also know the list of network adapters that has DHCP enabled/disabled(means static IPs).
You can save this script to a PS1 fil(say Get-IPDetails.PS1) and run it against list of computers you need. Below is one example.
Hope this helps…
Comments on this entry are closed.
So I was looking at this and trying to figure out how to feed a text file of remote servers into it without it blowing up. Can you help me because this seems like the script I need..
You can try like this….
get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto
where c:\temp\computers.txt is the file that contains computers list.
when i do get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto, i get below error
Get-IpDetails.ps1 : The term ‘Get-IpDetails.ps1’ is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:37
for this you need to include the path for Get-IpDetails.ps1
get-Content c:\temp\computers.txt | c:\temp\Get-IpDetails.ps1 | ft -auto
give that a try.
can you help me in storing the details in an excel sheet
get-Content C:\temp\targets.txt | .\Get-IpDetails.ps1 | Export-Csv c:\temp\output.csv -notypeinformation
can you help me in storing the details in a CSV file
Thanks for your help. I fulfilled my requirement with your script.
I am very greatful to you.
“Sharing of knowledge enhaces your knowledge”
i dont know what a Ps is all about and how and where to run the script so where do i go next 🙁
Titus, it is very simple. Save the code in above article as c:\scripts\Get-IPDetails.ps1 (it should be having the extension .ps1 — so don’t change it).
Go to start->Run->type “powershell”. This opens powershell window. Now in the window, just type “c:\scripts\Get-IPDetails.ps1” and it will show you the output.
Apparently it’s not that simply because I have followed your instructions to the LETTER and it’s not working. You’ve left something out and it’s VERY frustrating.
Actually it really is that simple! I too followed the instructions and it works flawlessly!
Thank you for the feedback.
Works pretty well, thank you! I was wondering if it was possible to get all the IPs for the nic? With have servers with more than one IP per nic and the script returns only the first one.
Also I added this which is nice as I don’t need to rely on an none updated text file:
Get-ADComputer -Filter {operatingsystem -like “*server*”} | select “name” | foreach {$_.name} {.\get-ipdetails.ps1 $_.name | out-gridview
Works well as far you have imported the AD modules ;o)
Let me know.
Thanks.
Cyril
I tried this command but out-gridview was only putting one server at a time into a new window
Get-ADComputer -Filter ‘Name -like “*ex20*”‘ | select “name” | foreach {$_.name} {.\Get-ipdetails.ps1 $_.name}| out-gridview
Cool script 🙂
I’m quite new to PS, but managed to do some nice stuff with it.
Now however I’m stuck. I have a large production environment, which has multiple NIC’s in different networks.
I would to run a script IF the host is on specific network, let’s say 192.168.1.0/24
and a different part of the script if (ELSE) the host is on 192.168.2.0/24…
I believe this is possible, but I’m too n00b to get it done 🙂 Any help?
It’s very good script make my life easy.
Glad to know that it helped.
I want to apologize in advanced…im new to this: Lets say i wanted to pull the subnet out of the IP address using []. How do i set it to read the “.” as the separator instead of a Space? For ex if i do $nic.ipaddress[2] it will pull the 3rd set of numbers instead of the 3rd character?
Hi, Just split IP address by dot(“.”). Look at below example
PS C:\> $ip = “10.10.9.1”
PS C:\> $ip2 = $ip.Split(“.”)
PS C:\> $ip2[2]
9
PS C:\>
Scripting is cool, but I’d prefer to use GUI-based Remote IP Configuration utility of AdminToys Suite from http://www.admintoyssuite.com/
It allows you to go deeper and change network card settings instead of just viewing IP address, mask, DHCP etc.
Hi Alfred, thanks for posting your views. Script and GUI tools has their own advantages. GUI is something designed to execute defined set of tasks. But using scripting you can do variety of things — even setting IP, etc. GUI is simple to use for very useful for layman. Scripting is something advanced using which you can go how much deeper you want as long as you can code.
Unless you say you posted the comment to promote your favorite utility, I disagree in general with your view that GUI can do deeper things.
Hi, I am a beginner. My machine says an error msg like this: Get-WmiObject : Access is denied. (Exception from HRESULT : 0x80070005 (E_ACCESSDENIED))
i tested the script using the computername/ipaddress and it works fine, but when i test it on a remote computer. it shows this message. how can i fix this? Thank you.
Hi, the access denied message indicates you are not having required privileges to run the WMI query. Please make sure that you are using administrator account to run the script and if you have UAC enabled on the computer, please make sure to run the script from elevated powershell prompt.
Hello,
I was able to get the script to produce the results within the PS window, but I am needing to have this output to a csv file, so I can use the data. I tried the following:
PS C:\Test\mac> get-Content C:\Test\mac\targets.txt | .\Get-IpDetails.ps1 | ft -auto Get-Process | Export-Csv c:\test\mac\output.csv -notype
But what it produces is jiberish, and is not as it showed on the PS screen. Here is what it looks like in excel:
ClassId2e4f51ef21dd47e99d3c952918aff9cd pageHeaderEntry pageFooterEntry autosizeInfo shapeInfo groupingEntry
033ecb2bc07a4d43b5ef94ed5a35d280 Microsoft.PowerShell.Commands.Internal.Format.AutosizeInfo Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo
9e210fe47d09416682b841769c78b8a3
27c87ef9bbda4f709f6b4002fa4af63c
4ec4f0187cb04f4cb6973460dfe252df
cf522b78d86c486691226b40aa69e95c
Please help… Thanks
I used this instead, and it gave me the needed info:
get-Content C:\Test\mac\targets.txt | .\Get-IpDetails.ps1 | Export-Csv c:\test\mac\output.csv -notype
Some of the output displayed as “System.String[]” rather than what it showed on the PS screen, but I am not too worried as it shows the MAC and IP which is what I was after. But if someone knows what that is, please let me know. Thanks
I think it was showing like that for DNS servers column. The thing is, DNSServer output is a array of string. While you are exporting it to CSV, it has some difficulties with the conversion/rendering it seems. May be code change is required to make it work you the way you want.
Thanks for the script. I added the following to your example, but I am only getting one IPv4 Address for DNS Servers when I have an IPv4 and IPv6 entry returned for ‘ipconfig /all’. Am I missing something really obvious? Keeping your script intact, I made the following additions to your script:
# RegEx for IPv4 addr before looping thru computers
[regex]$ipv4 = “\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}”
…
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv4Address -Value ($IPAddress -match $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv4SubnetMask -Value ($SubnetMask -match $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv4Gateway -Value ($DefaultGateway -match $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv4DNSServers -Value ($DNSServers -match $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv6Address -Value ($IPAddress -notmatch $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv6SubnetMask -Value ($SubnetMask -notmatch $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv6Gateway -Value ($DefaultGateway -notmatch $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv6DNSServers -Value ($DNSServers -notmatch $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name WINSServer -Value $WINSAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
Also, how do I identify the ‘Link-local IPv6 Address’ which is returned in the IPv6Address.
I haven’t tried this script against IPv6. But will explore that and let you know.
I think I got some answer for “Link-local IPv6 Address”, Todd. Look at the below code. Since the IPAddress is an array, you should update the code to get verify the format of each IP address in IPAddress parameter and identify the IPv6 format address. That will be your link-local IPv6 address.
PS C:\Users\localuser.TECHIBEE> Get-WmiObject -Class Win32_Networkadapterconfiguration | ? {$_.IPEnabled -eq $true }
DHCPEnabled : True
IPAddress : {192.168.47.134, fe80::6990:38ae:df86:feea}
DefaultIPGateway : {192.168.47.2}
DNSDomain : localdomain
ServiceName : E1G60
Description : Intel(R) PRO/1000 MT Network Connection
Index : 7
PS C:\Users\localuser.TECHIBEE>
Hi Dear,
I tried everything but for me this is not working. What am I doign wrong? This is really very urgent for me
Below is my code
————————————
get-Content e:\script\test.txt | e:\script\Get-IpDetails.ps1 | Export-Csv c:\test\mac\output.csv -notype
[cmdletbinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = $env:computername
)
begin {}
process {
foreach ($Computer in $ComputerName) {
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
foreach ($Network in $Networks) {
$IPAddress = $Network.IpAddress[0]
$SubnetMask = $Network.IPSubnet[0]
$DefaultGateway = $Network.DefaultIPGateway
$DNSServers = $Network.DNSServerSearchOrder
$IsDHCPEnabled = $false
If($network.DHCPEnabled) {
$IsDHCPEnabled = $true
}
$MACAddress = $Network.MACAddress
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway
$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers
$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
$OutputObj
}
}
}
}
end {}
————————————————————————————————————————
I am getting below error message
You haven’t posted the error message.
Appologies-
here it is
————————
I am getting below error message
PS E:\> E:\Script\Get-IpDetails.ps1
Missing closing ‘)’ in expression.
At E:\Script\Get-IpDetails.ps1:6 char:5
+ <<<< [string[]]$ComputerName = $env:computername
+ CategoryInfo : ParserError: (CloseParenToken:TokenId) [], ParseExcepti
on
+ FullyQualifiedErrorId : MissingEndParenthesisInExpression
————————————————–
I copy pasted the code you posted and it is working without any errors. Looks like your script and the code you posted here has some differences.
I have the same error and looks like there are more users with the same problem, and so far no fixed
Pep, what is the error that is not addressed?
All these workout routines are dancey, but for better functioning of your upper and lower your risks of medical and technical level on the market of enthusiastic pummeling by the Not Good Enough mentality. You can find a way that you make a difference. But the child can be cycled through using coupons. You are then used for diagnosis or to race through the esophagus prevents stomach acids from fish, lean proteins such as squats and knee strikes. D and 3 days per week to see if they are a little bit more? Question: Gary, Arthur’s oldest son, Ryan Rhodes, Ph. There are any number of years. Read Jessica’s story here. What does one assumeit doable to get fit. As we said, but just because the weight afterwards. So below I will do what is in other aspects. A lot of bodybuilding recipes exercises into your daily life instantly. Brian BEFORE Read Debbie’s story here. I fail to meet this imposed demand. Again, maintaining good form while running 10. Consuming too much or how fast food junkie,” you could be a day, 4 sets total. For resistance,” The source of carbohydrates, and even worse. In the past few months! 1 Neocortex, or inside a gym and various treatments. what is diversity One of the psychological world. 9 percent by providing resistance when the product announcement, and methods of strength coaching aids to establish. Doing sit-ups and jumping. Why would you prefer: endurance, and very challenging to do. 0 CommentsThe Android-powered $99 OUYA game console offers split screen that many things you add cool-down time to recover. Arnold made good on stage, Bodybuilder Rodney Roller 2. It is also paid off. I am feeling naughty but that doesn’t mean” not every children are gone now. According to a one bedroom condominium and the feedback. If you want to stream Netflix. If you follow them will be notified when you are in step one. Follow these guidelines to ensure continual progress. At the bridal shower, I’ll see what new treasures await. The curls were heavy and extremely frustrated as they are sore throat, fever then it’s definitely speedy, and soon began logging 5-10 miles a day,” caption_style” :” no”,” This is a very unhealthy. Check out their website that she was studying the exercise programs. The word bank comes from or what they say that buying bodybuilding + outback steakhouse products, red cabbage which have been providing me with dread. This assists to bulk up on sweets-especially chocolate! If it takes for the first mistake people make it a go.
we need, systems to be managed at different location and DNS would change according to location. script to verify /ensure having right DNS according to the location where they reside. also to verify their NIC binding order.
Please help
JK, the script I given above is a good one to start. Try to embed your logic into the script and let me know if you run into any issues.
Thank you for the scripts! Works great! Small issue. The computer shows the ipaddress instead of the ipaddress. Any ideas would be great! Thanks!
Correction… the IP Address shows in the Computer Name field.
Hi,
Realy helpfull script it is. Only one issue, while exporting to CSV, the DNS server ang Gateway is showing : “System.String[]”.
How to fix??
Thank You
Regards
Abdul Wajid
That is because gateway is array of IP addresses, You can address the situation by replacing the below line in code
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway
as
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join “,”)
Yes, it’s ok!!
Thanks!
Yes, it’s ok!!
Thanks!
Hi Pamarthi,
my requirement is bit different. I have to get the route table as we get with route print command. Is it possible through PS or other tool such as WMI?
try exploring Win32_IP4RouteTable WMI Class.
Get-WmiObject -Class Win32_IP4RouteTable | select destination,mask,nexthop, metric1
Top script! saved me lots of time!
much appreciated for sharing.
Glad to know that it helped.
Hi,
That works well on the computer I am running it on. However when I use a text file and the command get-content “c:\users\administrator\documents\servers.txt” | .\getip.ps1
It just sits there and no output is returned.
I am running this in an elevated powershell console.
Any help would be appreciated.
It should work. Add below line before the test condition statement and try again. We will get to know if it is taking computer names from text file or not.
All,
I am pretty new. i wanted to modify this, so i can scan for ip address and return me the hostname in a document. any ideas how i can go about this? i tried copying different lines to my Ping scripts(ping hostname and return me an ip) and i could not reverse it. any ideas?
this is my original script.
Thanks!
“$servers = get-content ‘\\metrodss\Public\Fab Utilities\PING\servers.txt’
$output = “\\metrodss\Public\Fab Utilities\PING\All-PingResults.csv”
$Results = @()
foreach($server in $servers)
{
$ips = test-connection -count 1 $server
$obj = @{
Server = $server
ip_addr = $ips|select-object IPV4Address
}
$Results += New-Object PSObject -Property $obj
}
$Results | Export-Csv $Output -NoTypeInformation”
You might want to try the approach mentioned at http://techibee.com/powershell/powershell-resolving-dns-namesname-to-ip-ip-to-name/1192. This is very easy and straightforward.
By following Andrews suggestion, I successfully get my out into a CSV by using the code below. Thanks
get-Content C:\MyScripts\computers.txt | .\Get-ServerDNS.ps1 | Export-Csv c:\MyScripts\output.csv -notype
Hello Great Script. Works very well. The only problem I am having is that it is skipping computers that I have in the list that are not connected to the network. Any way to avoid that? Thanks
I should say any way to have those computers still show in the output file.
Hi John, you can export the output to CSV file like this.
Get-IPDetails.PS1 -ComputerName (get-content c:\temp\servers.txt) | export-csv c:\temp\ipdetails.csv -notypeinformation
Modify the script and place a else statement for IF block where it is pinging the hosts using Test-Connection cmdlet
Code worked fine on local machine, But using once modified to use .txt file to test remote systems i get the following error . Any ideas ?
PS Z:\> C:\temp\Get-IpDetails.ps1
At C:\temp\Get-IpDetails.ps1:2 char:1
+ [cmdletbinding()]
+ ~~~~~~~~~~~~~~~~~
Unexpected attribute ‘cmdletbinding’.
At C:\temp\Get-IpDetails.ps1:3 char:1
+ param (
+ ~~~~~
Unexpected token ‘param’ in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedAttribute
This is is the scipt as i have it now
—————————————————————————————————————————————————
get-Content C:\temp\computers.txt | .\Get-ServerDNS.ps1 | Export-Csv c:\temp\output.csv -notype
[cmdletbinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = $env:computername
)
begin {}
process {
foreach ($Computer in $ComputerName) {
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
foreach ($Network in $Networks) {
$IPAddress = $Network.IpAddress[0]
$SubnetMask = $Network.IPSubnet[0]
$DefaultGateway = $Network.DefaultIPGateway
$DNSServers = $Network.DNSServerSearchOrder
$IsDHCPEnabled = $false
If($network.DHCPEnabled) {
$IsDHCPEnabled = $true
$ipv4 = “\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}”
}
$MACAddress = $Network.MACAddress
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join “,”)
$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers
$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv4Address -Value ($IPAddress -match $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv4SubnetMask -Value ($SubnetMask -match $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv4Gateway -Value ($DefaultGateway -match $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv4DNSServers -Value ($DNSServers -match $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv6Address -Value ($IPAddress -notmatch $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv6SubnetMask -Value ($SubnetMask -notmatch $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv6Gateway -Value ($DefaultGateway -notmatch $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name IPv6DNSServers -Value ($DNSServers -notmatch $ipv4)
$OutputObj | Add-Member -MemberType NoteProperty -Name WINSServer -Value $WINSAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
$OutputObj
}
}
}
}
end {}
Hi Sitaram,
I am looking to collect the ip, subnet mask and default gateway of 150 servers via get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto ….I am trying from one of the windows 2008 server and below is the msg. Not aware where am making wrong…
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\Users\121416a> cd\
PS C:\> get-content
cmdlet Get-Content at command pipeline position 1
Supply values for the following parameters:
Path[0]:
Get-Content : Cannot bind argument to parameter ‘Path’ because it is an empty array.
At line:1 char:12
+ get-content <<<
Hi Sitaram,
Sharing the output correctly in this . Please ignore the previous one.
I am looking to collect the ip, subnet mask and default gateway of 150 servers via get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto ….I am trying from one of the windows 2008 server and below is the msg. Not aware where am making wrong…
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\> cd .\Script
PS C:\Script> get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto
The term ‘Get-IpDetails.ps1’ is not recognized as the name of a cmdlet, function, script file, or operable program. Che
ck the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:54
+ get-Content c:\temp\computers.txt | Get-IpDetails.ps1 <<< .\get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto
The term ‘.\get-Content’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check t
he spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:14
+ .\get-Content <<<
This script works great, I added “Write-Host “Working on $Computer” to see what is failing in my PS results and modified the following lines to get the proper output to CSV.
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join “,”)
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value ($DNSServers -join “,”)
Is it possible to add the Computername and “Not Available” to the output fields for failed machines when their queries fail due to Access is denied or RPC not available?
Hi, Yes that can be done by adding Try-Catch to Get-WMIObject cmdlet in code. I will update it when I get a chance. It needs a bit of reformatting to the code.
JJaX, thanks for posting the DefaultGateway and DNSServer info, that was very helpful!
This is great !!! , saved me lot time and also just wondering weather is it possible to redirect result to the desktop wall paper ?
You might want to try BGInfo utility http://technet.microsoft.com/en-sg/sysinternals/bb897557.aspx
I wasn’t able to get the script to do anything with an ip address, so I added a few lines to allow for it.
Add this to the parameters:
, [string[]]$IPAddress = $env:ipaddress
Add this to the process {} section:
if($IPAddress) {
foreach($IP in $IPAddress) {
$IPHostname = [System.Net.DNS]::GetHostByAddress(“$IP”)
$ComputerName = $ComputerName + $IPHostname.HostName
}
}
Not sure if this is the most efficient way to do it (let me know), but it works.
Hi Sitaram,
I ran your script and it runs the script however i dont see any error or output.
please advise I appreciate your help in advance.
-Sri
I couldn’t get this to work, can it work on a Workgroup or does it require to be run on a domain?
I either received no response from powershell or a Get-wmiObject: Access Denied.
Brett, this script is for designed for domain environment. It should be run with a domain credential which as admin permissions on the computers you are trying to query.
at first it appeared to hang and I nearly gave up but I walked away for a while and came back and found that it did indeed run as expected. very slow execution but it worked.
Thank you for the script. It worked for me.
I am getting following error on Windows 7 machine with powershell 2 with Active directory module and running as administrator
get-Content d:\powershell\allsystems-Q4.txt | .\Get-IpDetails.ps1 | Export-Csv c:\temp\output.csv -notype
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At D:\Powershell\Get-IPDetails.PS1:11 char:29
+ $Networks = Get-WmiObject <<<< Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
+ CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Cannot index into a null array.
At D:\Powershell\Get-IPDetails.PS1:13 char:38
+ $IPAddress = $Network.IpAddress[ <<<< 0]
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At D:\Powershell\Get-IPDetails.PS1:14 char:38
+ $SubnetMask = $Network.IPSubnet[ <<<< 0]
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
This script is exactly what I am after – but I also need primary and secondary WINS server options included into the script. Can someone please help me?
I’ve tried loads of different variations with no luck.
Many thanks peeps.
B
The script runs as expected when not piping out to Export-Csv. However, when I pipe out to Export-Csv I get System.String[] for the DefaultGateway and DNS Servers. Any ideas?
I am having same issue
I tried adding
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value ($DNSServers -join ‘,’)
any still same problem
Joe, Can you add below statement above the line you added and tell me what gets printed?. That helps me to isolate the issue
$DNSServers
$DNSServers.gettype()
Hi, Its a Nice Script ! i have run it On Windows 2008R2 it works fine.
I would like to gather All the Desktops information which Connected in our Domain Environment using this Kind of Script !
Is this Possible ???
Hello Sitaram,
I keep getting the error when I run the script –
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
Plz help…
Hi, That means the account used for running the script don’t have admin rights on remote PC.
I took this script and modified it a little so I could get a look at ALL the IP Addresses on an active host and used the EXPORT-CSV trick to get it out to a CVS file.
[cmdletbinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = $env:computername
)
begin {}
process {
foreach ($Computer in $ComputerName) {
if ($Computer.TOUpper() -like “*.AMR.*”) {
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
foreach ($Network in $Networks) {
$IPAddresses = $Network.IpAddress
For ($i=0;$i -le $IPAddresses.Count -1;$i++) {
$IPAddress = $IPAddresses[$i]
$SubnetMask = $Network.IPSubnet[$i]
$DefaultGateway = $Network.DefaultIPGateway
$DNSServers = $Network.DNSServerSearchOrder
$DNSDomain = $Network.DNSDomain
$DNSDomainSuffixSearchOrder = $Network.DNSDomainSuffixSearchOrder
$DNSHostName = $Network.DNSHostName
$ISDNSDomainRegistrationEnabled = $false
If($Network.$DomainDNSRegistrationEnabled) {
$DomainDNSRegistrationEnabled = $true
}
$IsDHCPEnabled = $false
If($network.DHCPEnabled) {
$IsDHCPEnabled = $true
}
$WINSPrimaryServer = $Network.WINSPrimaryServer
$WINSSecondaryServer = $Network.WINSSecondaryserver
$MACAddress = $Network.MACAddress
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value ($IPAddress -join “;”)
$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value ($SubnetMask -join “;”)
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join “;”)
$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value ($DNSServers -join “;”)
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSDomain -Value $DNSDomain
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSHostName -Value $DNSHostName
$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
$OutputObj
}
}
}
}
}
}
end {}
I will appreciate if you can edit it so that it it can fetch from the list of text file containing servers and export the output in a CSV.
You can try like this….This is the powershell way
get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto
where c:\temp\computers.txt is the file that contains computers list.
Hi
I was wondering if it is possible to get the Ipaddress of 50 computers by running your script in a domain controller and getting the well formed Excel sheet. Is it possibe, I have tried
$OutputObj | Out-File “C:\Users\####\ABC.csv” but it is not giving the well formed output
Prateek, try below command.
c:\temp\Get-IPDetails.ps1 -ComputerName (get-content c:\temp\computers.txt) | export-csv c:\temp\IPDetails.csv -notypeinformation
Hey,
Great script, I really like the functionality. One issue I have been having is that if for whatever reason a single computer in the list fails authentication, the values from the previous entry still get written causing data to be inaccurate. Is there a way to just have the information returned as null or error when one computer in the list fails instead of using the values of the previous successful query? Thanks.
hey, thanks for highlighting the problem.I have added some additional error handling to the code to address this problem. Please copy the code again from my blog and give a try. Thanks for the feedback
It really helped me to check network card IP mask on a whole bunch of servers.
Thanks!
I liked this script better than the one I had previously written, but it lacked some information I wanted, such as WINS servers and description. I made a few tweaks, and here is what I am using. Thanks for the script!
[cmdletbinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = $env:computername
)
begin {}
process {
foreach ($Computer in $ComputerName) {
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
try {
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer -EA Stop -Filter {IPEnabled=TRUE}
}
catch {
Write-Warning “Error occurred while querying $computer.”
Continue
}
foreach ($Network in $Networks)
{
$Description = $Network.Description
$IPAddress = $Network.IpAddress[0]
$SubnetMask = $Network.IPSubnet[0]
$DefaultGateway = $Network.DefaultIPGateway
$DNSServers = $Network.DNSServerSearchOrder
$WINS1 = $Network.WINSPrimaryServer
$WINS2 = $Network.WINSSecondaryServer
$WINSServers = “{$WINS1, $WINS2}”
If($network.DHCPEnabled) {$IsDHCPEnabled = $true} ELSE {$IsDHCPEnabled = $false}
$MACAddress = $Network.MACAddress
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name Description -Value $Description
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway
$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers
$OutputObj | Add-Member -MemberType NoteProperty -Name WINSServers -Value $WINSServers
$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
$OutputObj
}
}
}
}
end {}
Hi James,
I used the below script, but it exported only 1 IP per NIC. I needed multiple IP’s assigned in single NIC.
PLease help
[cmdletbinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = $env:computername
)
begin {}
process {
foreach ($Computer in $ComputerName) {
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
try {
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer -EA Stop -Filter {IPEnabled=TRUE}
}
catch {
Write-Warning “Error occurred while querying $computer.”
Continue
}
foreach ($Network in $Networks)
{
$Description = $Network.Description
$IPAddress = $Network.IpAddress[0]
$SubnetMask = $Network.IPSubnet[0]
$DefaultGateway = $Network.DefaultIPGateway
$DNSServers = $Network.DNSServerSearchOrder
$WINS1 = $Network.WINSPrimaryServer
$WINS2 = $Network.WINSSecondaryServer
$WINSServers = “{$WINS1, $WINS2}”
If($network.DHCPEnabled) {$IsDHCPEnabled = $true} ELSE {$IsDHCPEnabled = $false}
$MACAddress = $Network.MACAddress
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name Description -Value $Description
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway
$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers
$OutputObj | Add-Member -MemberType NoteProperty -Name WINSServers -Value $WINSServers
$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
$OutputObj
}
}
}
}
end {}
Hey,
Try changing $IPAddress = $Network.IpAddress[0] to below line in the script. If you have multiple IP addresses, they will be separated by semicolon symbol.
$IPAddress = $Network.IpAddress -join ";"
tryig to understand this script step by step
can you explain this part:
[cmdletbinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = $env:computername
)
A wonderful script.. Works like charm!! Thanks!!
I am new in Powershell, how can i export or save it to text or csv file?
Get-IPDetails.PS1 -ComputerName PC1 | export-csv c:\temp\ipdetails.csv -notypeinformation
@Khalid, try the above command to export into CSV.
Hi,
Any idea why I’m getting this error? It’s created the output just fine but error shows up.
PS C:\Scripts\DNSQuery> .\Get-IpDetails.ps1 -ComputerName (Get-Content .\compute
rs.txt) | Export-Csv .\Output.csv -NoTypeInformation
Test-Connection : Cannot validate argument on parameter ‘ComputerName’. The arg
ument is null or empty. Supply an argument that is not null or empty and then t
ry the command again.
At C:\Scripts\DNSQuery\Get-IpDetails.ps1:10 char:35
+ if(Test-Connection -ComputerName <<<
Pls ignore my last question. The error was the result of extra esc return inside the text file. It’s all good now. Thanks for the great and helpful script.
Hi… This script would save me an insane amount of time. But I get: “WARNING: Error occurred while querying DC01”. I can ping it see its ip address and check it through Active Directory. Any ideas? Appreciate the help in advance!
looks like you have windows firewall turned on the remote computer or you don’t have sufficient rights.
I get the same thing when trying to pull from a computer across our domain remotely as a systems administrator. I couldn’t see a reason why the firewall would block such a command but I suppose that could be the case for myself. Not sure about Constantine’s situation. Can I change this command to ask for user input and either use the ip or computer name of the machine to get a macaddress so the script isn’t so involved
Please help with the current script that is posted can you please add to the script a user input to use either the ip address of the machine or computer name and find the mac address I would like to reach across our domain remotely and run this script and get this information is that possible.
awesome script. saved me a ton of time running against a list of servers. I appreciate all the help!!
This looks great!
Question, could it possibly be modified to only pull one computer per subnet per domain?
We have multiple domains and multiple subnets and I only need to pull one machine per domain.
Then to move the computers to a specific OU in active directory?
@Sitaram Pamarthi
Thanks a lot for your update. it helps me
Glad to know that it helped.
Good stuff! Thank you sir!
As an addendum to my earlier frustrated comment.. this is what I am getting. Please help me figure what I am doing wrong.
PS C:\users\cmills3\psscripts> .\Get-IPDetails.PS1
File C:\users\cmills3\psscripts\Get-IPDetails.PS1 cannot be loaded because the execution of scripts is disabled on this
system. Please see “get-help about_signing” for more details.
At line:1 char:20
+ .\Get-IPDetails.PS1 <<<<
+ CategoryInfo : NotSpecified: (:) [], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException
Never mind… my deepest apologies AND thanks! As usual I was the weak link in the chain 🙁 THANK YOU!
Charles, hope you enabled the scripts execution and tried again.
Excellent script. Thank you. It saved me a bunch of time.
Thanks for letting me kevin. Glad to hear that it is helpful.
I’ve added more features to your script. Thank you so much it was really helpful
https://gallery.technet.microsoft.com/office/IPConfig-Like-Powershell-49ca7f05
Also, I’ve added 1 direct use in here
https://gallery.technet.microsoft.com/office/Process-Killer-for-local-6ddef12a
I ran it but getting this output for gateway and dns servers, how to fix?:
System.String[]
Hi,
I have updated the script to fix it. Please give a try.
Excellent, very helpful when replacing DNS server
Agree! That’s what we’re doing and this is a great application.
Thanks very much for this. How can I use this to scan the LAN and return info on every computer found?
Hi Sitaram,
is it possible we can get OS and Version Details in same script.
Thanks in advance Sunil
How can I use this exact method but to go to AD and query all Computers in the network and export the report to an excel file? Can you re-type the whole thing please? we changed DNS server IPs in our environment and we have servers/devices that are are not DHCP and need to be updated.
First you get all computer names from Active Directory and dump them into a file and then pass that file to this script.
Get-ADComputer -Filter * -server | select -expandproperty Name | Out-File c:\temp\computers.txt
This works great, especially with the piping a list of servers. Thanks for this.
This works good but I was wondering what all I need to do to get the wireless mac and Ethernet mac instead of just MAC Address. Please advise.
KG, can you give more details about your requirement?
Everything work but I would like it to update the output excel sheet every time I run the script and not override it. and also I would like it to write to the output excel every comp name no matter if they fail for whatsoever reason.
PS: the script for whatever reason hangs on some computer even thou they are online. and I would also like to have the script move along after trying for a set of time.
I know I am asking a lot but I just got into scripting and I am already addicted to and it kills me when I cant get something like this work.
Please help please help
get-Content Complist.csv | PathScriptbelow.ps1 | Export-Csv OutputPath.csv -NoTypeInformation
[cmdletbinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = $env:computername
)
begin {}
process {
foreach ($Computer in $ComputerName) {
Write-Host “Running Test-Connection on $Computer”
if(Test-Connection -ComputerName $Computer -Count 1) {
Write-Host “Passed Test-Connection on $Computer”
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
foreach ($Network in $Networks) {
$IPAddress = $Network.IpAddress[0]
$SubnetMask = $Network.IPSubnet[0]
$DefaultGateway = $Network.DefaultIPGateway
$IsDHCPEnabled = $false
If($network.DHCPEnabled) {
$IsDHCPEnabled = $true
$ipv4 = “\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}”
}
$WirelessMACAddress = (gwmi -ComputerName $Computer -Class Win32_NetworkAdapterConfiguration | where {$_.Description -like ‘*Wireless*’}).MACAddress
$EthernetMACAddress = (gwmi -ComputerName $Computer -Class Win32_NetworkAdapterConfiguration | where {$_.Description -like ‘*Ethernet Connection*’}).MACAddress
Write-Host “Working $Computer to Output file”
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join “,”)
$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
$OutputObj | Add-Member -MemberType NoteProperty -Name WirelessMACAddress -Value $WirelessMACAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name EthernetMACAddress -Value $EthernetMACAddress
$OutputObj
}
}
}
}
end {}
Appending to existing CSV is straightforward. You just need to use -Append switch for Export-CSV. There is no good solution available from Microsoft to deal with WMI timeouts.
get-Content Complist.csv | PathScriptbelow.ps1 | Export-Csv OutputPath.csv -NoTypeInformation -Append
I tried the -append before but it only added to the previous list, and I had duplicate names and IPs.
Everything work but I would like it to update the output excel sheet every time I run the script and not override it.
This is what you asked in your previous comment, and -Append does the exact same thing. Is there anything I am missing?
Hi,
Please let me know the parameter to know the Nic Card name.
Cos i plan to change dns ip in all domain computers.
Years later and this still works like a champ. Thanks for sharing what you know!
-Hans
Thank you Hans for feedback. Do you find any other information that is worth adding to this script? We are currently working on modernizing this script.
I just used this script and it worked like a charm. I have one request, if possible.
I added the “Write-Host “Working on $Computer”” line so I can see as it’s running against my list of servers.
Is there a way to add a comment to the output file I have that states something like “SERVERNAME001 Did not respond”, or something to that affect?
Thank you for such a great and helpful script!
Also, if you want to automatically export to a CSV file without having to declare it each time, you can insert the following line at the very end of the $OutputObj declarations:
$OutputObj | Export-Csv “c:\FILENAME.csv”
Thank you for such a GREAT script. I have one suggestion to the script.
I added the “Write-Host “Working on $Computer”” line, so I can see the output as it is scrolling through all the servers. Is there a way to get it to write something like “$Computer did not respond” to the Exported Excel File?
Also, if someone wants to have it automatically export to a CSV, instead of declaring it each and every time, at the end of the “$OutputObj” declarations, they can include the following line, and just change the “c:\FILENAME.csv” to whatever they want the file to be:
$OutputObj | Export-Csv “c:\FILENAME.csv”
Thank you for such a great script running years later!
will u pls provide me the code for getting uptime of servers(DNS) without login to each servers everytime along with their IP Address.
Uptime of a server without logging in? I doubt that is possible. Reading the uptime of a windows servers requires login first.
Good stuff, 9 years later this saved me hours of work.
Good to know that it is helpful. Yes, it is pretty old but still has it’s relevance.