脚本没有正确地将WMI对象分配给数组

时间:2017-07-18 08:34:41

标签: powershell wmi

我正在尝试创建一个脚本,用于检测最长15天的域用户配置文件并将其删除。这将捆绑在一个作业中,该作业将在运行之前注销任何空闲会话。

我知道这通常是通过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,以查看识别出删除的内容但是它没有用。

3 个答案:

答案 0 :(得分:2)

$Objects = @(...)确保$Objects是一个数组,即使该命令没有返回结果。一个数组(甚至是一个空数组)永远不会等于$null,所以你的第一个条件永远不会被触发。

更改

if ($Objects -eq $null) {

if ($Objects.Count -gt 0) {

并且代码应该按照您的期望进行。

答案 1 :(得分: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的错误...我建议在控制台中以一条平局的方式运行您的代码,以确保它正在按预期执行。

答案 2 :(得分: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
                            }

            }       
        }
}