2017-07-18 78 views
1

我試圖創建一個腳本來檢測長達15天的域用戶配置文件並刪除它們。這將捆綁在一個工作中,在運行之前將會註銷任何閒置會話。腳本沒有正確地將WMI對象分配給陣列

我知道這通常是通過GPO完成的,但是由於各種原因它不適合用於這個特定業務領域。

這裏是我的代碼:

#Assign how old the user profile must be for deletion 
[int]$NoOfDays = 15 

#Get WMI object where it is a domain account and over 15 days old 
$Objects = @(Get-WmiObject -Class Win32_UserProfile | Where { 
    (!$_.Special) -and 
    $_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-15) 
}) 

if ($Objects -eq $null) { 
    #If no users returned, write to host. 
    Write-Host "no profiles found for deletion" 
} else { 
    #If users are found, do the following 
    foreach ($Object in $Objects) { 
     Write-Host "'$($object.LocalPath)' has been identified for deletion" 
    } 

    foreach ($Object in $Objects) { 
     try { 
      Write-Host "Attempting to delete profile '$($Object.LocalPath)'" 
      Remove-WmiObject 
      Write-Host "Deleted profile '$($Object.LocalPath)' successfully." 
     } catch { 
      Write-Host "Unable to delete profile '$($Object.LocalPath)'" -ErrorAction Continue 
     } 
    } 
} 

沒有輸出,它只是返回命令沒有錯誤行,立竿見影。

我試圖註釋掉刪除配置文件的位,在每個語句的第一個語句之後結束else,看看刪除了哪些內容,但它沒有起作用。

+0

它似乎對刪除-WmiObject可以cmdlet的一個部分的輸入參數在上面的代碼中給定的被錯過。 –

+0

@Andrei Odegov對不起,謝謝你指出。我設法使刪除工作正常。 –

回答

2

$Objects = @(...)確保$Objects是一個數組,即使該命令沒有返回結果。一個數組(甚至是一個空數組)永遠不會等於$null,所以你的第一個條件永遠不會被觸發。

變化

if ($Objects -eq $null) { 

if ($Objects.Count -gt 0) { 

和代碼應該做你的期望。

0

或者Ansgar's solution您可以避免將$Objects變量分配給數組(看不出爲什麼需要這樣)。

$Objects = Get-WmiObject -Class Win32_UserProfile | Where { 
    (!$_.Special) -and 
    $_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-15) 
} 

-eq $null然後可以使用檢查。您也可能會發現[string]::IsNullOrEmpty($Object)有用,因爲這將檢測到一個空陣列。

你提到沒有輸出......這是懷疑,因爲你應該得到Write-Hosts印刷東西。在我的機器上,我得到了一個錯誤$_.ConvertToDateTime ...我建議在控制檯的一條平行線上運行你的代碼,以確保它符合你的期望。

+0

無法爲此代碼獲取WMI解決的日期時間問題,但是我的本地腳本副本存在問題。 –

0

好吧,事實證明這是一個問題的組合。主要的是它運行的PS版本,不能在4上工作。下一階段是嘗試強制PowerShell將其作爲版本3運行。如前所述,它不需要在數組中處理。這是我的最終腳本(不喜歡每狀態後返回所以離開原來的格式):

if ($PSVersionTable.PSVersion.Major -eq 4) 
{ 
    Write-host "this script will terminate as machine is running PowerShell version 4" 
    exit 1 
    } 

    Else 
    { 


Start-Sleep -s 5 

Set-Location -Path C:\Users  

#Assign how old the user profile must be for deletion 

     [DateTime]$AdjustedDate = (Get-Date).AddDays(-15) 
     [DateTime]$CompareDate = Get-Date $AdjustedDate -Format MM-dd-yyyy 
     $Hostname = $env:computername 
     #$testdate = Get-WmiObject -Class Win32_UserProfile | select {$_.ConvertToDateTime($_.lastusetime).ToShortDateString()} 
     #write-host $testdate 
     #Get WMI object where it is a domain account and over 15 days old 
     $Objects = Get-WmiObject -Class Win32_UserProfile | Where {(!$_.Special) -and ($_.ConvertToDateTime($_.lastusetime) -lt (Get-Date).AddDays(-15)) -and $_.LocalPath -notlike "*.NET*" -and $_.LocalPath -notlike "*SQL*" -and $_.LocalPath -notlike "*Admin*" -and $_.LocalPath -notlike "*ADMIN*"} 


     #If no users returned, write to host. 
     If($Objects.Count -eq 0) 
     { 
      Write-host "no profiles found for deletion" 
     } 

     #If users are found, do the following 
     Else 
     { 
      Foreach($Object in $Objects) 
      { 
      Write-host "'$($object.LocalPath)' has been identified for deletion" 
Write-host " " 
      } 

      Foreach($Object in $Objects) 
      { 

          Try{ 
          Write-Host "Attempting to delete profile '$($Object.LocalPath)'" 
          $UserSID = (Get-WmiObject Win32_UserProfile | Where {$_.LocalPath -like '$($object.LocalPath)'}).SID 
          Remove-WmiObject -InputObject $Object 
          Write-Host "Deleted profile '$($Object.LocalPath)' successfully." 
          } 
          Catch{ 
          Write-Host "Unable to delete profile '$($Object.LocalPath)' due to the following error" 
          Write-Host "$error" -ErrorAction Continue 
          } 

      }  
     } 
}