0

I am trying to write a script that will remove old queues from users HKLM (will eventually delete from HKCU by mounting ntuser.dat but I am not there yet).

The problem I am having is that I am only iterating through one sid under SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\ and I get the following error message:

The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is corr ect and try again.

Has anyone ran into this issue before?

  #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
  • 1
    You've got the right number of curly braces but one is in the wrong place. It's hard to tell which from a quick review, i'd tidy up your indentation a little and make sure they all match up where you expect them to. One clue that this is the issue is that ISE is highlighting "else" in a different colour and the error is basically saying "there is an else statement where I didn't expect one". Commented Apr 26, 2017 at 17:36

1 Answer 1

1

You have an extra curly brace on line 153. If you move that to after line 165 it should work, although I can't test it right now. I got in the habit of systematically collapsing my if-else statements to ensure that they all match up with eachother.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! That piece is working. I am now trying to delete the 2003 keys found and the following is not working: $strkeytodelete='"hklm:\"+$strPrinterPath+"\\"+$_' #delete 2003 Key Remove-Item -Path $strkeytodelete It throws Remove-Item : Cannot find drive. A drive with the name '"hklm' does not exist. What is wrong with my logic there?
Remove the single quotes. They are making it a literal string and are also unnecessary.
Assuming you fixed the single quotes, try modifying it to this: Registry::hklm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.