读取CSV文件
$Header =‘MachineName’,‘Username’,‘Password’,‘IPAddress’
$data = Import-Csv -Path “C:\filetest.csv” -Header $Header
循环遍历CSV文件中的每一行
foreach($row in $data) {
# 提取机器名、用户名、密码、IP地址
$machineName = $row.MachineName
$username = $row.Username
$password = $row.Password
$ipAddress = $row.IPAddress
# 创建远程PSSession
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($username, (ConvertTo-SecureString -String $password -AsPlainText -Force))
$session = New-PSSession -ComputerName $ipAddress -Credential $credentials
# 安装软件
$softwarePath = '\\10.167.19.23\soft'
Invoke-Command -Session $session -ScriptBlock {
param($path)
if (!(Test-Path -Path "$path\software1.exe")) {
Start-Process -FilePath "$path\software1.exe" -ArgumentList "/silent" -Wait
} else {
Write-Host "Software1 already installed on $env:COMPUTERNAME"
}
if (!(Test-Path -Path "$path\software2.exe")) {
Start-Process -FilePath "$path\software2.exe" -ArgumentList "/silent" -Wait
} else {
Write-Host "Software2 already installed on $env:COMPUTERNAME"
}
if (!(Test-Path -Path "$path\software3.exe")) {
Start-Process -FilePath "$path\software3.exe" -ArgumentList "/silent" -Wait
} else {
Write-Host "Software3 already installed on $env:COMPUTERNAME"
}
} -ArgumentList $softwarePath
# 关闭远程PSSession
Remove-PSSession -Session $session
}