Powershell 脚本未将WMI对象正确分配给数组
我试图创建一个脚本,检测域用户配置文件,是长达15天,并删除他们。这将捆绑在一个作业中,该作业将在运行之前注销所有空闲会话 我知道这通常是通过GPO完成的,但是由于各种原因,它不适合用于此特定业务领域 这是我的密码:Powershell 脚本未将WMI对象正确分配给数组,powershell,wmi,Powershell,Wmi,我试图创建一个脚本,检测域用户配置文件,是长达15天,并删除他们。这将捆绑在一个作业中,该作业将在运行之前注销所有空闲会话 我知道这通常是通过GPO完成的,但是由于各种原因,它不适合用于此特定业务领域 这是我的密码: #指定用户配置文件的删除时间 [int]$NoOfDays=15 #获取WMI对象,其中该对象是域帐户且已超过15天 $Objects=@(获取wmioobject-Class Win32_UserProfile|其中{ (!$特别)-和 $\.ConvertToDateTime(
#指定用户配置文件的删除时间
[int]$NoOfDays=15
#获取WMI对象,其中该对象是域帐户且已超过15天
$Objects=@(获取wmioobject-Class Win32_UserProfile|其中{
(!$特别)-和
$\.ConvertToDateTime($\.LastUseTime)-lt(获取日期).AddDays(-15)
})
如果($Objects-eq$null){
#如果未返回任何用户,请写入主机。
写入主机“未找到要删除的配置文件”
}否则{
#如果找到用户,请执行以下操作
foreach($Objects中的对象){
写入主机“$($object.LocalPath)”已标识为要删除
}
foreach($Objects中的对象){
试一试{
写入主机“正在尝试删除配置文件“$($Object.LocalPath)”
删除WmiObject
已成功写入主机“已删除配置文件”$($Object.LocalPath)”
}抓住{
写入主机“无法删除配置文件“$($Object.LocalPath)”—错误操作继续
}
}
}
并没有输出,它直接返回到命令行,并没有错误
我尝试注释掉删除配置文件的位,在每个语句的第一个之后结束else,以查看标识要删除的内容,但它没有起作用。$Objects=@(…)
确保$Objects
是一个数组,即使命令没有返回结果。数组(即使是空数组)永远不会等于$null
,因此不会触发第一个条件
改变
if ($Objects -eq $null) {
到
代码应该按照您的期望执行。或者,您可以避免将$Objects
变量指定给数组(不明白为什么需要这样做)
-eq$null
然后可以使用检查。您还可能发现[string]::IsNullOrEmpty($Object)
非常有用,因为这将检测空数组
你提到没有输出。。。这是可疑的,因为你应该让
写主机打印一些东西。在我的机器上,$\uConvertToDateTime
。。。我建议您在控制台中运行一行代码,以确保它执行的是您期望的操作。好的,因此这是一系列问题的组合。主要的一个是它运行的PS版本,不适用于4。下一步是尝试强制PowerShell以版本3的形式运行它。如前所述,它不需要在数组中处理。这是我的最后一个脚本(不喜欢在每个条件之后返回,所以格式保持不变):
上面给出的代码中似乎遗漏了Remove-WmiObject cmdlet的一些输入参数。@Andrei Odegov很抱歉,谢谢您指出这一点。我已成功使删除正常工作。无法使用WMI解决此代码的日期时间问题,我的脚本本地副本存在问题。
if ($Objects.Count -gt 0) {
$Objects = Get-WmiObject -Class Win32_UserProfile | Where {
(!$_.Special) -and
$_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-15)
}
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
}
}
}
}