Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PowerShell在远程服务器上运行批处理脚本_Powershell_Batch File_Remote Server - Fatal编程技术网

通过PowerShell在远程服务器上运行批处理脚本

通过PowerShell在远程服务器上运行批处理脚本,powershell,batch-file,remote-server,Powershell,Batch File,Remote Server,我需要从客户端(与服务器相同的域)连接到一些远程服务器连接后,我需要运行批处理文件: 我使用以下代码完成了此操作: $Username = 'USER' $Password = 'PASSWORD' $pass = ConvertTo-SecureString -AsPlainText $Password -Force $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pas

我需要从客户端(与服务器相同的域)连接到一些远程服务器连接后,我需要运行批处理文件:

我使用以下代码完成了此操作:

$Username = 'USER'
$Password = 'PASSWORD'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

try {
    Invoke-Command -ComputerName "SERVER1" -Credential $Cred -ScriptBlock -ErrorAction Stop {
        Start-Process "C:\Users\nithi.sundar\Desktop\Test.bat"
    }
} catch {
    Write-Host "error"
}
此脚本没有给出任何错误,但它似乎没有执行批处理脚本

如果您对此有任何意见,我们将不胜感激。

请尝试更换

invoke-command -computername "SERVER1" -credential $Cred -ScriptBlock -ErrorAction stop { Start-Process "C:\Users\nithi.sundar\Desktop\Test.bat" }


您发布的代码不可能没有错误地运行,因为您将参数的顺序弄乱了,无法调用命令。这:

Invoke-Command ... -ScriptBlock -ErrorAction Stop { ... }
实际上应该是这样的:

Invoke-Command ... -ErrorAction Stop -ScriptBlock { ... }
另外,不要为此使用
调用表达式。这是为了你需要完成的任何事情。由于PowerShell可以直接运行批处理脚本,因此您也不需要
启动进程

Invoke-Command -ComputerName "SERVER1" -ScriptBlock {
    C:\Users\nithi.sundar\Desktop\Test.bat
} -Credential $Cred -ErrorAction Stop
如果命令是字符串而不是纯单词,则需要使用:

您还可以使用
cmd.exe
调用批处理文件:

Invoke-Command -ComputerName "SERVER1" -ScriptBlock {
    cmd /c "C:\Users\nithi.sundar\Desktop\Test.bat"
} -Credential $Cred -ErrorAction Stop
如果出于某种原因必须使用
启动流程
,则应添加参数
-nonewindow
-Wait

Invoke-Command -ComputerName "SERVER1" -ScriptBlock {
    Start-Process 'C:\Users\nithi.sundar\Desktop\Test.bat' -NoNewWindow -Wait
} -Credential $Cred -ErrorAction Stop

默认情况下,
Start Process
在单独的窗口中异步运行被调用的进程(即调用立即返回)。这很可能是您的代码没有按预期工作的原因。

我正在尝试动态创建路径,但它给出了错误->$toinstall=(““+”cmd.exe/c“+$inputdata.TargetFolder+”\'+$inputdata.InstallerBat+”)------------------------------------调用命令-ComputerName$inputdata.TargetMachineName-ErrorAction停止-ScriptBlock{Invoke Expression-Command:$toinstall}-----------------错误:无法将参数绑定到参数“Command”,因为它为空。。
Invoke-Command -ComputerName "SERVER1" -ScriptBlock {
    cmd /c "C:\Users\nithi.sundar\Desktop\Test.bat"
} -Credential $Cred -ErrorAction Stop
Invoke-Command -ComputerName "SERVER1" -ScriptBlock {
    Start-Process 'C:\Users\nithi.sundar\Desktop\Test.bat' -NoNewWindow -Wait
} -Credential $Cred -ErrorAction Stop