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!
-ArgumentListofInvoke-Commandmight be what you are looking for