2016-09-20 7 views
0

私はユーザーのSIDを調べる場所に次のコードを書いています。見つからない場合は、SIDに出力値を設定していません。 psobjectを使用して結果全体をcsvファイルに出力します。 $ objPrintdata.User_SID_Status =「無効なSID」、それは同じSIDを処理し続け、$ regPrinterDetailsに行く: キャッチの問題を試す

起こっていただきました

は、私は、無効なSIDを見つけて、代わりに以下を実行した後に、ユーザを処理停止のことです。 GetSubKeyNames()| ForEach-Object行が失敗し、同じSIDを持つ別の行を書き込むが、レジストリの正しいエラーがオープンされ、2003年が見つかりません。私がSIDを見つけられなかったユーザーのためにループから抜け出す方法はありますか?

Get-Content "P:\PowerShell\jv\servers.txt" | ForEach-Object{ 
    #Write-Host $_ 
    $intNumberofServers++ 
    $strServer =$_ 
    #setting the psobject server value 
    #$strserver 
    $objPrintdata.Server = $strServer 

    $blnHasOldQueues = $false 
    #Setting server value 
    $strComputer = $strServer.server 

    $PingResult = Test-Connection -ComputerName $strServer -Count 1 -Quiet 
    If($PingResult){ 
    #Outputting server status 
    $objPrintdata.ServerStatus = "Workstation is UP" 

     try{ 
      If($strHklm = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$strServer)){ 
       #executes when it can open HKLM 
       $objPrintdata.RegistryStatus = "Was able to open the registry" 
       #set the path of the registry 
       $PrinterRegKey = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Providers\\Client Side Rendering Print Provider' 
       $regPrinterRef = $strHklm.OpenSubKey($PrinterRegKey) 
       #debug 
       #$regPrinterRef 

      } 

      #setting this to output to csv 
      # $resultsarray [email protected]() 

      #$strPrinters = $regPrinterRef.GetSubKeyNames() 
      #check if regprinterref is not null 
      if($regPrinterRef){ 
       #executes when there are keys 

       #region Loop thru all child keys. These contain the calculable UNC paths to 2003 
       $regPrinterRef.GetSubKeyNames() | ForEach-Object{ 
       #debug 
       # $_ 
       #concatinating to get to the connections key 
       $strPrinterpath =$PrinterRegKey+"\\"+ $_ + "\\Printers\\Connections" 
       #$strPrinterPath 
       if ($strPrinterpath -notlike "*servers*"){ 

        #this value is the sid 
        # $_ will give us the sids. Here I am storing the SIDs into strUserSID to use later on 
        $strUserSID = $_ 
        #debug 
        $strUserSID  

        # The logic below will convert SID to username 
        #pass the SID to the secrity principal SID being struserSID 
        #$objSID = New-Object System.Security.Principal.SecurityIdentifier("$strUserSID") 


        #using a try catch to filter out deleted SIDs, otherwise powershell will throw an error saying it is null 
        Try{ 

         $objSID=get-aduser -filter {sid -eq $strUserSID} 

          if($objSID-eq $null) 
           { 
            #does a test AD lookup to see if the user still exists baseed on their sid. Returns the user object if it exists & null if not 

            $objPrintdata.User_SID_Status ="Invalid SID" 
            $objPrintdata.User = "Invalid SID" 
            $objPrintdata.Does_it_Have_2003_Queues ="Invalid SID" 
            $objPrintdata.UNC_2003_Queues = "Invalid SID" 
            $objPrintdata | Export-Csv P:\powershell\jv\results.csv -NoTypeInformation -Append 

           } 
           else 
           { 
            #executes when the sid is still valid. Displays a subset of the user object properties 
            $strUser= $objSID.SamAccountName 
            $objPrintdata.User_SID_Status ="Valid SID" 
           }                     

         } 


        Catch{ 
          #$strUserID = $objSID.Value 
          Write-Host "Lost connection to AD" 
          #exit 

         } 
        #$strUser 
        $regPrinterDetails = $Strhklm.OpenSubKey($strPrinterPath) 
        #debug 
        #$strPrinterPath 
        #$regPrinterDetails 
        $regPrinterDetails.GetSubKeyNames() |ForEach-Object{ 
         #looping through each key at the connections level to search for the 2003 print server names 
         if($_ -like "*sabppprt2*"){ 

          $objPrintdata.Does_it_Have_2003_Queues = "Yes" 
          #this value is the printer if it exists 
          # $_ will give us the printers. Here I am storing the printers into strUserPrinters to user later on 
          $strUserPrinters = $_ 
          #$strUserPrinters 
          $blnHasOldQueues = $true 
          #The code below is to build the printer UNC to make it more legible 
          $intPrinterLength= $strUserPrinters.Length 
          $strPrintServer= $strUserPrinters.Substring(2,10) 
          #Doing the -13 because we have to limit the length of the substring statement to the length minus the starting poistion of the substring 
          $strPrinterShareName =$strUserPrinters.Substring(13,$intPrinterLength-13) 
          $strPrintUNC = "\\"+$strPrintServer+"\"+$strPrinterShareName 
          $objPrintdata.UNC_2003_Queues = $strPrintUNC 
          $objPrintdata.User = $strUser 
          # Write-Host $strServer $blnHasOldQueues $strUser $strPrintUNC 
          $objPrintdata | Export-Csv P:\powershell\jv\results.csv -NoTypeInformation -Append 




         } 


         # Write-Host $strServer $blnHasOldQueues $strUserSID $strUserPrinters 

        } 

       } 
          else 
          { 
           #Write-host "No 2003 Queues Detected" 
           #Write-host "no 2003 Queues detected" $strUserSID,$strUser,$strPrintUNC 
           $objPrintdata.User = $strUser 
           $objPrintdata.Does_it_Have_2003_Queues = "No 2003 Queues Detected for this user or vsabppprt* queue" 
           $objPrintdata.UNC_2003_Queues = "No 2003 Queues Detected for this user or vsabppprt* queue" 
           $objPrintdata | Export-Csv P:\powershell\jv\results.csv -NoTypeInformation -Append 

          } 

      } 
      #endregion 
      } 

     } 
     catch{ 
      Write-Host "Can Not access this machines registry: " $strServer 
      $objPrintdata.RegistryStatus = "Can not open the registry" 
      $objPrintdata.User_SID_Status = "Can not open the registry" 
      $objPrintdata.User = "Can not open the registry" 
      $objPrintdata.UNC_2003_Queues = "Could not open the registry" 
      $objPrintdata.Does_it_Have_2003_Queues = "Could not open the registry" 
      $objPrintdata | Export-Csv P:\powershell\jv\results.csv -NoTypeInformation -Append 


     }  
    } #if ends here 

    else{ 
     #executes when the machine is unpingable 
     Write-Host "This machine is currently not on the network: " $strServer 
     $objPrintdata.ServerStatus = "Workstation is Down" 
     $objPrintdata.RegistryStatus = "Workstation is Down" 
     $objPrintdata.User_SID_Status = "Workstation is Down" 
     $objPrintdata.User = "Workstation is Down" 
     $objPrintdata.UNC_2003_Queues = "Workstation is Down" 
     $objPrintdata.Does_it_Have_2003_Queues = "Workstation is Down" 
     $objPrintdata | Export-Csv P:\powershell\jv\results.csv -NoTypeInformation -Append 

    } 
    #Doesn't the Else need to be here 

} 
    $intNumberofServers 

答えて

0

無効なSIDが見つかった場合は、現在の子キーの処理を停止したいと正しく理解しています。その場合は、returnステートメントを次の行の後に追加してください。

$objPrintdata | Export-Csv P:\powershell\jv\results.csv -NoTypeInformation -Append 
+0

それでもreturnステートメントが続きます。 –

+0

同じSubKeyNameまたはリスト内の次のSubKeyNameを使用していますか?私は後者を期待している –

+0

ちょうど起こっていたことを実現しました。私は自分のSIDの名前を変更し、それは無効なSIDとしてそれを選んだが、それでも私自身のSIDへの参照を持っていた。 SIDを削除したときも同じことが起こりました。非常に奇妙な。 –

関連する問題