Quantcast
Channel: PowerShell
Viewing all articles
Browse latest Browse all 15370

Try and Catch not working

$
0
0

I am trying to run gpresults remotely. It works but only on one machine instead of the entire list that is in the txt file. 

Here is the code

Text
Function get-test(){

$Computers = get-content C:\Users\AdministratorDocuments\WindowsPowerShell\Scripts\Listlaptops.txt

try{

foreach($CO in $Computers){


get-gpresultantsetofpolicy -Computer $CO -ReportType HTML -path C:\Users\Administrator\Documents\WindowsPowerShell\Scripts\$CO.html
}
}
catch [System.Runtime.InteropServices.COMException]{
Write-Host "The $CO is offline"}
}

As soon as I get rid of "try and catch" it works as expected but you get a ton of RPC errors instead of the message. Do I need to add another foreach cmdlet inside catch? 


Viewing all articles
Browse latest Browse all 15370

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>