powershell 修复获取AD计算机:服务器返回了以下错误:枚举上下文无效

zxlwwiss  于 2022-12-26  发布在  Shell
关注(0)|答案(1)|浏览(154)

我正在尝试运行一个脚本以获取PC列表并将其导出到csv文件。当我运行该脚本时,它返回"服务器已返回以下错误:无效枚举上下文。"错误。
下面是我的脚本:

Get-ADComputer -Filter * -Property * | Select-Object Created,Name,OperatingSystem,ipv4Address,DistinguishedName | Export-CSV test.csv -NoTypeInformation -Encoding UTF8

我做了些改变:

$properties = 'Created','Name','OperatingSystem','OperatingSystemVersion','ipv4Address','DistinguishedName'
Get-ADComputer -Filter * -Properties $properties | Export-CSV test.csv -NoTypeInformation -Encoding UTF8

但我还是收到枚举错误。
另一项更新:
代码给出了一个错误,所以我想也许我需要把执行策略设置为Unrestricted,但是这也没有解决这个问题。现在我有了一个新的错误和枚举错误。

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

$properties = @(
    'Created'
    'Name'
    'OperatingSystem'
    'ipv4Address'
    'DistinguishedName'
)

Get-ADOrganizationalUnit -Filter * | ForEach-Object {
    Get-ADComputer -Filter * -Properties $properties -SearchBase $_.DistinguishedName
} | Select-Object $properties | Export-CSV test.csv NoTypeInformation -Encoding UTF8

以下是错误(奇数,因为AD未关闭):

Get-ADComputer : Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running.
At \\nasinfrastructure.wakefern.com\Infrastructure\Security\Powershell\Susan_Scripts\AD_User_PCs\Get-ADComputer_4All.ps1:12 char:5
+     Get-ADComputer -Filter * -Properties $properties -SearchBase $_.D ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [Get-ADComputer], ADServerDownException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
 
Get-ADComputer : The server has returned the following error: invalid enumeration context.
At \\nasinfrastructure.wakefern.com\Infrastructure\Security\Powershell\Susan_Scripts\AD_User_PCs\Get-ADComputer_4All.ps1:12 char:5
+     Get-ADComputer -Filter * -Properties $properties -SearchBase $_.D ...
jgovgodb

jgovgodb1#

在具有许多对象的大型环境中可能会发生这种情况,我看到您已更新代码以仅查询特定的属性集,而不是查询所有(-Properties *),这是一个良好的开端。解决此问题的最佳方法是查询每个组织单位的所有计算机。

$properties = @(
    'Created'
    'Name'
    'OperatingSystem'
    'ipv4Address'
    'DistinguishedName'
)

Get-ADOrgnizationalUnit -Filter * | ForEach-Object {
    Get-ADComputer -Filter * -Properties $properties -SearchBase $_.DistinguishedName
} | Select-Object $properties | Export-CSV test.csv -NoTypeInformation -Encoding UTF8

相关问题