2017-04-26 109 views
0

我正在尝试编写一个脚本,它将从用户HKLM中删除旧队列(最终将通过挂载ntuser.dat从HKCU删除,但我还没有)。与else语句Powershell错误

我遇到的问题是,我只能通过一个SID下SOFTWARE \微软\的Windows NT \ CURRENTVERSION \打印\提供商迭代\客户端绘制打印提供商\,我得到了以下错误消息:

术语'else'不被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否为更正路径,然后重试。

有没有人遇到过这个问题?

#defining my object that will be used throughout the script. Will be used to log everything 
$objQueueData=[pscustomobject]@{ 
computername="" 
computerstatus="" 
Registrystatus="" 
SID="" 
Does_It_Have_2003_Queues="" 
User_SID_Status="" 
user="" 
UNC_2003_Queues="" 

} 

#$QueueDataCollection=[pscustomobject]@{ 
#queuecollection=$QueueData 
#} 

#reading the list of workstations 
Get-Content "P:\PowerShell\jm\DeletePrintQueues\Workstations.txt" | ForEach-Object{ 
    $strComputerName = $_ 
    #check if the workstation is up 
    IF (Test-Connection -ComputerName $strComputerName -count 2 -quiet) 
    { 
      #$objUser= Get-ChildItem c:\users 
      #$strUserName=$objUser.Name 
      $objQueueData.computername=$strComputerName 
      $objQueueData.computerstatus="Machine is up" 
      DeleteHklm $strComputerName 
    } 
    else 
    { 
    #We are here because the computer could not be reached 
    Write-Host "Machine down" $strComputerName 
    $objQueueData.computername =$strComputerName 
    $objQueueData.computerstatus = "Machine Down" 
    $objQueueData.Registrystatus ="Machine Down" 
    $objQueueData.SID = "Machine Down" 
    $objQueueData.Does_It_Have_2003_Queues="Machine Down" 
    $objQueueData.User_SID_Status="Machine Down" 
    $objQueueData.user="Machine Down" 
    $objQueueData.UNC_2003_Queues="Machine Down" 
    $objQueueData | Export-Csv P:\powershell\jm\results2.csv -NoTypeInformation -Append 
    }  

} 


function DeleteHKLM { 
    param ([string]$computername) 


    try{ 
      If($strHklm = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$strcomputername)) 
      { 
       #executes when it can open HKLM 
       $objqueuedata.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' 
       #$PrinterRegKey 
       $regPrinterRef = $strHklm.OpenSubKey($PrinterRegKey) 
       #debug 
       Write-Host "regprinterref is: "$regPrinterRef       
      } 

       If($regPrinterRef) 
       { 
       #This executes if there are Printers present in the registry 
       #region Loop thru all child keys. These contain the calculable UNC paths to 2003 
       $regPrinterRef.GetSubKeyNames() | ForEach-Object{ 
       #debug 
       Write-Host "The sid is: " $_ 
       #concatinating to get to the connections key 
       #$PrinterRegKey 
       $strPrinterpath =$PrinterRegKey+"\\"+ $_ + "\\Printers\\Connections" 
       #debug 
       Write-Host "The printer keys for SID are located in: " 
       $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{ 

          $strUser = $objSID.Translate([System.Security.Principal.NTAccount]).Value 
          $objQueueData.User_SID_Status ="Valid SID" 
          $strUser          

          } 
         Catch{ 
           #$strUserID = $objSID.Value 
           $objQueueData.User_SID_Status ="Invalid SID" 
           $objQueueData.User = "Invalid SID" 
           $objQueueData.Does_it_Have_2003_Queues ="Invalid SID" 
           $objQueueData.UNC_2003_Queues = "Invalid SID" 
           $objQueueData | Export-Csv P:\powershell\jm\results1.csv -NoTypeInformation -Append 
           #exit 

          } 

         $regPrinterDetails = $Strhklm.OpenSubKey($strPrinterPath) 
         $regPrinterDetails.GetSubKeyNames() |ForEach-Object{ 
         #looping through each key at the connections level to search for the 2003 print server names 
          if($_ -like "*sarspprt2*") 
           {        
            $objQueueData.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 = $_ 
            Write-Host "struserprinters value is " $_ 
            #$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 
            $objQueueData.UNC_2003_Queues = $strPrintUNC 
            $objQueueData.User = $strUser 
            $objQueueData | Export-Csv P:\powershell\jm\results.csv -NoTypeInformation -Append 
            $strkeytodelete=$strPrinterPath+"\\"+$_ 
            $strkeytodelete 
            #delete 2003 Key 

            Remove-Item -Path '$strkeytodelete' -Recurse              
           } 
           elseif($_ -notlike "*sarspprt2*") 
            { 
             #Write-host "No 2003 Queues Detected" 
             #Write-host "no 2003 Queues detected" $strUserSID,$strUser,$strPrintUNC 
             $objQueueData.User = $strUser 
             $objQueueData.Does_it_Have_2003_Queues = "No 2003 Queues Detected for this user or vsarspprt* queue" 
             $objQueueData.UNC_2003_Queues = "No 2003 Queues Detected for this user or vsarspprt* queue" 
             $objQueueData | Export-Csv P:\powershell\jm\results.csv -NoTypeInformation -Append 

            } 

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

        } 

        } 

       } 
         else 
          { 
           #Write-Host "No Printers in the Registry" 
           $objQueueData.computername="" 
           $objQueueData.computerstatus="" 
           $objQueueData.Registrystatus="" 
           $objQueueData.SID="" 
           $objQueueData.Does_It_Have_2003_Queues="" 
           $objQueueData.User_SID_Status="" 
           $objQueueData.user="" 
           $objQueueData.UNC_2003_Queues="" 
          } 
       } 

     } 
    catch{ 
      # Write-Host "cant read registry" 
      $_.Exception.Message 

     } 


} 
+1

你已经得到了正确数量的花括号,但有一个错误的地方。很难从快速回顾中分辨出来,我会稍微整理一下你的缩进,并确保它们都符合你的期望。一个线索,这是问题是,ISE突出显示不同颜色的“其他”,错误基本上是说“还有一个我没有期待的声明”。 –

回答

1

你对线153额外的大括号如果移动到线165后,它应该工作,虽然我现在不能测试它。我习惯于系统性地折叠我的if-else语句,以确保它们都与彼此匹配。

+0

谢谢!那件作品正在运作。我现在试图删除找到的2003键,但以下操作不起作用: $ strkeytodelete ='“hklm:\”+ $ strPrinterPath +“\\”+ $ _' #delete 2003键 Remove-Item -Path $ strkeytodelete 它抛出 Remove-Item:找不到驱动器。名称为“hklm”的驱动器不存在 我的逻辑有什么问题? –

+1

删除单引号,它们使它成为一个字符串并且也是不必要的 –

+0

假设您修复了单引号,将其修改为:Registry :: hklm – Nick