2017-04-18 448 views
1

以下のスクリプトがあります。私がコメントを解除した場合のラインが#3をコメントし、私は関係なく、私が#1#2形式を使用してWMIに問い合わせるかどうかのエラー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 

を取得します。私はコメントして、コメントを外し#4#3を残す場合は、私は関係なく、Get-WMIObjectのためのクエリ文字列のエラー

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 

を取得します。

私がウェブ上で見つけることができるもの - 他にもいくつかの質問があります - これらの方法のいずれかがうまくいくはずですが、どちらもそうではないようです。ターゲットプロファイルが読み込まれているかどうかを確認しましたが、そうではありません。 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

これは、意味のないエラーメッセージが単純な解決策を発見する方法の良い例です。別名として、同じ名前のPSの自動変数と衝突するので、変数名 '$ profile'を使用しない方が良いでしょう。 – mklement0

+1

変数名をうまく捕まえることができます。それに応じてスクリプトを修正して、技術サポートユニットの残りの部分に配布します。そして、はい、これは、エラーメッセージが「積極的に反支援的」に近づいていた場所の1つです... –

答えて

1

編集: あなたが管理者としてスクリプトを実行する必要があります。

関連する問題