PowerShell 批量拷贝本地文件到远程多台主机

Posted CIAS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PowerShell 批量拷贝本地文件到远程多台主机相关的知识,希望对你有一定的参考价值。

 

 批量拷贝本地文件到远程主机自动化脚本

  • $user="administrator" #系统用户
  • $password=ConvertTo-SecureString "YOU_password" -AsPlainText -Force #用户密码
  • $pchost='192.168.19.10','192.168.12.249' #目标主机IP,有多少编写多少
powershell-Copy-local_files-remote-host.ps1
<# Powershell Copy local files to a remote host
+++++++++++++++++++++++++++++++++++++++++++++++++++++
+  _____                       _____ _          _ _ +
+ |  __ \\                     / ____| |        | | |+
+ | |__) |____      _____ _ _| (___ | |__   ___| | |+
+ |  ___/ _ \\ \\ /\\ / / _ \\ '__\\___ \\| '_ \\ / _ \\ | |+
+ | |  | (_) \\ V  V /  __/ |  ____) | | | |  __/ | |+
+ |_|   \\___/ \\_/\\_/ \\___|_| |_____/|_| |_|\\___|_|_|+
+ +++++++++++++++++++++++++++++++++++++++++++++++++++
                                                                                                              
# Copy local files to a remote host
# .\\powershell-Copy-local_files-remote-host.ps1
#> 


$user="administrator"
$password=ConvertTo-SecureString "YOU_password" -AsPlainText -Force
$cred=New-Object System.Management.Automation.PSCredential($user,$password)
$pchost='192.168.19.10','192.168.12.249'
$port=5985

$local_path="D:\\Powershell-Windows_Admin_Center-install\\"
$Destination="d:\\"

foreach($server in $pchost)

# Create a session with remote computer using New-Session
$Session = New-PSSession -ComputerName $server -Credential $cred

Write-Host "Log on to the host $pchost" -ForegroundColor Green
Enter-PSSession -ComputerName $server -port $port -Credential $cred

Write-Host "Use Copy-Item to copy files to a remote computer $pchost" -ForegroundColor Green
Copy-Item -Path "$local_path" -Destination $Destination -ToSession $session -Recurse

Write-Host "Restart winRM the service $pchost" -ForegroundColor Green
Restart-Service winRM

执行批量拷贝本地文件到远程主机

.\\powershell-Copy-local_files-remote-host.ps1

结果输出

以上是关于PowerShell 批量拷贝本地文件到远程多台主机的主要内容,如果未能解决你的问题,请参考以下文章

PowerShell 批量部署windows_exporter到所有Windows主机

PowerShell 批量部署windows_exporter到所有Windows主机

PowerShell 批量部署VMware-tools到所有Windows主机

PowerShell 批量部署VMware-tools到所有Windows主机

PowerShell 批量部署VMware-tools到所有Windows主机

PowerShell 批量创建用户用户组到所有Windows系统