欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

跨平台PowerShell如何远程管理Linux/Mac/Windows?

程序员文章站 2023-12-30 14:49:28
...


跨平台PowerShell如何远程管理Linux/Mac/Windows?


首先,在要管理的机器上安装跨平台PowerShell:https://github.com/PowerShell/PowerShell/releases

如何安装呢?看Instructions:

Platform Downloads How to Install
Windows 10 / Server 2016 .msi Instructions
Windows 8.1 / Server 2012 R2 .msi Instructions
Ubuntu 16.04 .deb Instructions
Ubuntu 14.04 .deb Instructions
CentOS 7 .rpm Instructions
OS X 10.11 .pkg Instructions
Docker   Instructions

然后,安装OMI和PSRP:

OMI:https://github.com/Microsoft/omi

PSRP:https://github.com/PowerShell/psl-omi-provider

Platform Releases Link
Linux Debian psrp-1.0.0-0.universal.x64.deb
Linux RPM psrp-1.0.0-0.universal.x64.rpm

最后,在windows端运行下列命令来管理Linux/Mac/Windows机器:

$User = "root"
$PWord = convertto-securestring "密码" -asplaintext -force
$cred = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $PWord
$mySession = New-PSSession  -ComputerName 机器名 -Credential $cred -Authentication basic -UseSSL -SessionOption (New-PSSessionOption -SkipCACheck -SkipRevocationCheck -SkipCNCheck)
Invoke-Command -Session $mySession {Get-Host} 

如果你想把本地的脚本在被管理的机器上运行:

$script1='get-host';
$path1='/root/test.ps1';
Invoke-Command -Session $mySession {echo $args[0]>$args[1];. $args[1]} -Args $script1,$path1

读取本地的文件,然后在被管理的机器上运行:

$script1 = Get-Content "d:\test.txt"
$path1='/root/test.ps1';
Invoke-Command -Session $mySession {echo $args[0]>$args[1];. $args[1]} -Args $script1,$path1



上一篇:

下一篇: