【发布时间】:2016-12-06 18:04:24
【问题描述】:
我在这里要做的是根据 csv 输入(计算机、用户)授予和删除本地管理权限。由于我们的用户计算机经常处于离线状态,并且会出现许多不同的时区,因此我希望脚本重试授予权限,它确实如此。
问题是,如果脚本添加/删除了 1 个权限,但此时还没有另一个权限(由于计算机不可访问、访问被拒绝或类似情况),脚本将返回“可能not send Information retrying in 1 hours..." 尽管它可以成功地为列出的一个或多个条目删除/添加它。
所以我想做的是一旦powershell 返回错误消息 0(成功),我想从 CSV 中删除当前工作的 csv 行,因此 CSV 实际上“工作”计算机应该这样做。
谁能帮我解决这个问题?到目前为止,我还没有找到类似这个问题的东西。
$Stoploop = $false
[int]$Retrycount = "0"
do {
try {
Import-Csv "E:\Folder\Script\Remove-Admin-1.csv" | foreach {
$DomainName = "domain.local"
$ComputerName = $($_.Computer)
$UserName = $($_.User)
$AdminGroup = [ADSI]"WinNT://$ComputerName/Administrators,group"
$User = [ADSI]"WinNT://$DomainName/$UserName,user"
$AdminGroup.Add($User.Path)
}
Write-Host "Job completed"
$Stoploop = $true
}
catch {
if ($Retrycount -gt 24){
Write-Host "Could not send Information after 24 retrys."
$Stoploop = $true
}
else {
Write-Host "Could not send Information retrying in 1 hours..."
Write-Host $(Get-Date)
Start-Sleep -Seconds 3600
$Retrycount = $Retrycount + 1
}
}
}
While ($Stoploop -eq $false)
【问题讨论】:
标签: powershell powershell import-csv