在远程计算机上运行.ps1 [英] Run .ps1 on remote machine
问题描述
我需要在具有管理员权限的远程计算机上执行Powershell脚本。
我有2个脚本:客户端和服务器。
当我启动client.ps1时,我在服务器计算机上调用命令,但出现访问错误。如果在server.ps1中使用简单的代码(如写入主机 hello),则不会出错。
I need to execute powershell script on remote computer with admin privilegies. I have 2 scripts: client and server. When i start client.ps1 i invoke command on server machine but get access error. I get no error if I use simple code in server.ps1 like write-host "hello".
server.ps1:
server.ps1:
Get-service -ComputerName 'client'
client.ps1:
client.ps1:
$ScriptBlockContent = {
d:\server.ps1
}
$remote=New-PSSession -ComputerName 'server'
Invoke-Command $remote -ScriptBlock $ScriptBlockContent
推荐答案
您的问题是身份验证。您必须启用服务器才能使用您的凭据。您可以使用 CredSSP
来做到这一点。
Your problem is authentication. You have to enable the server to use your credentials for that. You can do this by using CredSSP
.
-
在您的计算机上启用此功能客户端:
Enable this on your client:
启用WSManCredSSP-角色客户端-DelegateComputer ServerNameHere
Enable-WSManCredSSP -Role Client -DelegateComputer ServerNameHere
在服务器上启用它:
启用WSManCredSSP-角色服务器
Enable-WSManCredSSP -Role Server
现在将其添加到您的调用命令
:
-凭据域\YourUsername -身份验证CredSSP
请注意:使用CredSSP,如果您连接到受感染的系统,则很容易窃取您的凭据(与RDP)。确保仅在安全计算机上执行此操作。
A remark on that: With CredSSP, its easy to steal your credentials, if you connect to a compromised system (same as RDP). Be sure that you do this on secure computers only.
这篇关于在远程计算机上运行.ps1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!