powershell - Catch exception when it doesn't find any member from AD -


i made code check groups , authorities of every member in ad group specific drive. works every drive select, breaks when finds both , ad group , user member.

get-adgroupmember : cannot find object identity: 'gf05856_adm' under: 'dc=gescoeurope,dc=fiatgesco,dc=com'.

it breaks because tries search member inside group, can't find member inside user.

cls $result = [system.windows.messagebox]::show("select path of drive.`n" + "once drive selected, excel file made informations`n" + "of selected harddrive's directory premissions") if ($result = "ok") {   add-type -assemblyname system.windows.forms   $folderbrowser = new-object system.windows.forms.folderbrowserdialog   [void]$folderbrowser.showdialog()   $selection = $folderbrowser.selectedpath } $result2 = [system.windows.messagebox]::show("pleas select save file") if ($result2 = "ok") {   add-type -assemblyname system.windows.forms   $folderbrowser2 = new-object system.windows.forms.folderbrowserdialog   [void]$folderbrowser2.showdialog()   $folderbrowser2.description = "select save file"   $selection2 = $folderbrowser2.selectedpath }  $ss =$selection1 -replace '[\w]', '' $subtitle = "harddrive" + $ss  $outfile = ($selection2 + "\" + $subtitle + $title) write-host = $outfile  if (test-path $outfile -pathtype leaf) {   $result3 = [system.windows.messagebox]::show("the file exists in selected path`n" + "do want delete , proceed ?", 'file exists', 'yesnocancel', 'exclamation')   if ($result3 = "yes") {     remove-item $outfile     $header = "folder path,identityreference,names"      $rootpath = $selection      $folders = dir $rootpath  | {$_.psiscontainer -eq $true} 

here main code:

  try {     foreach ($folder in $folders) {       $acls = get-acl $folder.fullname |               foreach-object { $_.access } |               {$_.identityreference -notmatch $exclude}       foreach ($acl in $acls) {         if ($acl.identityreference -notlike "administrators" -and $acl.identityreference -notlike "creator owner"-and $acl.identityreference -notlike "builtin\administrators" -and $acl.identityreference -notlike "nt authority\system" -and  $acl.identityreference -notlike "system") {           $stracls = $acl.identityreference.tostring()           $strusers = @()           $strnames = $stracls.remove(0, 12)           $user = $(try {get-aduser $dn} catch {$null})            if ($strnames -ne $null) {             $a = get-adgroupmember -identity $strnames -recursive |                  get-aduser -property displayname |                  select name |                  sort-object name           } else {           }            foreach ($env:username in $a) {             $strusers += $env:username           }           $outinfo = $folder.fullname + "," + $acl.identityreference + $strusers         }       }     }   } catch [system.io.ioexception] {   } } 


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -