Unable to run an exe on remote computer via Invoke-command

Welcome Forums General PowerShell Q&A Unable to run an exe on remote computer via Invoke-command

This topic contains 3 replies, has 3 voices, and was last updated by

1 year, 1 month ago.

  • Author
  • #85337

    Points: 0
    Rank: Member

    Hi experts,

    I have recently learnt PS and trying to build a script which can reduce my work. Please don't mind if any silly mistake identified.

    My business Requirement – There are 78 servers where I have to uninstall SCCM agents. here is the script that works on local computer.

    Set-location C:\windows\CCMsetup | ccmsetup.exe /uninstall — this command uninstall SCCM agent on local computer.

    However, when i run it on remote computer i get an error that ccmsetup.exe isn't recognize as cmdlet or function etc.

    $session =New-PSSession -ComputerName MachineName
    Invoke-Command -Session $session -ScriptBlock {Set-Location C:\Windows\ccmsetup | ccmsetup.exe /uninstall}

    how can I get it working? please help.

    Akhtar Nawaz

  • #85339

    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    C:\Windows\ccmsetup | ccmsetup.exe /uninstall

    Is not technically correct.

    C:\Windows\ccmsetup ; ccmsetup.exe /uninstall

    Would be better. If the error persists, then you need to confirm that Ccmsetup.exe exists at that location on the remote computers. Also note that some uninstaller don't like running in a remoting session; if this turns out to be one of those uninstallers, there's really nothing you can do about it.

    • #85340

      Points: 0
      Rank: Member

      Hi Don, i'm studying your book for learning PS. so glad to see your response on my first question in this forum 🙂

      Thank you for correction in script. I confirm that Ccmsetup.exe file does exist on remote computer. with a little bit more effort i found someone already uninstalled SCCM via PS remoting. I made changes accordingly in my script and it worked.

      $session =New-PSSession -ComputerName MachineName
      Invoke-Command -Session $session -ScriptBlock {C:\windows\ccmsetup\ccmsetup.exe /uninstall }

      But i still don't know why it was not working? logically I need to understand.

      Akhtar Nawaz

    • #85343

      Points: 512
      Helping Hand
      Rank: Major Contributor

      Firstly, this..

      $session =New-PSSession -ComputerName MachineName
      Invoke-Command -Session $session -ScriptBlock {Set-Location C:\Windows\ccmsetup | ccmsetup.exe /uninstall}
      ... is not correct.

      Do, something like this...

      $session =New-PSSession -ComputerName MachineName
      Invoke-Command -Session $session -ScriptBlock {
      # Change to the executable location
      Push-Location Path C:\Windows\ccmsetup

      # Execute the process and wait for completion before continuing
      Start-Process -FilePath -Arguments -wait

      # Return to the original directory

      Secondly, running native commands / exe require a different approach.
      See these articles:

      PowerShell: Running Executables

      Calling native commands from PowerShell

The topic ‘Unable to run an exe on remote computer via Invoke-command’ is closed to new replies.