使用变量后 PowerShell 命令失败 [英] PowerShell command fails after variable is used

查看:87
本文介绍了使用变量后 PowerShell 命令失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个命令来收集具有某些要求的使用配置文件,这是我的代码:

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

我使用 $numberOfDays 变量来确定要用作过滤器的从今天的日期中减去的天数.现在它被注释掉了,命令成功了,虽然因为 $numberOfDays 没有定义我假设它使用的是空值?我不太确定,但它是这样工作的......

I use the $numberOfDays variable to determine the number of days subtracted from today's date that I want to use as the filter. Right now it's commented out, and the command succeeds, although since $numberOfDays isn't defined I assume it's using a null value? I'm not really sure but it works that way...

但是,当我将 $numberOfDays 分配给 30 时,它根本无法使用任何内容填充变量 $profileStructsToRemove.它完全失败了.我真的可以使用一些意见来解释为什么会发生这种情况.

However, when I assign $numberOfDays to 30, it fails to populate the variable $profileStructsToRemove with ANYTHING at all. It just utterly fails. I could really use some input on why this is happenening.

  1. $numberOfDays 未定义时,命令如何工作?它只是一个空值,还是在 AddDays 函数中将其视为 0?
  2. 为什么在为 $numberOfDays 赋值后此命令会失败?
  1. How is the command working when $numberOfDays isn't defined? Is it just a null value, or treating it as 0 for the AddDays function?
  2. Why is this command failing once $numberOfDays is assigned a value?

推荐答案

  1. 是的,它是空的,增加了零天.这很好 - 你可以测试:

  1. Yes, it's null, which added zero days. This is fine - you can test with:

$(Get-Date).Date.AddDays($null)

  • 您确定有与该数据匹配的配置文件吗?当$numberOfDaysnull时检查数据确认.

    这篇关于使用变量后 PowerShell 命令失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆