I think I’m misunderstanding the try catch…
Below is my code, I’m trying to see if the actual auditpolicy’s match the required audipolicy settings. If not it attempts to correct them.
$dvhash = @{
"Filtering Platform Connection" = "Failure";
"Other Object Access Events" = "Failure";
"Authorization Policy Change"="Success and failure"
"test"="success"
}
#delcaring actual auditpol hash
$audithash = @{}
foreach( $string in ((auditpol /get /category:*) -match '\s\s+' -NotMatch 'Setting'-replace '^\s+([a-zA-Z0-9\s-\\\/(\)?]+\b)\s\s+([a-zA-Z0-9\s]+)', '$1 = $2'))
{
$audithash += ConvertFrom-StringData -StringData $string
}
foreach($dvh in $dvhash.keys)
{
if($audithash[$dvh] -like "*" + $dvhash[$dvh] + "*")
{
write-host "all good! - $dvh"
}
else
{
try
{
write-host "entering try - $dvh"
if($dvhash[$dvh] -match "Success" -and $dvhash[$dvh] -match "Failure")
{
#success and failure
(auditpol /set /subcategory:"$dvh" /success:enable /failure:enable) | out-null
}
elseif($dvhash[$dvh] -match "Success" -and $dvhash[$dvh] -notmatch "Failure")
{
#success
(auditpol /set /subcategory:"$dvh" /success:enable) | out-null
}
else
{
#failure
(auditpol /set /subcategory:"$dvh" /failure:enable) | out-null
}
"Pass!"
}
catch
{
write-host "Entring catch $dvh"
$error[0]
}
}
}
Sample output looks like…
all good! - Authorization Policy Change all good! - Other Object Access Events entering try - test Error 0x00000057 occurred: The parameter is incorrect. Pass! all good! - Filtering Platform Connection
I thought on an error it would immediately go into the catch, but it appears I’m incorrect on thinking this. Can anyone help me out, or show me a good article that talks about try/catches and how they’re actually used.