【问题标题】:Powershell error with else statement带有 else 语句的 Powershell 错误
【发布时间】:2017-04-27 01:44:43
【问题描述】:

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

我遇到的问题是我只遍历 SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\ 下的一个 sid,我收到以下错误消息:

“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

         }


}

【问题讨论】:

  • 你有正确数量的花括号,但一个在错误的位置。很难从快速回顾中分辨出哪一个,我会稍微整理一下您的缩进,并确保它们都与您期望的位置相匹配。这是问题的一个线索是 ISE 以不同的颜色突出显示“else”,并且错误基本上是说“有一个我没想到的 else 语句”。

标签: powershell registry


【解决方案1】:

你在第 153 行有一个额外的花括号。如果你把它移到第 165 行之后,它应该可以工作,尽管我现在无法测试它。我养成了系统地折叠我的 if-else 语句的习惯,以确保它们都相互匹配。

【讨论】:

  • 谢谢!那件作品正在工作。我现在正在尝试删除找到的 2003 密钥,但以下内容不起作用: $strkeytodelete='"hklm:\"+$strPrinterPath+"\\"+$_' #delete 2003 Key Remove-Item -Path $strkeytodelete 它抛出删除项目:找不到驱动器。名为“hklm”的驱动器不存在。我的逻辑有什么问题?
  • 去掉单引号。他们使它成为一个文字字符串,也是不必要的。
  • 假设您修复了单引号,请尝试将其修改为:Registry::hklm
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多