2016-08-16 1 views
5

ユーザー名の入力を求めるプロンプトが表示され、リストされている各コンピュータから削除されるこのプロファイルスクリプトがあります。プロファイルの削除と「ユーザーがログインしています」の両方のパートが動作していますが、「名前$ UserNameの$ Computerでプロファイルが見つかりません」という部分はありません。私は2台のコンピュータでスクリプトを実行し、両方のプロファイルを正常に削除しました。私は自分のプロフィールを再作成し(ログインした)、もう一方にはログインしていない状態でログインしていました。私は再びそれを実行し、それは私に "ユーザーがログインしている"というメッセージを与えます。他のコンピュータの場合、プロファイルを削除しただけで、「プロファイルが見つかりません」というメッセージは表示されません。それをスキップして何も表示しません。私は "if"を "else"に変更しましたが、私がそうすると、以前にプロファイルを削除したコンピュータを含めて、 "プロファイルが見つかりません"という複数の行が表示されます。Powershellプロファイルスクリプトを削除 - エラーチェックが機能しない

ここには、ほとんどのスクリプトが由来するリンクがあります。 http://techibee.com/powershell/powershell-script-to-delete-windows-user-profiles-on-windows-7windows-2008-r2/1556。コメントを見てみると、他の誰もその部分に問題がないように見えました。

私はPowerShellについて多くの知識を持っていませんが、これは私のニーズに基づいて見つかった他のスクリプトからまとめたものです。私たちの環境はWindows 7とServer 2008 R2です。どんな支援も大歓迎です。

$UserName=Read-host "Please Enter Username: " 

$ComputerName= @("computer1","computer2") 


foreach($Computer in $ComputerName) {    
Write-Verbose "Working on $Computer"    
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {    
    $Profiles = Get-WmiObject -Class Win32_UserProfile -Computer $Computer -ea 0    

    foreach ($profile in $profiles) {    
    $objSID = New-Object System.Security.Principal.SecurityIdentifier($profile.sid)    
    $objuser = $objsid.Translate([System.Security.Principal.NTAccount])    
    $profilename = $objuser.value.split("\")[1]    

    if($profilename -eq $UserName) {    
    $profilefound = $true    

    try {    
    $profile.delete()    
    Write-Host -ForegroundColor Green "$UserName profile deleted successfully on $Computer"    
    } catch {    
    Write-Host -ForegroundColor Yellow "Failed to delete the profile, $UserName logged on to $Computer"    
    }    
    }    
    }    

    if(!$profilefound) {    
    Write-Host -ForegroundColor Cyan "No profiles found on $Computer with Name $UserName"    
    }    
} else {    
    write-verbose "$Computer Not reachable"    
}    
} 
+0

'$ Profile'は、foreachループ(例えば、別の変数名を使用して、自動変数である。' foreachの($は$プロファイルでpsprofile ){} ') –

+0

私はそれを知りませんでした。私はそれを試みましたが、もう出力を出すことはありません。私はいずれかのコンピュータにログインしています。ユーザーがログインしています。 " – DavidG

+0

どんな場合でも 'Win32_UserProfile'には' delete() 'メソッドがありません –

答えて

1

PowerShellの番号はautomatic variablesです。再使用しないでください。

$Profileは、現在のセッションに適用可能なプロファイルスクリプトへのパスを含んでいます。

他の変数名を使用します(つまり、$userprofileを。)、あなたは大丈夫:

foreach ($userprofile in $profiles) {    
    $objSID = New-Object System.Security.Principal.SecurityIdentifier($userprofile.sid)    
    $objuser = $objsid.Translate([System.Security.Principal.NTAccount])    
    $profilename = $objuser.value.split("\")[1]    

    if($profilename -eq $UserName) { 
    $profilefound = $true 

    try { 
    $userprofile.delete() 
    Write-Host -ForegroundColor Green "$UserName profile deleted successfully on $Computer" 
    } catch { 
    Write-Host -ForegroundColor Yellow "Failed to delete the profile, $UserName logged on to $Computer" 
    } 
    } 
    } 
0

私はそれが「$ profilefound = $ false」に変更し、それをグローバルにすることによって動作させることができました変数。また、「複数の行を表示していたのは、なぜelse文に変更したときに見つからなかったのか」というのは、サーバ上のすべてのプロファイルをチェックしていたからです。 "が見つかりませんプロファイル"。

はここで働いスクリプトです。

$UserName=Read-host "Please Enter Username: " 

$ComputerName= @("computer1","computer2") 
$profilefound = "false" 

foreach($Computer in $ComputerName) {    
Write-Verbose "Working on $Computer"    
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {    
    $Profiles = Get-WmiObject -Class Win32_UserProfile -Computer $Computer -ea 0    

    foreach($userprofile in $profiles){    
    $objSID = New-Object System.Security.Principal.SecurityIdentifier($userprofile.sid)    
    $objuser = $objsid.Translate([System.Security.Principal.NTAccount])    
    $profilename = $objuser.value.split("\")[1]   

    if($profilename -eq $UserName) {    
    $profilefound = "true"   

    try {    
    $userprofile.delete()    
    Write-Host -ForegroundColor Green "$UserName profile deleted successfully on $Computer"    
    } catch {    
    Write-Host -ForegroundColor Yellow "Failed to delete the profile, $UserName logged on to $Computer"    
    }    
    }   
    } 

} 
else {    
    write-verbose "$Computer Not reachable"    
} 
if ($profilefound -eq "false") {    
    Write-Host -ForegroundColor Cyan "No profiles found on $Computer with Name $UserName"    
    }   
} 
関連する問題