Trapping error and continuing?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 15 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
pc_doctor
Posts: 61
Last visit: Tue Jan 03, 2012 4:22 am

Trapping error and continuing?

Post by pc_doctor »

I want to scan a list of computers and find any directories that DO NOT have local Administrators ACL'ed.

I found the code on technet that describes how to recurse through a drive and get the ACL's and modified it a little to suit my needs. The issue I am having is that I want the recursion to continue after the error is trapped. I do not want to change permissions, just generate a list of problem folders. My script just stops at the first folder it can't access with the error: Attempted to perform an unauthoized operation.

How do I get it to continue?

Code: Select all

function chk-myacls {
	param (
	$mycomputer
	)
	$erroractionpreference = "SilentlyContinue"
	trap {
	Write-Host "An error has occured!"
	Write-Host "Error ID: " $_.ErrorID
	Write-Host "Message: "$_.Exception.Message
	Continue
	}
	$namespace = "rootCIMV2"
	$mydrives = Get-WmiObject win32_logicaldisk -Namespace $namespace -ComputerName $mycomputer -filter drivetype="3"
	foreach ($drv in $mydrives) {
	[STRING]$mydrv = $drv.deviceid
	$mydrv = $mydrv.replace(":","$")
	Write-Host "Checking $mydrv on $mycomputer. Please wait..."
	get-childitem $mycomputer$mydrv
User avatar
pc_doctor
Posts: 61
Last visit: Tue Jan 03, 2012 4:22 am

Trapping error and continuing?

Post by pc_doctor »

I want to scan a list of computers and find any directories that DO NOT have local Administrators ACL'ed.

I found the code on technet that describes how to recurse through a drive and get the ACL's and modified it a little to suit my needs. The issue I am having is that I want the recursion to continue after the error is trapped. I do not want to change permissions, just generate a list of problem folders. My script just stops at the first folder it can't access with the error: Attempted to perform an unauthoized operation.

How do I get it to continue?

Code: Select all

function chk-myacls {
	param (
	$mycomputer
	)
	$erroractionpreference = "SilentlyContinue"
	trap {
	Write-Host "An error has occured!"
	Write-Host "Error ID: " $_.ErrorID
	Write-Host "Message: "$_.Exception.Message
	Continue
	}
	$namespace = "rootCIMV2"
	$mydrives = Get-WmiObject win32_logicaldisk -Namespace $namespace -ComputerName $mycomputer -filter drivetype="3"
	foreach ($drv in $mydrives) {
	[STRING]$mydrv = $drv.deviceid
	$mydrv = $mydrv.replace(":","$")
	Write-Host "Checking $mydrv on $mycomputer. Please wait..."
	get-childitem $mycomputer$mydrv
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Trapping error and continuing?

Post by jvierra »

Try it this way:

Code: Select all

function chk-myacls($mycomputer){
     $mydrives = Get-WmiObject win32_logicaldisk -Namespace "rootCIMV2" -ComputerName omega2 -filter drivetype="3"
     foreach ($drv in $mydrives) {
         [STRING]$mydrv = $drv.deviceid
         $mydrv = $mydrv.replace(":","$")
         Write-Host "Checking $mydrv on $mycomputer. Please wait..."
         get-childitem "$mycomputer$mydrv" -recurse  -Ea Continue|get-acl
    }
}

The trap in a function does not override the pipeline handling. Any error will terminate teh pipeline unless you use "-EA" to override deafult behavior. In that case you cannot run into the custom trap.
This topic is 15 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked