获取-AdComputer筛选参数不接受获取最新的输出不接受、参数、最新、AdComputer

2023-09-09 21:41:35 作者:顾北

我穿过一个奇怪的错误PowerShell脚本返回,我在Active Directory中的计算机特定的OU(以及子OU的)已启用,并登录到在某一特定日期栽了跟头。

I've stumbled across an odd error in a PowerShell script that returns for me the computers in Active Directory in a specific OU (and sub OU's) that are enabled and logged on to within a certain date.

下面的代码片段不工作:

The following snippet does work:

$date = (get-date).AddDays(-100)
Get-ADComputer -Filter {(enabled -eq "true") -and (lastLogonTimestamp -gt  $date)} -Properties lastLogonTimestamp -SearchBase "CN=Computers,DC=some,DC=domain,DC=com"

...但是,我开始尝试做,这是一条线:

... however I initially attempted to do this is one line:

Get-ADComputer -Filter {(enabled -eq "true") -and (lastLogonTimestamp -gt  $((get-date).AddDays(-100)))} -Properties lastLogonTimestamp -SearchBase "CN=Computers,DC=some,DC=domain,DC=com"

...但我反复收到以下错误(这让我试试上面的两个线路为例):

... but I repeatedly received the error below (which made me try the two line example above):

Get-ADComputer : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again.
At line:1 char:1
+ Get-ADComputer -Filter {(enabled -eq "true") -and (lastLogonTimestamp -gt  $((ge ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADComputer], PSArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.Management.Automation.PSArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

无论使用PowerShell v3的 -

我已经在Windows 8和Windows Server 2008 R2上运行脚本。我认为在这两个操作系统的安装的RSAT工具的Active Directory模块可能略有不同的版本,以及。

I've run the script on both Windows 8 and Windows Server 2008 R2 - both with PowerShell v3. I assume the Active Directory module installed by the RSAT tools on both OS's may be slightly different versions as well.

数据明智的我已经得到了我所需要的,但我难倒,为什么一行程序无法正常工作。不需要任何的获取最新的或Get-AdComputer的cmdlet的参数的路径,但一些尝试,我也确信我的工作目录为C:

Data-wise I have got what I need, but I am stumped as to why the one-liner isn't working. The argument "path" isn't required for either the Get-Date or Get-AdComputer cmdlets, but as something to try I've also made sure my working directory is C:.

我很感兴趣,如果有人或许可以提供,为什么这可能是一些见解 - 我以前在非ActiveDirectory中模块的cmdlet使用这种类型的语法,所以我倾向于认为,因为查询在这也许是越来越不正确地传递给Get-AdComputer cmdlet将自己的过滤器参数(和是我们都必须忍受)。

I'm interested if anyone may be able to provide some insight in to why this might be - I've used this type of syntax before in non-ActiveDirectory module cmdlets, so I'm inclined to assume that because the query is in the "filter" parameter perhaps this is getting passed incorrectly to the Get-AdComputer cmdlet itself (and is something we all have to live with).

推荐答案

过滤器部件的右侧必须是价值,而不是前pression。

The right-hand side of a filter component must be a value, not an expression.

将该溶液(作为你已经发现自己)是分配由前pression到可变而产生的值,并使用该变量在过滤器上的右侧:

The solution (as you already found out yourself) is to assign the value produced by the expression to a variable, and use that variable on the right-hand side of the filter:

$date = (get-date).AddDays(-100)
Get-ADComputer -Filter {(enabled -eq "true") -and (lastLogonTimestamp -gt  $date)} ...