2016-12-06 9 views
0

私がここでやろうとしているのは、csv入力(コンピュータ、ユーザ)に基づいてローカルの管理者権限を与えて削除することです。私たちのユーザのコンピュータはかなりオフラインになりがちで、さまざまなタイムゾーンが出てくるので、私はスクリプトが権限を与えるために再試行してほしい。powershell - エラーメッセージに基づいて行を削除します

キャッチは、スクリプトによって1つのアクセス権が追加/削除されたが、別のアクセス権がコンピュータに到達できないため(アクセスが拒否されているなど)、この時点ではなかった場合、 1時間後に再試行する情報を送信できませんでした "が表示されます。

は、エラーメッセージ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) 

答えて

0

このようにするには、失敗したすべての試行を含むオブジェクトを保持し、そのオブジェクトを使用して既存のファイルを上書きする必要があります。これはまた、あなたの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) 
+0

こんにちはマイク、 お返事ありがとうございました。悲しいことに、それはうまく動作しませんでした。 csvが正しく追加されたコンピュータ、ユーザを追加し、その行をCSVから削除しました。 私はまだCSVを満たしていないという問題に直面しています。それはcsvを無効にしますが、コンテンツを入れません。 これを解決する方法についてご提案がありますか? ありがとうございました! –

関連する問題