2017-04-18 89 views
1

我有以下腳本。如果我取消註釋行註釋#3,我得到的錯誤如何使用PowerShell刪除用戶配置文件

Exception calling "Delete" with "0" argument(s): "" 
At Z:\Scripts\Powershell\Remove-UserProfile.ps1:48 char:21 
+      $Profile.Delete() 
+      ~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : DotNetMethodException 

不管WMI使用格式#1#2我是否審問。如果我離開#3評論,並取消#4,我得到的錯誤

Remove-WmiObject : 
At Z:\Scripts\Powershell\Remove-UserProfile.ps1:49 char:21 
+      Remove-WmiObject -InputObject $Profile 
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [Remove-WmiObject], COMException 
    + FullyQualifiedErrorId : RemoveWMICOMException,Microsoft.PowerShell.Commands.RemoveWmiObject 

不管查詢字符串Get-WMIObject的。

我在網上可以找到的所有東西 - 包括其他一些SO問題 - 意味着這兩種方法都可以工作,但似乎都不行。我已檢查目標配置文件是否已加載,但不是。爲什麼我不能使用WMI來刪除用戶配置文件?我能做些什麼確實工作,並且不涉及從第三方下載實用程序(這是我們的「信息安全」團隊所不允許的)?

腳本:

function Remove-UserProfile { 
<# 
.SYNOPSIS 
Removes user profiles from computers 
#> 

    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="High")] 

    param(
     [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] 
     [String[]]$ComputerName = $env:ComputerName, 

     [Alias("UserName","sAMAccountName")] 
     [String]$Identity, 

     [Int]$Age, 

     [Switch]$DomainOnly 
    ) 

    BEGIN { 
     $NoSystemAccounts = "SID!='S-1-5-18' AND SID!='S-1-5-19' AND SID!='S-1-5-20' " # Don't even bother with the system accounts. 
     if ($DomainOnly) { 
      $SIDQuery = "SID LIKE '$((Get-ADDomain).DomainSID)%' "      # All domain account SIDs begin with the domain SID 
     } elseif ($Identity.Length -ne 0) { 
      $SIDQuery = "SID LIKE '$(Get-UserSID -AccountName $Identity)' " 
     } 
     $CutoffDate = (Get-Date).AddDays(-$Age) 
     $Query = "SELECT * FROM Win32_UserProfile " 
    } 

    PROCESS{ 
     ForEach ($Computer in $ComputerName) { 
      Write-Verbose "Processing Computer $Computer..." 
      if ($SIDQuery) { 
       $Query += "WHERE " + $SIDQuery 
       $FilterStr = $SIDQuery 
      } else { 
       $Query += "WHERE " + $NoSystemAccounts 
       $FilterStr = $NoSystemAccounts 
      } 
      Write-Verbose "Querying WMI using '$Query' and filtering for profiles last used before $CutoffDate ..." 
#1   $Profiles = Get-WMIObject -Query $Query | Where-Object { [Management.ManagementDateTimeConverter]::ToDateTime($_.LastUseTime) -lt $CutoffDate } 
#2   $Profiles = Get-WMIObject -ComputerName $Computer -Class Win32_UserProfile -Filter $FilterStr | Where-Object { [Management.ManagementDateTimeConverter]::ToDateTime($_.LastUseTime) -lt $CutoffDate } 
      ForEach ($Profile in $Profiles) { 
       if ($PSCmdlet.ShouldProcess($Profile)) { 
#3     $Profile.Delete() 
#4     Remove-WmiObject -InputObject $Profile 
       } 
      } 
     } 
    } 

    END {} 
} 
+0

這是一個很好的例子,說明無意義的錯誤消息如何獲得一個簡單的解決方案。另外:最好不要使用變量名'$ profile',因爲它會與PS的同名自動變量衝突。 – mklement0

+1

變量名稱的好處;在提供給我們的技術支持部門的其他人員之前,我會相應地修改腳本。是的,這是錯誤信息接近「積極反對」的地方之一...... –

回答

相關問題