Powershell - 条件运算符

时间:2017-11-20 05:21:34

标签: powershell

我编写了这个脚本,将AD中的PC和服务器提取到两个不同的电子表格中。但每当我运行脚本时,我都会收到以下错误。任何人都可以通过指出我哪里出错来帮助我。

这是我得到的错误:

Get-ADComputer : The server has returned the following error: invalid enumeration context.
At line:3 char:1
+ Get-ADComputer -filter { OperatingSystem -ne '*server*' -OR Operating ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADComputer], ADException
    + FullyQualifiedErrorId : The server has returned the following error: invalid enumeration context.,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

Get-ADComputer : The server has returned the following error: invalid enumeration context.
At line:6 char:1
+ Get-ADComputer -filter { OperatingSystem -eq '*server*' -OR Operating ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADComputer], ADException
    + FullyQualifiedErrorId : The server has returned the following error: invalid enumeration context.,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

以下是脚本: -

import-module ac*

Get-ADComputer -filter { OperatingSystem -ne '*server*' -OR OperatingSystemVersion -Like '*5.1*' -OR OperatingSystemVersion -Like '*6.1*' -and Enabled -eq "true"} -SearchBase 'OU=IMComputers,OU=IM,dc=image,dc=inter' -Properties '*' | Select Name,OperatingSystem,Status, OperatingSystemVersion, `
LastLogonDate,CanonicalName | Export-Csv -NoType "C:\Temp\PC's in AD" -Encoding UTF8

Get-ADComputer -filter { OperatingSystem -eq '*server*' -OR OperatingSystemVersion -Like '*5.0*' -OR OperatingSystemVersion -Like '*5.2*' -OR OperatingSystemVersion -Like '*6.0*' -OR OperatingSystemVersion -Like '*6.1*' -OR OperatingSystemVersion -Like '*6.2*' -OR OperatingSystemVersion -Like '*6.3*' -and Enabled -eq "true"} -SearchBase 'OU=IMComputers,OU=IM,dc=image,dc=inter' -Properties '*' | Select Name,OperatingSystem,Status, OperatingSystemVersion, `
LastLogonDate,CanonicalName | Export-Csv -NoType "C:\Temp\Servers in AD" -Encoding UTF8

1 个答案:

答案 0 :(得分:1)

当我将脚本调整到我的组织时(这是一个很小的脚本),你的脚本会起作用,所以你的错误不会那么糟糕。但是几年前我读了一些关于这个的内容,感谢G ....我的朋友我找到了Invalid Enumeration Context using powershell script to check computer accounts

摘要:如果您根据未编入索引的属性进行查询,问题会更严重。操作系统和operatingSystemVersion属性未编制索引。假设您的域中有大量计算机对象,并且经常基于操作系统进行查询,那么将operatingSystem编入索引可能是有意义的。另一种解决方案是使用DirectorySearcher过滤器和LDAP过滤器而不是Get - ADComputer