3

I am trying to use the Invoke-Command powershell cmdlet to install a MSI installer. From within powershell on the local machine and from the proper directory, the following works:

./setup /quiet

The following does not seem to work:

$script =
{
    param($path)
    cd "$path"
    & ./setup /quiet
    return pwd
}
return Invoke-Command -ComputerName $product.IPs -ScriptBlock $script -Args $sourcePath

For test purposes I am working on the local machine passing in "." for the -ComputerName argument. The paths have been verified correct before passing in to Invoke-Command, and errors generated on different versions of this code indicate the paths are correct. I have also tried with and without the "& " on the remote call to setup. Other Invoke-Command calls are working, so I doubt it is a permissions issue. I have verified that the return from the pwd call is the expected directory.

How do I get the install to work?

PatrickV
  • 2,057
  • 23
  • 30

3 Answers3

0

I have had weird issues when trying to remotely execute a script on a local machine. In other words, remote powershell to the local machine. It comes back with an error that seems to say that PowerShell remoting is not enabled on the machine, but it was. I can run the script remotely from another machine to the target, but when using remoting to the same box, the issue crops up.

  • Verify that the WinRM service is running.
  • Verify powershell remoting has been enabled as in Enable-PSRemoting -force.
  • Verify your powershell execution policy is loose enough as in Set-ExecutionPolicy Unrestricted, for example. If the policy was set to RemoteSigned, this might be the problem.

You might also want to verify the user you are running the script as (locally, but using remoting) has privileges to "log on as a service" or as a batch job. Just guessing there, if the above list doesn't solve anything.

Greg
  • 906
  • 1
  • 9
  • 22
0

What error (if any) are you receiving? Unfortunately, you must run the shell as admin on your local machine to be able to connect to your local machine with invoke-command or any WINRM based command that requires administrative privilege (this is not a requirement when connecting remotely).

When connecting to loopback, I believe it is unable (for some security reason) to enumerate groups and determine if you are in an admin enabled AD or local group, which is how it auto elevates when invoking on a remote machine. The only solution may be to have a conditional which checks for localhost and if so, don't use the -ComputerName parameter.

This GitHub Issue covers it

DGM
  • 76
  • 5
  • Marking this as an accepted answer, although I don't have the ability to test the answer. This is a 10-year-old question and I no longer have access to the environment. – PatrickV Dec 07 '22 at 14:52
0

You might try using Start-Process in your script block:

cd $path start-process setup.exe -arg "/quiet"

Not sure if you will want or need to wait. Look at help for Start-Process.

Jeffery Hicks
  • 947
  • 5
  • 8
  • I'll give it a shot in the morning. I did try Start-Process, but from your example I got the syntax wrong. – PatrickV Feb 10 '12 at 03:02
  • Well, that was better I guess, but still doesn't work. Same behavior - works from a local powershell, but doesn't work via an Invoke-Command talking to the local machine. At this point my best guess is that it is a permissions issue with running an installer via a remote call. – PatrickV Feb 10 '12 at 19:41
  • That could be. I've run into apps that refuse to install via a remote desktop session. What about setting up a one-time scheduled task on the remote machine to run the install? – Jeffery Hicks Feb 13 '12 at 13:17
  • I'm going to try to install a script on the target machine and just kick it off remote - just waiting on IT to give me the appropriate permissions. – PatrickV Feb 13 '12 at 13:36