0
我有下面的代碼,我擡頭一用戶的SID,如果它沒有發現我設定的輸出值與SID沒有被發現。我使用psobject將整個結果輸出到一個csv文件。嘗試捕捉問題
請告訴我發生的事情是,我發現一個無效的SID和而不是停止執行以下操作後處理用戶: $ objPrintdata.User_SID_Status =「無效的SID」,就繼續處理相同的SID並轉到$ regPrinterDetails。 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
它仍然繼續使用return語句。 –
使用相同的SubKeyName或列表中的下一個?我期望後者 –
剛剛意識到發生了什麼事。我重命名了我的SID,並將其作爲一個無效的SID進行了檢索,並且仍然引用了我自己的SID。同樣的事情發生在我刪除SID時。很奇怪。 –