[英]Try catch problems
I have the following code where I look up a users SID and if its not found I set an output value to the SID not being found. 我有以下代码在其中查找用户SID,如果找不到该代码,则将输出值设置为找不到的SID。 I output the entire results to a csv file using a psobject.
我使用psobject将整个结果输出到一个csv文件中。
Whats happening is that I find an invalid sid and instead of stopping processing the user after doing the following: $objPrintdata.User_SID_Status ="Invalid SID", it continues to process the same sid and goes to the $regPrinterDetails.GetSubKeyNames() |ForEach-Object line where it fails and writes another line with the same SID but with the correct error of registry cant be opened and 2003 cant be found. 发生的是,我找到了一个无效的sid,而不是在执行以下操作后停止处理用户:$ objPrintdata.User_SID_Status =“ Invalid SID”,它继续处理相同的sid并转到$ regPrinterDetails.GetSubKeyNames()| ForEach -对象行失败,并写入另一行具有相同SID但具有正确错误的注册表的行,无法打开,并且找不到2003。 How can I get out of the loop for the user where I haven't found a SID?
如何找到尚未找到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 =@()
#$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
if I understand correctly you want to stop processing the current child key if you have found an invalid SID. 如果我正确理解,如果发现无效的SID,则要停止处理当前的子密钥。 If so just add a
return
statement after the following line: 如果是这样,只需在以下行之后添加
return
语句:
$objPrintdata | Export-Csv P:\powershell\jv\results.csv -NoTypeInformation -Append
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.