【发布时间】:2018-10-03 17:19:05
【问题描述】:
我正在尝试运行一个命令来收集具有特定要求的使用配置文件,这是我的代码:
#$numberOfDays = 30
#$numberOfDays
$profileStructsToRemove = Get-CimInstance Win32_UserProfile |
Where-Object {$_.LastUseTime -lt $(Get-Date).Date.AddDays(-$numberOfDays) } |
Where-Object {$_.LocalPath.ToUpper() -ne 'C:\USERS\ADMINISTRATOR'} |
Where-Object {$_.LocalPath.ToUpper() -ne 'C:\USERS\SOME_PROFILE_TO_KEEP'} |
Where-Object {$_.LocalPath.ToUpper() -ne 'C:\USERS\PUBLIC'}
$profileStructsToRemove #print
我使用$numberOfDays 变量来确定从今天的日期中减去我想用作过滤器的天数。现在它被注释掉了,命令成功了,虽然因为$numberOfDays没有定义我假设它使用的是空值?我不太确定,但它是这样工作的......
但是,当我将 $numberOfDays 分配给 30 时,它根本无法用 ANYTHING 填充变量 $profileStructsToRemove。它完全失败了。我真的可以就为什么会发生这种情况提出一些意见。
- 当
$numberOfDays未定义时,该命令如何工作?它只是一个空值,还是将其视为AddDays函数的 0? - 为什么一旦给
$numberOfDays赋值,这个命令就会失败?
【问题讨论】:
-
.AddDays()需要一个整数。$null转换为[int]时为 0。[int]$null -
@Birdman:请查看我的答案的更新。
标签: windows powershell scripting user-profile