1

I am trying to execute a .cmd file on a remote server with a variable but I am having issues with passing the variable which is causing the below error message:

The term 'D:\MyDir\MyFile.cmd myparam' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

This error only occurs when I attempt to execute the .cmd with a variable / parameter.

Here is my code-

param($InParam)
$Username = 'MYMACHINE\myuser'
$Password = 'mypassword'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
If ($InParam -eq "test")
{
$Script = {&"D:\MyDir\MyFile.cmd myparam"}
Invoke-Command -ComputerName MY-PC-NAME -Authentication Default -ScriptBlock $Script -Credential $Cred

Any help hugely appreciated and TIA!

1
  • The parameter -ArgumentList of Invoke-Command might be what you are looking for Commented Mar 10, 2016 at 11:10

1 Answer 1

2

The problem is it's looking for the file "MyFile.cmd myparam" which doesn't exist. You need to move your quotes so that the param is in a separate string:

$Script = {&"D:\MyDir\MyFile.cmd" myparam}
Sign up to request clarification or add additional context in comments.

1 Comment

So simple yet this has been troubling me for hours! Thank you

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.