【问题标题】:powershell - removes lines based on error messagepowershell - 根据错误消息删除行
【发布时间】:2016-12-06 18:04:24
【问题描述】:

我在这里要做的是根据 csv 输入(计算机、用户)授予和删除本地管理权限。由于我们的用户计算机经常处于离线状态,并且会出现许多不同的时区,因此我希望脚本重试授予权限,它确实如此。

问题是,如果脚本添加/删除了 1 个权限,但此时还没有另一个权限(由于计算机不可访问、访问被拒绝或类似情况),脚本将返回“可能not send Information retrying in 1 hours..." 尽管它可以成功地为列出的一个或多个条目删除/添加它。

所以我想做的是一旦 返回错误消息 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


    【解决方案1】:

    因此,为了做到这一点,您需要保留一个包含所有失败尝试的对象,然后使用该对象覆盖现有文件。这也意味着您的 try/catch 需要在您的 foreach 中,以便可以独立处理每台计算机。

    $Stoploop = $false
    [int]$Retrycount = "0"
    $fails = @()
    
    do {
    
    Import-Csv "E:\Folder\Script\Remove-Admin-1.csv" | foreach {
        Try{
            $DomainName = "domain.local"
            $ComputerName = $($_.Computer)
            $UserName = $($_.User)
            $AdminGroup = [ADSI]"WinNT://$ComputerName/Administrators,group"
            $User = [ADSI]"WinNT://$DomainName/$UserName,user"
            $AdminGroup.Add($User.Path)
        } catch {
            $fails += $_
            if ($Retrycount -gt 24){
                Write-Host "Could not send Information after 24 retrys."
                $fails | export-csv "E:\Folder\Script\Remove-Admin-1.csv" -NoTypeInformation
                $Stoploop = $true
            }
            else {
                Write-Host "Could not send Information retrying in 1 hours..."
                Write-Host $(Get-Date)
                Start-Sleep -Seconds 3600
                $Retrycount = $Retrycount + 1
                $fails += $_
            }
        }
    }
    if($fails = 0){
        Write-Host "Job completed"
        $Stoploop = $true
    } elseif ($Retrycount -gt 24){
        Write-Host "Could not send Information after 24 retrys."
        $fails | export-csv "E:\Folder\Script\Remove-Admin-1.csv" -NoTypeInformation -force
        $Stoploop = $true
    } else {
        Write-Host "Could not send Information retrying in 1 hours..."
        Write-Host $(Get-Date)
        Start-Sleep -Seconds 3600
        $Retrycount = $Retrycount + 1
        $fails | export-csv "E:\Folder\Script\Remove-Admin-1.csv" -NoTypeInformation -force
    }
    
    
    }
    While ($Stoploop -eq $false)
    

    【讨论】:

    • 嗨,迈克,感谢您的快速回复,遗憾的是它并没有完全发挥作用。为了正确填充 csv,添加了 -header Computer,User 并从 CSV 中删除了该行。我仍然面临的问题是它没有填充 CSV。它会覆盖 csv 但不会将内容放入其中。你对如何解决这个问题有什么建议吗?谢谢!
    猜你喜欢
    • 2012-08-19
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2011-05-26
    • 2016-11-17
    • 1970-01-01
    相关资源
    最近更新 更多