Powershell删除配置文件脚本 - 错误检查无效

时间:2016-08-16 19:45:26

标签: windows powershell scripting wmi

我有这个删除配置文件脚本,提示输入用户名并从列出的每台计算机中删除它。删除配置文件和"用户已登录"部件都工作,但部分说“没有在名为$ UserName的计算机上找到的配置文件”不是。我在两台计算机上运行我的脚本,并成功删除了我的配置文件。我重新创建了我的个人资料(已登录)并保持登录到一个而不是另一个。我再次运行它,它给了我消息"用户已登录"。对于其他计算机,它刚刚删除了配置文件,但未显示"未找到配置文件"信息。它只是跳过它并且什么也没显示。我已经改变了" if"到了"否则"但是,当我这样做时,它会显示多行"没有找到任何配置文件"包括之前删除了个人资料的计算机。

以下是大部分脚本派生自的链接。 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"            
 }            
}

2 个答案:

答案 0 :(得分:1)

PowerShell有一些automatic variables,你应该避免重复使用。

$Profile就是其中之一,它包含适用于当前会话的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"
    }
   }
  }

答案 1 :(得分:1)

通过更改" $ profilefound = $ false"我能够让它工作。并使其成为一个全局变量。当我将其更改为else语句时,显示多行"配置文件的原因还在于它放置的位置。它正在检查服务器上的每个配置文件。当它触及计算机上的每个配置文件时,它会显示"未找到配置文件"。

这是工作脚本。

# Tells Apache to identify which site by name
NameVirtualHost *:80
# Tells Apache to serve the default WAMP Server page to "localhost"
<VirtualHost 127.0.0.1>
    ServerName localhost
    DocumentRoot "C:/wamp/www"
</VirtualHost> 
# Tells Apache to serve your mobile pages to "m.localhost"
<VirtualHost 127.0.0.1>
# The name to respond to ServerName m.localhost
# Folder where the file is located which in your case is
    DocumentRoot "C:/wamp/www/mobile/"
<Directory "C:/wamp/www/mobile/">
    Allow from all
    Order Allow,Deny
    AllowOverride All
</Directory>
# Apache will look for these files, in this order, if no file is specified in the URL, but you can add more files apart from the two I listed depending on what you are having
    DirectoryIndex index.html index.php
</VirtualHost> 
#Here you duplicate the code for your mobile site to also accept your IP address which is 192.168.56.1
<VirtualHost 192.168.56.1>
# The name to respond to ServerName m.localhost
# Folder where the file is located which in your case is
    DocumentRoot "C:/wamp/www/mobile/"
<Directory "C:/wamp/www/mobile/">
    Allow from all
    Order Allow,Deny
    AllowOverride All
</Directory>
# Apache will look for these files, in this order, if no file is specified in the URL, but you can add more files apart from the two I listed depending on what you are having
    DirectoryIndex index.html index.php
</VirtualHost>