Reputation: 307
I am trying to query multiple computer for their public IP address. If I use script 1, I am only able to query one computer at a time and save the output to a file. If I use text file with multiple computernames, only 1 or 2 computers get queried. ( I am able to query all computers if I do 1 by 1.)
I then added try,catch in script 2 to catch any non-responding computers but that script outputs nothing. Ideally, I would like to query multiple computers and catch any non responding computers but script 2 wont work.
Script 1
$computers= Get-Content .\hostnames.txt
foreach ($computer in $computers) {
$computerSystem = (get-wmiobject Win32_ComputerSystem -Computer $computer).name
$IP=Invoke-Command -ComputerName $computer -ScriptBlock {
(Invoke-WebRequest -uri "http://smart-ip.net/myip" -UseBasicParsing).content } -ErrorAction SilentlyContinue }
$computerSystem, $ip | out-file .\output2.csv -Append
Script 2
$computernames = Get-Content .\hostnames.txt
$NotRespondingLog = ".\notresponding.log"
$data = ForEach ($Computer in $computernames) {
try{
$computerSystem = (get-wmiobject Win32_ComputerSystem -Computer $computer).name
$IP=Invoke-Command -ComputerName $computer -ScriptBlock {
(Invoke-WebRequest -uri "http://smart-ip.net/myip" -UseBasicParsing ).content } -ErrorAction SilentlyContinue
} catch{
$Computer | Out-File -FilePath $NotRespondingLog -Append -Encoding UTF8
continue } }
$data | out-file ".\output2.csv" -Append
Upvotes: 0
Views: 240
Reputation: 13537
I think your code isn't emitting anything to return to your pipeline or your text file. I'd advise we should re-write this a bit to be easier to understand.
If you wonder why your try/catch
isn't being evaluated, this is because on your Invoke-WebRequest
command you have -ErrorActionPreference
set as SilentlyContinue
. This directly tells PowerShell that we do not want to evaluate a catch block or alert when an error occurs.
Here I've rewritten your code a bit for clarity, and added a step or two to emit the objects back out, that's what the lines with the single $thisPC
commands do. Whether inside of the try block, or the catch block, we simply create a new PowerShell objects, map the properties, then emit it to the console and add it to a track list called $ComputerList
. This is a very common pattern you'll see in enterprise ready scripts..
$computernames = 'SomePC123','SomePC234','OfflinePC'
$ComputerList = New-Object System.Collections.ArrayList
ForEach ($Computer in $computernames) {
try{
$computerName = (get-wmiobject Win32_ComputerSystem -Computer $computer).name
$IP= Invoke-Command -ComputerName $computer -ScriptBlock {
(Invoke-WebRequest -uri "http://smart-ip.net/myip" -UseBasicParsing ).content -ErrorAction Stop
}
$thisPC = [psCustomObject]@{Name=$computerName;IP=$IP}
$thisPC
$ComputerList.Add($thisPC) | Out-Null
}
catch{
$thisPC = [psCustomObject]@{Name=$Computer;IP='Not Responding'}
$thisPC
$ComputerList.Add($thisPC) | Out-Null
}
}
$ComputerList | out-file ".\output2.csv" -Append
Here's the output of it:
Name IP
---- --
SomePC234 149.178.121.237
OfflinePC Not Responding
SomePC123 48.40.234.122
Upvotes: 1