即使删除了用户配置文件,本地用户配置文件文件夹路径的测试路径也会返回true。并且没有用户文件夹

时间:2018-04-16 16:58:21

标签: windows powershell automation

删除用户个人资料     $ UserFolderPath1 =“\ $ Server \ C $ \ Users \ $ UserName”

Try {
         (Get-WmiObject -ComputerName $Server Win32_UserProfile | Where-Object {$_.LocalPath -eq "C:\Users\$UserName"}).Delete()
         Write-Host -ForegroundColor Green "$UserName has been deleted from $Server"

       } Catch [System.Management.Automation.MethodInvocationException]{
         Write-Host -ForegroundColor Red "ERROR: Profile is currently locked on $Server "

       } Catch [System.Management.Automation.RuntimeException] {
         Write-Host -ForegroundColor Yellow -BackgroundColor Blue "INFO: $UserName Profile does not exist on $host"

       } Catch {
         Write-Host -ForegroundColor Red "ERROR: an unknown error occoured. The error response was $($error[0])"

       }

用户文件夹已删除,但测试路径c:\ users \ userprofilefolder仍显示为true

    if(test-path "$UserFolderPath1"){

       Write-host "user folder found"
       Get-ChildItem $UserFolderPath1 -Recurse | Remove-Item -recurse -Force  
       Remove-Item $UserFolderPath1 -Force   

    }

0 个答案:

没有答案