Invoke-Command remote computer

Welcome Forums General PowerShell Q&A Invoke-Command remote computer

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

 
Keymaster
1 year, 6 months ago.

  • Author
    Posts
  • #70400

    Participant
    Points: 1
    Rank: Member

    Hello,

    I have read all kinds of documentation and examples, but I simply cannot start this .exe using Invoke-Command. I look for it in the processes list and it never starts on the remote system, but I do not get any error in my local console. Here it is...

    Invoke-Command -ComputerName remotecomp -ScriptBlock {
    
    $ccmCleanLocal = "C:\hold-1\ccmclean.exe"
    
    if ([System.IO.File]::Exists($ccmCleanLocal)) {
    
    Start-Process -FilePath $ccmCleanLocal
    
    $wait = Get-Process ccmclean.exe
    
    $wait.WaitForExit()
    
    } Else {"ccmclean is not in hold-1"}
    
    }

    No errors on my end. The process never starts on the remote computer. PS remoting works, as if I change the if statement to:

    if ([System.IO.File]::Exists($ccmCleanLocal)) {"yep"}

    ...my console will return yep. I can also do other tasks remotely. Also if I remove ccmclean.exe from \hold-1, my console tells me it is not there.

    I have tried adding -credential, still does not work.

    Why wouldn't this work?!

    Thank you!

  • #70478

    Keymaster
    Points: 1,644
    Helping HandTeam Member
    Rank: Community Hero

    You might find that Test-Path offers a "cleaner looking" way of testing for the presence of the executable.

    Are you able to connect to the remote computer using Enter-PSSession, and then simply run your Start-Process command interactively, to see what happens?

    • #70529

      Participant
      Points: 1
      Rank: Member

      Hi Don,

      The plan is to run this as part of a script on several machines at once. I am not sure Enter-PSSession would work in that scenario?

      I got this to work (mostly) by using PSExec...

      ForEach ($pc in $list) {
      
      $cmd = "C:\hold-1\ccmclean.exe"
      
      $args = "/Q"
      
      if ((Test-Connection -ComputerName $pc -Quiet) -And (Test-Path -Path "\\$pc\c$\Hold-1\ccmclean.exe")) { & "C:\PSTools\psexec.exe" \\$pc $cmd} 
      
      Else 
      
      {"$pc is not online, or ccmclean is not in \Hold-1"}
      
      } 

      What I cannot seem to do properly is add args to the ccmclean.exe command. I need to run it with /Q.

  • #70624

    Participant
    Points: 0
    Rank: Member

    Start-Process [-FilePath] [[-ArgumentList] ]

  • #70649

    Keymaster
    Points: 1,644
    Helping HandTeam Member
    Rank: Community Hero

    My suggestion with using Enter-PSSession was as a troubleshooting step. If you can get the command to run that way, then you'll know you have the correct syntax and permissions and everything, and then you can copy and paste the working command into a script.

The topic ‘Invoke-Command remote computer’ is closed to new replies.