help with -ea stop

Welcome Forums General PowerShell Q&A help with -ea stop

Viewing 5 reply threads
  • Author
    Posts
    • #191254
      Participant
      Topics: 21
      Replies: 74
      Points: 354
      Rank: Contributor

      Have this working with invoke command but not as local command.

      The commands with the # was working on my desktop but I run this script on the server. So I needed to convert the commands to local commands

      
      $Results += $("" | Out-String)
      $Results += 'Server TGCS012' | out-string
      try
      {
      #$results += invoke-command -computer TGCS012 -scriptblock {reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations /f} -ea Stop
      #$results += invoke-command -computer TGCS012 -scriptblock {(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager' -Name PendingFileRenameOperations ).PendingFileRenameOperations} -ea Stop
      $results += reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations /f
      $results += (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager' -Name PendingFileRenameOperations ).PendingFileRenameOperations
      
      }
      catch { $Results += $_.exception.message}
      
      

      reg : ERROR: The system was unable to find the specified registry key or value.
      At line:7 char:13
      + $results += reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\C ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : NotSpecified: (ERROR: The syst...y key or value.:String) [], RemoteException
      + FullyQualifiedErrorId : NativeCommandError

      Get-ItemProperty : Property PendingFileRenameOperations does not exist at path
      HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager.
      At line:8 char:14
      + ... results += (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYST ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidArgument: (PendingFileRenameOperations:String) [Get-ItemProperty], PSArgumentException
      + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException,Microsoft.PowerShell.Commands.GetItemPropertyCommand

      PS C:\Util> $results
      The operation completed successfully.

      Server TGCS012

       

      Any ideas

       

      Thank you

      Tom

       

       

       

       

       

       

       

       

    • #191263
      Participant
      Topics: 21
      Replies: 38
      Points: 202
      Helping Hand
      Rank: Participant

      You need to clear the basic concept of error handling. Please read this document. Each and every line is important.

      https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-6

    • #191266
      Senior Moderator
      Topics: 8
      Replies: 1140
      Points: 3,922
      Helping Hand
      Rank: Community Hero

      Not really able to understand the question. The title and post content is confusing. Can you elaborate a little more on the expectation from your script.

    • #191296
      Participant
      Topics: 9
      Replies: 36
      Points: 156
      Helping Hand
      Rank: Participant

      Just a suggestion that may or may not help – check out using Remove-Item instead of "reg delete" to remove a registry key.

      I like to use PowerShell cmdlets whenever I can, instead of going back to older commands.

    • #191299
      Participant
      Topics: 21
      Replies: 74
      Points: 354
      Rank: Contributor
      
      PS C:\util> $Results += Remove-item 'hklm:\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations'
      -ea Stop
      Remove-item : Cannot find path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations'
      because it does not exist.
      At line:1 char:13
      + $Results += Remove-item 'hklm:\SYSTEM\CurrentControlSet\Control\Sessi ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : ObjectNotFound: (HKLM:\SYSTEM\Cu...enameOperations:String) [Remove-Item], ItemNotFoundEx
      ception
      + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
      
      

      The command in a DOS Windows worked

      C:\>reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations /f
      The operation completed successfully.

      How to get this to work in powershell?

       

       

       

    • #191341
      Senior Moderator
      Topics: 8
      Replies: 1140
      Points: 3,922
      Helping Hand
      Rank: Community Hero

      You would use Remove-ItemProperty to do this.

      # Take a backup of this registry key before testing.
      Remove-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' -Name PendingFileRenameOperations -WhatIf
      # Remove -WhatIf to take the action.
      
Viewing 5 reply threads
  • You must be logged in to reply to this topic.