目录
远程管理方式
windows远程连接windows,我知道的有这么几种:RDP、WinRM、ssh。
rdp方式
UI操作,适合手工,不适合自动化。
Write-Output "Please Enter The Targets Computer Name or IP!"
$targetCompName = Read-Host -Prompt "Target: "
Write-Output "Please Enter The Username!"
$userName = Read-Host -Prompt "UserName: "
Write-Output "Please Enter The Password!"
$password = Read-Host -Prompt "Password: "
# 添加凭证
cmdkey /generic:TERMSRV/$targetCompName /user:$userName /pass:$password
# 登录
mstsc /v:$targetCompName
winrm方式
这种是最正统的,也是适合于实现自动化的。
windows机器默认没有开启winrm远程,需要运行 Ennable-PSRemoting
来开启。
ssh方式
如果只作为客户端,那么只需要ssh.exe添加到PATH。如果也可能作为服务端,那么sshd.exe也需要被添加到PATH。
New-PSSession -HostName "" -UserName "" -KeyFilePath ""
如果不带 -KeyFilePath
,那么就需要手工输入密码。
常用cmdlets
Enter-PSSession
适用于一对一,开启交互式shell
Invoke-Command
适用于一对多,在本地或远程执行命令
New-PSSession
创建session,可以同时创建多个主机的session,可用于Invoke-Command
的-Session
参数。
同时记得用完之后,Remove-Session
,同时还有一个Get-Session
。
Get-Credential
直接运行Get-Credential
会提示输入账户密码,将其保存在一个变量中,后续可以在New-PSSession
等命令的-Cred
参数中使用。
Connect-WSMan
这个是不包含于pscore的cmdlets,也就是仅windows平台支持。
开启winrm连接,https://docs.microsoft.com/en-us/powershell/module/microsoft.wsman.management/connect-wsman?view=powershell-7.1