当前位置: 移动技术网 > IT编程>脚本编程>Shell > PowerShell 远程执行任务的方法和步骤

PowerShell 远程执行任务的方法和步骤

2020年07月18日  | 移动技术网IT编程  | 我要评论

PowerShell 远程执行任务的方法步骤

1. 查看WinRM服务

Get-Service WinRM

在这里插入图片描述
如果为关闭状态,以管理员权限启动PowerShell窗口,执行命令:

Enable-PSRemoting –Force

如果计算机已经加入了服务器相同的域,那么上面的配置就可以了。对于没有加入域的计算机还需要进行信任设置,远程服务器也执行相同的命令,然后重启 WinRM 服务:

Set-Item wsman:\localhost\client\trustedhosts *
Restart-Service WinRM

2. 测试远程连接

Test-WsMan xxx.xxx.xxx.xxx
如果成功返回信息则说明远程主机已经配置好了允许远程访问的服务,如果返回错误的信息,则说明远程主机没有打开远程访问的服务。

远程执行命令:
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $pass
$Session = New-PSSession -ComputerName $ip -Credential $Cred
Invoke-Command -ComputerName $ip -ScriptBlock {     “PowerShell 相关执行命令”    } -credential $Cred
远程执行脚本:
Invoke-Command -ComputerName $ip -FilePath .\****.ps1 -credential $Cred

本文地址:https://blog.csdn.net/a1014825552/article/details/107402734

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网