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

Posted CIAS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PowerShell 批量部署windows_exporter到所有Windows主机相关的知识,希望对你有一定的参考价值。

  

前提条件 参考

批量拷贝脚本到远程主机

  • $local_path="D:\\PowerShell\\Powershell-Windows_Admin_Center-install\\" #本地脚本存放目录
  • $Destination="d:\\" #本地拷贝的脚本到目标主机的目录
  • Invoke-Command -filepath D:\\powershell-install-windows_exporter-0.20.ps1 -ComputerName $server -Credential $cred #此处是直接执行本地代码生效到目标主机,此处我注释了,需要此案例的可以启用注释掉下方invoke-expression段
  • invoke-expression -command D:\\powershell-install-windows_exporter-0.20.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.19.11' #主机IP,有多少填写多少,注意格式
  • $port=5985 #winrm http服务端口
powershell-Remote-Software-Installation.ps1
<# Powershell Remote Software Installation
+++++++++++++++++++++++++++++++++++++++++++++++++++++
+  _____                       _____ _          _ _ +
+ |  __ \\                     / ____| |        | | |+
+ | |__) |____      _____ _ _| (___ | |__   ___| | |+
+ |  ___/ _ \\ \\ /\\ / / _ \\ '__\\___ \\| '_ \\ / _ \\ | |+
+ | |  | (_) \\ V  V /  __/ |  ____) | | | |  __/ | |+
+ |_|   \\___/ \\_/\\_/ \\___|_| |_____/|_| |_|\\___|_|_|+
+ +++++++++++++++++++++++++++++++++++++++++++++++++++
                                                                                                              
# Remote Software Installation
# .\\powershell-Remote-Software-Installation.ps1
#> 
 
$user="administrator"
$password=ConvertTo-SecureString "YOU_host_password" -AsPlainText -Force
$cred=New-Object System.Management.Automation.PSCredential($user,$password)
$pchost='192.168.19.10'
$port=5985
 
$local_path="D:\\powershell-install-windows_exporter-0.20.ps1"
$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 "Execute local script to take effect on remote server $pchost" -ForegroundColor Green
#Invoke-Command -filepath D:\\powershell-install-windows_exporter-0.20.ps1 -ComputerName $server -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 "Execute the script directly on the server directory $pchost" -ForegroundColor Green
Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock 
invoke-expression -command D:\\powershell-install-windows_exporter-0.20.ps1
 

创建安装windows_exporter自动化脚本

powershell-install-windows_exporter-0.20.ps1
<# Powershell Install windows_exporter-0.20
+++++++++++++++++++++++++++++++++++++++++++++++++++++
+  _____                       _____ _          _ _ +
+ |  __ \\                     / ____| |        | | |+
+ | |__) |____      _____ _ _| (___ | |__   ___| | |+
+ |  ___/ _ \\ \\ /\\ / / _ \\ '__\\___ \\| '_ \\ / _ \\ | |+
+ | |  | (_) \\ V  V /  __/ |  ____) | | | |  __/ | |+
+ |_|   \\___/ \\_/\\_/ \\___|_| |_____/|_| |_|\\___|_|_|+
+ +++++++++++++++++++++++++++++++++++++++++++++++++++
                                                                                                              
# Powershell Install windows_exporter-0.20
# .\\powershell-install-windows_exporter-0.20.ps1
#> 

#windows_exporter-0.20
$windows_exporter = "windows_exporter-0.20.0-amd64.msi"
$url = "https://github.com/prometheus-community/windows_exporter/releases/download/v0.20.0/"
$drive = "D:\\"

Write-Host "download windows_exporter-0.20 up D dish" -ForegroundColor Green
wget -Uri $url/$windows_exporter -UseBasicParsing -OutFile $drive$windows_exporter

Write-Host "Install windows_exporter" -ForegroundColor Green 
Start-Process "D:\\$windows_exporter" -ArgumentList "-qn"  -wait

Write-Host "Delete the downloaded file" -ForegroundColor Green
rm $drive$windows_exporter 

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

执行一键批量安装到所有主机

.\\powershell-Remote-Software-Installation.ps1

以上是关于PowerShell 批量部署windows_exporter到所有Windows主机的主要内容,如果未能解决你的问题,请参考以下文章

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

PowerShell 初始化环境 用于批量部署

PowerShell 初始化环境 用于批量部署

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

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

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