簡體   English   中英

使用PowerShell腳本中的參數在遠程服務器上執行.cmd文件

[英]Executing .cmd file on remote server with parameter from PowerShell script

我正在嘗試在具有變量的遠程服務器上執行.cmd文件,但是傳遞變量存在問題,這會導致以下錯誤消息:

術語'D:\\ MyDir \\ MyFile.cmd myparam'不被識別為cmdlet,函數,腳本文件或可運行程序的名稱。 檢查名稱的拼寫,或者是否包含路徑,請驗證路徑是否正確,然后重試。

僅當我嘗試使用變量/參數執行.cmd時,才會發生此錯誤。

這是我的代碼-

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

任何幫助深表感謝和TIA!

問題是它正在尋找不存在的文件“ MyFile.cmd myparam”。 您需要移動引號,以使參數位於單獨的字符串中:

$Script = {&"D:\MyDir\MyFile.cmd" myparam}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM