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

This topic contains 3 replies, has 3 voices, and was last updated by  postanote 1 week, 5 days ago.

  • Author
    Posts
  • #85337

    Akhtar Nawaz
    Participant

    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.

    Thanks,
    Akhtar Nawaz

  • #85339

    Don Jones
    Keymaster

    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

      Akhtar Nawaz
      Participant

      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.

      Thanks,
      Akhtar Nawaz

    • #85343

      postanote
      Participant

      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
      Pop-Location
      }

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

      PowerShell: Running Executables
      'social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx'

      Calling native commands from PowerShell
      'powershellmagazine.com/2013/01/02/calling-native-commands-from-powershell'

You must be logged in to reply to this topic.