powershell实现批量分发文件小结

$Username = 'Administrator'
$Password = 'Ccaoweogtoeo256335@^$'

$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass


$ips= '139.159.138.5','139.9.68.72','139.9.73.156','139.159.138.236','139.159.229.64','139.9.66.248','139.9.69.197','139.159.134.23','139.159.160.112','119.3.183.8','119.3.251.155','119.3.177.172','119.3.169.168','119.3.163.162','119.3.163.143','119.3.179.41','119.3.224.99','119.3.249.13','119.3.161.233','139.9.231.46','139.9.230.140','139.9.228.133','139.9.234.117','139.9.230.207','139.9.239.89','139.9.229.28','139.9.235.200','139.9.238.129','139.9.229.146','139.9.239.48','139.9.241.123','139.9.238.128','139.9.228.211','139.9.230.15','139.9.232.99','139.9.234.144','139.9.243.98','139.9.249.140','139.9.234.129','122.112.145.81','122.112.173.205','122.112.190.178','122.112.175.102','122.112.189.86','122.112.175.149','122.112.161.122','122.112.145.150','122.112.146.1','122.112.169.142','122.112.176.194','122.112.161.138','122.112.154.99','122.112.146.174','122.112.177.59','122.112.186.124','122.112.177.88','122.112.187.85','122.112.162.251','122.112.181.202','122.112.190.115','122.112.169.73','122.112.175.211','122.112.158.149','122.112.191.47','122.112.160.49','122.112.182.58','122.112.159.148','122.112.168.206','122.112.180.37','122.112.184.144','122.112.148.38','122.112.159.36','122.112.158.227','122.112.157.235','122.112.149.239','122.112.180.246','122.112.173.240','122.112.171.132','122.112.150.73','122.112.177.164','122.112.172.163','122.112.179.112','122.112.154.125','122.112.177.121','122.112.166.100','122.112.167.203','119.3.80.91','119.3.77.0','119.3.5.84','122.112.213.165','122.112.182.137','119.3.37.138','122.112.165.213','119.3.16.138','119.3.107.9','122.112.148.76','122.112.155.81','119.3.105.225','122.112.181.42','119.3.1.179','122.112.146.58','119.3.111.165','119.3.71.127','122.112.149.63','119.3.24.235','122.112.150.223','122.112.148.52','122.112.150.14','122.112.232.48','119.3.105.41','122.112.189.145','122.112.187.31','122.112.163.43','122.112.182.24','122.112.164.164','122.112.188.252','122.112.153.193','119.3.39.221','122.112.162.87','122.112.171.90','119.3.25.133','122.112.152.208','119.3.67.188','119.3.23.61','119.3.27.100','119.3.107.237','122.112.152.155','122.112.185.116','122.112.150.93','122.112.171.153','122.112.186.71','122.112.174.245','122.112.169.85','122.112.176.236','119.3.174.210','119.3.163.66','119.3.178.39','119.3.178.146','119.3.183.138','119.3.180.158','119.3.183.62','119.3.171.152','119.3.171.68','119.3.169.60','119.3.179.189','119.3.188.102','119.3.180.201','119.3.182.49','119.3.165.176','119.3.164.247','119.3.160.137','119.3.185.230','119.3.170.73','119.3.180.232','119.3.178.182','119.3.190.11','119.3.172.222','119.3.184.245','119.3.165.191','119.3.177.240','119.3.177.26','119.3.177.89','119.3.165.71','119.3.164.161','119.3.186.71','119.3.191.6','119.3.189.152','119.3.180.46','119.3.181.14','119.3.169.31','119.3.184.48','119.3.191.80','119.3.173.124','119.3.167.80','119.3.166.73','119.3.189.125','119.3.185.179','119.3.190.145','119.3.170.135','119.3.163.68','119.3.174.40','119.3.165.67','119.3.162.26','119.3.165.90','119.3.179.0','119.3.167.92','119.3.181.189','119.3.165.87','119.3.187.22','119.3.177.59','119.3.187.91','119.3.164.5','119.3.161.175','119.3.173.5','119.3.180.62','119.3.171.124','119.3.163.138','119.3.178.212','119.3.163.39','119.3.163.207','119.3.160.225','119.3.191.202','119.3.177.112','119.3.163.2','119.3.167.73','119.3.175.241','119.3.166.220','119.3.163.226','119.3.185.142','119.3.164.136','119.3.189.234','119.3.183.111','119.3.181.60','119.3.178.153','119.3.182.252','119.3.161.135','119.3.168.41','119.3.181.15','119.3.181.6','119.3.173.3','119.3.179.186','119.3.169.43','119.3.165.59','119.3.108.31','119.3.5.93','119.3.51.57','119.3.17.109','122.112.148.51','119.3.1.3','119.3.64.210','119.3.6.25','119.3.111.1','119.3.22.179','122.112.163.1','119.3.77.68','119.3.71.222','122.112.184.28','119.3.73.39','119.3.90.107','119.3.88.122','119.3.89.121','119.3.41.218','122.112.167.231','122.112.155.252','119.3.4.33','119.3.88.240','119.3.7.165','122.112.160.34','119.3.69.139','122.112.147.109','119.3.67.157','119.3.48.9','119.3.85.239','119.3.69.97','122.112.156.225','122.112.189.208','119.3.90.98','119.3.57.149','122.112.163.106','119.3.26.21','119.3.47.215','122.112.175.64','119.3.88.49','119.3.91.57','119.3.68.81','122.112.187.161','119.3.52.35','122.112.181.66','119.3.110.163','122.112.169.64','119.3.28.224','119.3.104.73','119.3.26.175','122.112.245.253','119.3.83.163','122.112.253.133','122.112.202.157','119.3.110.98','119.3.45.220','119.3.72.103','119.3.48.169','119.3.68.110','122.112.160.239','122.112.155.35','122.112.227.104','122.112.170.223','119.3.26.231','122.112.251.101','119.3.22.130','119.3.56.144','122.112.184.156','122.112.150.208','119.3.103.132','119.3.106.233','122.112.160.230','119.3.64.42','119.3.64.94','122.112.182.159','122.112.173.149','122.112.173.114','122.112.170.239','122.112.182.240','122.112.181.143','122.112.183.41','122.112.191.163','122.112.170.116','122.112.176.166','122.112.164.250','122.112.186.118','122.112.160.189','122.112.183.210','122.112.180.188','122.112.163.93','122.112.166.157','122.112.181.218','122.112.163.152','122.112.189.225','139.9.66.140','139.9.61.57','139.9.215.23','139.9.192.130','139.9.39.26','139.9.90.9','139.159.205.142','139.159.203.198','139.159.206.191','139.9.37.253','139.159.205.46','139.159.205.97','139.9.67.110','139.9.210.223','139.9.210.205','139.9.214.110','139.9.75.132','139.9.42.131','139.9.197.7','139.9.198.151','139.9.200.15','139.9.70.130','139.9.197.37','139.9.47.13','139.9.215.163','139.9.213.96','139.9.212.186','139.9.34.180','139.9.208.216','139.159.206.84','139.9.193.246','139.9.54.119','139.9.199.35','139.159.196.77','139.9.90.243','139.9.208.243','139.9.215.7','139.9.76.53','139.9.45.190','139.9.34.233','139.159.193.28','139.9.79.250','139.159.202.129','139.9.86.26','139.9.88.95','139.9.47.214','139.9.199.87','139.9.58.7','139.9.204.36','139.9.207.63','139.159.203.218','139.9.203.111','139.9.212.195','139.9.55.242','139.9.45.3','139.9.201.198','139.9.46.42','139.9.33.203','139.9.209.22','139.9.205.153','139.9.68.113','139.9.213.19','139.9.194.26','139.9.215.159','139.9.200.123','139.9.91.59','139.9.43.233','139.9.49.42','139.159.205.28','139.9.204.57','139.9.42.132','139.159.192.117','139.9.54.208','139.9.43.136','139.9.37.152','139.9.195.121','139.9.212.250','139.9.43.88','139.159.204.158','139.159.194.116','139.9.201.187','139.9.214.86','139.9.40.86','139.9.51.159','139.9.194.145','139.9.67.161','139.9.73.215','139.159.205.241','139.9.195.64','139.159.199.178','139.9.195.52','139.159.163.221','139.9.75.66','139.159.193.105','139.9.194.66','139.9.199.66','139.9.199.238','139.9.215.244','139.159.192.234','119.3.169.94','119.3.171.120','119.3.186.194','119.3.176.29','119.3.182.242','119.3.165.93','119.3.185.109','119.3.187.108','119.3.183.199','119.3.165.89','119.3.191.149','119.3.173.44','119.3.187.26','119.3.164.242','119.3.187.119','119.3.165.183','119.3.177.82','119.3.187.205','119.3.162.43','119.3.178.1','119.3.178.195','119.3.187.40','119.3.191.250','119.3.185.94','119.3.173.70','119.3.171.232','119.3.164.102','119.3.175.25','119.3.164.87','119.3.178.230','119.3.163.168','119.3.171.72','119.3.168.78','119.3.190.28','119.3.167.47','119.3.168.81','119.3.185.114','119.3.172.19','119.3.170.105','119.3.168.155','119.3.173.217','119.3.183.241','119.3.190.120','119.3.165.150','119.3.189.193','119.3.169.225','119.3.186.235','119.3.191.17','119.3.187.79','119.3.191.30','119.3.163.183','119.3.179.219','119.3.187.147','119.3.171.55','119.3.164.33','119.3.189.154','119.3.184.191','119.3.181.204','119.3.161.23','119.3.162.146','119.3.173.95','119.3.189.227','119.3.172.45','119.3.169.108','119.3.184.61','119.3.170.9','119.3.185.60','119.3.181.59','119.3.180.76','119.3.186.24','119.3.161.200','119.3.181.168','119.3.180.177','119.3.183.57','119.3.188.52','119.3.190.108','119.3.162.8','119.3.165.177','119.3.189.46','119.3.172.110','119.3.173.205','119.3.162.97','119.3.170.62','119.3.178.34','119.3.172.21','119.3.162.25','119.3.164.155','119.3.167.67','119.3.174.138','119.3.173.143','119.3.176.206','119.3.168.149','119.3.173.252','119.3.180.237','119.3.185.238'

foreach ($ip in $ips)
{
    Invoke-Command -ComputerName $ip -FilePath .\cc.ps1 -credential $Cred
}

目前网上关于pwershell 的资料很少,自己尝试通过powershell 远程连接指定IP的服务器(Windows2012)执行psl 文件,此文件内部布置好了解压文件等系列操作

psl文件如下:

Function Unzip-File()
{
    param([string]$ZipFile,[string]$TargetFolder)
    #确保目标文件夹必须存在
    if(!(Test-Path $TargetFolder))
    {
        mkdir $TargetFolder
    }
    $shellApp = New-Object -ComObject Shell.Application
    $files = $shellApp.NameSpace($ZipFile).Items()
    $shellApp.NameSpace($TargetFolder).CopyHere($files)
}

$wc = New-Object System.Net.WebClient
$wc.DownloadFile('http://159.138.60.15/python-3.7.3-embed-amd64.zip', 'C:\python.zip')

#将zip文件E:\a.zip解压到e:\test,目录
Unzip-File -ZipFile C:\python.zip -TargetFolder 'C:\Program Files\python'

$wc.DownloadFile('http://159.138.60.15/CloudControlPython.zip', 'C:\CloudControlPython.zip')
Unzip-File -ZipFile C:\CloudControlPython.zip -TargetFolder c:\CloudControlPython

Remove-Item C:\CloudControlPython.zip
Remove-Item C:\python.zip
 
python c:\CloudControlPython\processes.py

实现了远程控制服务器自动给下载python安装包,自动解压安装的自动化操作。

注意: 在powershell 进行远程操作时,需要开启客户端和服务端相关权限,以及相应的安全配置,具体方法可以百度,很详细。

posted @ 2019-07-12 17:22  风中的沙~~  阅读(949)  评论(0编辑  收藏  举报