[英]PowerShell command fails after variable is used
I'm trying to run a command to gather use profiles with certain requirements, here's my code: 我正在尝试运行命令来收集具有特定要求的使用配置文件,这是我的代码:
#$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
I use the $numberOfDays
variable to determine the number of days subtracted from today's date that I want to use as the filter. 我使用
$numberOfDays
变量来确定从我今天要用作过滤器的日期中减去的天数。 Right now it's commented out, and the command succeeds, although since $numberOfDays
isn't defined I assume it's using a null value? 现在它被注释掉了,命令成功了,虽然
$numberOfDays
没有定义,我假设它使用空值? I'm not really sure but it works that way... 我不太确定,但它的工作方式......
However, when I assign $numberOfDays
to 30, it fails to populate the variable $profileStructsToRemove
with ANYTHING at all. 但是,当我将
$numberOfDays
分配给30时,它无法用ANYTHING填充变量$profileStructsToRemove
。 It just utterly fails. 它完全失败了。 I could really use some input on why this is happenening.
我真的可以使用一些输入来解释为什么会发生这种情况。
$numberOfDays
isn't defined? $numberOfDays
,命令如何工作? Is it just a null value, or treating it as 0 for the AddDays
function? AddDays
函数? $numberOfDays
is assigned a value? $numberOfDays
分配值后,为什么此命令失败? gms0ulman's helpful answer answers question #1 well (converting $null
to an [int]
yields 0
). gms0ulman的有用答案回答问题#1井(将
$null
转换$null
[int]
产生0
)。
As for question #2: 至于问题#2:
At least on Windows 10 on a non-domain machine, the .LastUseTime
property seemingly always returns the current date and time , which (a) makes it useless and (b) explains why you didn't see any results. 至少在非域机器上的Windows 10上,
.LastUseTime
属性似乎总是返回当前的日期和时间 ,这(a)使它无用,(b)解释了为什么你没有看到任何结果。
You could try to check the .LastDownloadTime
instead, if that field has a value in your case - I presume it would only have a value if the profiles are roaming profiles. 您可以尝试检查
.LastDownloadTime
, 如果该字段在您的情况下具有值 - 我认为如果配置文件是漫游配置文件,它将只有一个值。
For reference, here's the full list of available time stamps: 作为参考,这里是可用时间戳的完整列表:
LastAttemptedProfileDownloadTime
, LastAttemptedProfileUploadTime
, LastBackgroundRegistryUploadTime
, LastDownloadTime
, LastUploadTime
, LastUseTime
. LastAttemptedProfileDownloadTime
, LastAttemptedProfileUploadTime
, LastBackgroundRegistryUploadTime
, LastDownloadTime
, LastUploadTime
, LastUseTime
。
As for how to optimize the code in your question in general: 至于如何优化问题中的代码 :
PowerShell string operators are case-insensitive by default, so there's no need for .toUpper()
. PowerShell字符串运算符默认情况下不区分大小写,因此不需要
.toUpper()
。
You could combine your multiple Where-Object
calls into a single one, and you can use 您可以将多个
Where-Object
调用合并为一个,并且可以使用
-notin
with an array of paths on the RHS, instead of using -ne
with individual paths. -notin
与RHS上的路径数组,而不是使用-ne
与单独的路径。
To put it all together (PSv3+; keeping in mind that comparing against .LastUsedTime
may be pointless): 把它们放在一起(PSv3 +;请记住,与
.LastUsedTime
进行比较可能毫无意义):
$profileStructsToRemove = Get-CimInstance win32_userprofile | Where-Object {
$_.LastUseTime -lt $(Get-Date).Date.AddDays(-$numberOfDays) -and
$_.LocalPath -notin 'C:\USERS\ADMINISTRATOR',
'C:\USERS\SOME_PROFILE_TO_KEEP',
'C:\USERS\PUBLIC'
}
Yes, it's null, which added zero days. 是的,它是null,它增加了零天。 This is fine - you can test with:
这很好 - 您可以测试:
$(Get-Date).Date.AddDays($null)
Are you sure there are profiles that match that data? 您确定有匹配该数据的配置文件吗? Check the data when
$numberOfDays
is null
to confirm. $numberOfDays
为null
时检查数据以确认。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.