加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Powershell-远程操作

发布时间:2020-12-15 22:22:59 所属栏目:安全 来源:网络整理
导读:1. 查看WinRM是否开启 ?Get-Service WinRM 2. Enable-PSRemoting –Force 3. 进行信任设置: Set-Item wsman:localhostclienttrustedhosts * Restart-Service WinRM 4. 测试远程连接 Test-WsMan xxx.xxx.xxx.xxx 5. 建立连接 Enter-PSSession -ComputerNa

1. 查看WinRM是否开启

?Get-Service WinRM

2. Enable-PSRemoting –Force

3. 进行信任设置:

Set-Item wsman:localhostclienttrustedhosts *

Restart-Service WinRM

4. 测试远程连接

Test-WsMan xxx.xxx.xxx.xxx

5. 建立连接

Enter-PSSession -ComputerName my-svr -Credential nickli

?

创建凭证

?

$Username = ‘zhnaghao‘

$Password = ‘mima‘

$pass = ConvertTo-SecureString -AsPlainText $Password -Force

$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

?

进入会话

$Username = ‘zhnaghao‘

$Password = ‘mima‘

$pass = ConvertTo-SecureString -AsPlainText $Password -Force

$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

Enter-PSSession -ComputerName SDSVD13 -Credential $Cred

?

退出会话

Exit-PSSession

?

执行单条命令:

$Username = ‘zhnaghao‘

$Password = ‘mima‘

$pass = ConvertTo-SecureString -AsPlainText $Password -Force

$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

Invoke-Command -ComputerName SDSVD13 -ScriptBlock {? [System.Net.DNS]::GetHostByName(‘‘).HostName

?} -credential $Cred?

?

远程执行脚本

$Username = ‘zhnaghao‘

$Password = ‘mima‘

$pass = ConvertTo-SecureString -AsPlainText $Password -Force

$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

Invoke-Command -computername sdsvd13 -FilePath C:UsersSDSADMINDesktopPowershellNEWhostname.ps1? -credential $Cred?

远程返回结果复制给变量

$Username = ‘zhnaghao‘

$Password = ‘mima‘

$pass = ConvertTo-SecureString -AsPlainText $Password -Force

$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

$mySession = new-PSSession -ComputerName SDSVD13 -Credential $Cred

$sub = Invoke-Command -Session $mySession -ScriptBlock {[System.Net.DNS]::GetHostByName(‘‘).HostName}

$sub

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读