Power shell 复制文件到远程主机

昨天因为要复制文件到多个服务器上,感觉一个个复制很麻烦,我就想用Power shell 命令来完成这个任务。具体代码如下:

$servers= 'server1' ,'server2'
$username='jimt'
$srcUrl="E:\test1.txt"
$disUrl="c:\test1.txt"

$password = ConvertTo-SecureString -String 'Aa1111111' -AsPlainText -Force
$credential =New-Object System.Management.Automation.PSCredential -argumentlist $username ,$password

foreach($server in $servers){

$sessions=New-PSSession -ComputerName $server -credential $credential
$File = [System.IO.File]::ReadAllBytes($srcUrl)
Invoke-Command -Session $sessions -ArgumentList $disUrl,$file -ScriptBlock{[System.IO.File]::WriteAllBytes($args[0],$args[1])}

}

核心代码为 Invoke-Command 直接调用远程主机的copy命令,System.IO.File 是除了写命令,还有new ,delete 命令

另下面的link的文章采用了share folder 映射的方法copy-item ,以后也可以作为参考 

https://www.pstips.net/powershell-transform-file-remotely.html


猜你喜欢

转载自www.cnblogs.com/jimfighting/p/8965912.html
今日推荐