Powershell version of On Error Resume next.....Maybe

Tags: powershell

I'm sure powershell people will correct me, but coming from a VBScript background, ON ERROR RESUME NEXT had it's uses. I worked on a script and needed to execute a command even though an error might happen.  I needed a quick a dirty execution statement, I found trap [Exception] {continue} did exactly what I needed.   I thought I would pass this along for VBScripters who have converted to Powershell

'Read in a text file which contains a list of computers
$ComputerList = Get-Content("c:\computerlist.txt")

'Loop through the computer list executing the command
foreach($item in $ComputerList)

{

//Write out the host to keep track of the status
 Write-Host $item

//Continue the script even if an error happens
 trap [Exception] {continue}

//Add a domain group to the local Administrators group
 ([ADSI]"WinNT://$item/Administrators,group").add("WinNT://ExampleCom/GroupName_$item,group")
}
 

PS:MoW, if you are reading this, feel free to correct me. :)  

PSS:This is for Powershell 1.0, I've not tested error handling in Powershell 2.0

Cheers,

Steve Schofield

Add a Comment