2016-08-16 32 views
5

Tôi có tập lệnh cấu hình xóa này sẽ nhắc tên người dùng và xóa nó khỏi từng máy tính được liệt kê. Các cấu hình xóa và "người dùng được đăng nhập" cả hai đều làm việc nhưng phần nói rằng "Không tìm thấy hồ sơ trên máy tính $ với Tên $ UserName" thì không. Tôi chạy kịch bản của tôi trên hai máy tính và nó đã xóa thành công hồ sơ của tôi trên cả hai. Tôi tạo lại hồ sơ của tôi (đăng nhập) và ở lại đăng nhập vào một và không phải là khác. Tôi chạy nó một lần nữa và nó mang lại cho tôi thông báo "người dùng đã đăng nhập". Đối với máy tính khác, nó chỉ xóa hồ sơ trên không hiển thị thông báo "không tìm thấy hồ sơ". Nó chỉ bỏ qua nó và không hiển thị gì cả. Tôi đã thay đổi "nếu" thành một "khác" nhưng, khi tôi làm điều đó nó sẽ hiển thị nhiều dòng "không tìm thấy hồ sơ" bao gồm cả máy tính trước đó nó đã xóa hồ sơ trên.Powershell Xóa tập lệnh tiểu sử - kiểm tra lỗi không hoạt động

Đây là liên kết nơi hầu hết tập lệnh được lấy từ đó. http://techibee.com/powershell/powershell-script-to-delete-windows-user-profiles-on-windows-7windows-2008-r2/1556. Nhìn qua các bình luận, không ai khác dường như có bất kỳ vấn đề với phần đó của nó.

Tôi không có nhiều kiến ​​thức trong PowerShell và điều này vừa được ghép lại với nhau từ các tập lệnh khác mà tôi đã tìm thấy dựa trên nhu cầu của chúng tôi. Môi trường của chúng tôi là Windows 7 và Server 2008 R2. Bất kỳ trợ giúp được đánh giá rất cao.

$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' là một biến số tự động, sử dụng một tên biến trong vòng lặp foreach (ví dụ.' foreach ($ psprofile trong $ hồ sơ) {} ') –

+0

Cảm ơn vì đề nghị này. Tôi không biết điều đó. Tôi đã thử nó và nó không còn cho đầu ra nữa. Tôi đã đăng nhập vào một trong các máy tính và nó sẽ đưa ra một kết quả nói rằng" người dùng đã đăng nhập ". – DavidG

+0

Nó sẽ không hoạt động trong mọi trường hợp, 'Win32_UserProfile' không có phương thức' delete() ' –

Trả lời

1

PowerShell có số automatic variables mà bạn nên tránh sử dụng lại.

$Profile là một trong số này, nó chứa các đường dẫn đến các kịch bản Hồ sơ áp dụng cho phiên hiện tại.

Sử dụng bất kỳ tên biến khác (ví dụ $userprofile.) Và bạn sẽ ổn thôi:

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

tôi đã có thể để có được nó làm việc bằng cách thay đổi "$ profilefound = $ false" và làm cho nó một toàn cầu biến. Cũng là lý do tại sao nó được hiển thị nhiều dòng "hồ sơ không tìm thấy khi tôi thay đổi nó để một tuyên bố khác là vì nơi nó được đặt. Nó đã được kiểm tra đối với mọi hồ sơ trên máy chủ. "Hồ sơ không tìm thấy".

đây là kịch bản làm việc.

$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"    
    }   
} 
Các vấn đề liên quan