我有一个脚本使用一个txt文件与约80个目标的脚本。我使用ForEach循环来循环遍历目标。我想做的是找出一种方法来修改源文件,删除成功的计算机的名称,留下失败的计算机。这是迄今为止的脚本:
$computers = Get-Content -Path C:\Temp\Computers.txt
$Credentials = Get-Credential
$Credentials.Password | ConvertFrom-SecureString | Set-Content C:\temp\password.txt
$Username = $Credentials.Username
$Password = Get-Content “C:\temp\password.txt” | ConvertTo-SecureString
$Credentials = New-Object System.Management.Automation.PSCredential $Username,$Password
$file1 = "C:\Temp\installer.msi"
$ErrorActionPreference = 'Continue'
ForEach($computer in $computers){
# See if Computer is online before proceeding
IF (Test-Connection -BufferSize 32 -Count 1 -ComputerName $computer) {
$b = New-PSSession -computername $computer -Credential $Credentials
# Create c:\Temp if it doesn't already exist
Invoke-Command -Session $b -ScriptBlock {
New-Item -ItemType Directory -Path C:\Temp -Force
}
#Copy Installer to Local Temp Directory
Copy-Item -path $file1 -Destination "C:\Temp\" -ToSession $b
# Install msi
Invoke-Command -Session $b -ScriptBlock {
$Arguments = "/i", "`"C:\Temp\installer.msi`"", "/qn", "/norestart", "ALLUSERS=1", "/L*v", "`"c:\temp\msisetup.log`""
$msiProcess = Start-Process msiexec.exe -Wait -ArgumentList $Arguments -PassThru
# Check if installation was successful
# 0 = ERROR_SUCCESS, 3010 = ERROR_SUCCESS_REBOOT_REQUIRED
if( $msiProcess.ExitCode -in 0, 3010 ) {
Write-Host "Installation succeeded with exit code $($msiProcess.ExitCode)"
}
}
# Wait for Install to finish completely
$Seconds = 60
$EndTime = [datetime]::UtcNow.AddSeconds($Seconds)
while (($TimeRemaining = ($EndTime - [datetime]::UtcNow)) -gt 0) {
Write-Progress -Activity 'Watiting for Sleep to Finish' -SecondsRemaining $TimeRemaining.TotalSeconds
Start-Sleep 1
}
# Cleanup
Invoke-Command -Session $b -ScriptBlock {
Remove-Item -Path C:\Temp\installer.msi
}
$List = "C:\Temp\computers.txt"
$string = "$computer"
$ListContent = Get-Content $List
$measureureObject = $string | Measure-Object -Character
$index = $ListContent.IndexOf("$computer")
$length = $measureureObject.Characters
$ListContent.Remove($index,$length) | Set-Content $List
} Else {
Continue
}
}
当到达$ListContent.Remove行时,靠近末尾的部分给出以下错误:
找不到“Remove”和参数计数的重载:“2”.第7行字符:1
- $ListContent.Remove($index,$length)|设置内容$列表
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
先谢谢你了!
1条答案
按热度按时间axr492tv1#
我找到了更好的办法。我只是把它添加到If/Else的Else末尾