0

I need to run a remote command with help of PowerShell from CMD. This is the command I call from CMD:

powershell -command "$encpass=convertto-securestring -asplaintext mypass -force;$cred = New-Object System.Management.Automation.PSCredential -ArgumentList myuser,$encpass; invoke-command -computername "REMOTE_COMPUTER_NAME" -scriptblock {<command>} -credential $cred;"

in place of <command> (including < and > signs) can be any command which can be run in cmd.exe. For example there can be perl -e "print $^O;" or echo "Hello World!" (NOTE: There cannot be perl -e 'print $^O;', because it is incorrect command for CMD due to the single quotes). So it appears the command perl -e "print $^O;" and any other command which contains double quotes doesn't handled as expected. Here I expect it to return OS name of remote box from perl's point of view, but it prints nothing due to obscure handling of double quotes by PowerShell and/or CMD.

So the question is following, how to run command correct for CMD in remote box using PowerShell?

2
  • Are you sure it isn't an effect of unescaped quotes (") near REMOTE_COMPUTER_NAME? Commented Jul 17, 2013 at 14:38
  • @user268396 Yes I am. Generally I've tried with single-quoted REMOTE_COMPUTER_NAME and it doesn't affect to the result of any operation. I can surely tell that the problem is in <command>. Commented Jul 18, 2013 at 8:26

1 Answer 1

1

There are several possible problems with the command line in the OP. If the command line in the OP is being executed from Powershell itself the $encpass and $cred will get substituted before the (sub-instance) of powershell is invoked. You need to use single quotes or else escape the $ signs, for example:

powershell -command "`$encpass=2"
powershell -command '$encpass=2'

If, instead of using Powershell, the command line is executed from CMD, then ^ has to be escaped, because it is the CMD escape character.

And quoting " is a good idea as well. In a few tests that I did I had to use unbalanced quotes to get a command to work, for example, from powershell:

powershell -command "`$encpass=`"`"a`"`"`"; write-host `$encpass"

worked, but balanced quotes didn't.

To avoid all this, probably the most robust way to do this is given in powershell command line help: powershell -?:

# To use the -EncodedCommand parameter:
$command = 'dir "c:\program files" '
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand

However there is a new feature in PS 3.0 that is also supposed to help, but I don't think it will be as robust. Described here: http://blogs.msdn.com/b/powershell/archive/2012/06/14/new-v3-language-features.aspx, near the middle of the blog.

Sign up to request clarification or add additional context in comments.

1 Comment

This is not what I want. But it helps me to figure out the problem. So I have written perl -e """print `$^O;""" instead of perl -e "print $^O;" and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.