通过PowerShell在远程服务器上运行批处理脚本 [英] Running Batch Script on remote Server via PowerShell

查看:562
本文介绍了通过PowerShell在远程服务器上运行批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to connect to some remote servers from a client (same domain as the servers) once connected, I need to run a batch file:

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

I've done so with this code:

$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"
}

此脚本没有给出任何错误,但似乎没有执行批处理脚本.

This script does not give any errors, but it doesn't seem to be executing the batch script.

对此表示感谢.

推荐答案

尝试替换

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

使用

Invoke-Command -ComputerName "Server1" -credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c 'C:\Users\nithi.sund
ar\Desktop\Test.bat'"}

这篇关于通过PowerShell在远程服务器上运行批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆