Войти

Показать полную графическую версию : [решено] Get-adcomputer вместо ошибки ,что объект не найдет писал в файл имя этого компа


ejik_off
22-02-2017, 09:43
Добрый день.
Не знаю как решить задачу, что бы все не найденные компы записывать в отдельный файл.
есть такой скрипт
Get-Content d:\Script\PcList.txt | Get-ADComputer -Properties * | select Name,IPv4Address,Enabled,LastLogonDate,CanonicalName,DistinguishedName,OperatingSystem |
Export-CSV -Path 'C:\Script\ListPc.csv' -Encoding UTF8
Если компьютера нет в Ad, то в консоле появляется ошибка
Get-ADComputer : Cannot find an object with identity: 'PC-3167' under: 'DC=city,DC=corp'.
At line:2 char:51
+ Get-Content C:\Script\PcList.txt | Get-ADComputer <<<< -Properties * | select Name,IPv4Address,Enabled,LastLogonDate,CanonicalName,DistinguishedName,OperatingSystem |
+ CategoryInfo : ObjectNotFound: (PC-3167:ADComputer) [Get-ADComputer], ADIdentityNotFoundException
+ FullyQualifiedErrorId : Cannot find an object with identity: 'PC-3167' under: 'DC=city,DC=corp'.,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
как сделать, что бы эти компы записывались в отдельный файл?

Kazun
22-02-2017, 09:54
.... | Get-ADComputer -Properties * -ErrorVariable comps |....
[string[]]$comps.TargetObject | Out-File mcomps.txt

ejik_off
22-02-2017, 10:15
Kazun, я не совсем понял. как ваш код совместить с моим.
вот что пытался запустить
Get-Content d:\Script\PcList.txt | Get-ADComputer -Properties * -ErrorVariable comps | select Name,IPv4Address,Enabled,LastLogonDate,CanonicalName,DistinguishedName,OperatingSystem |
Export-CSV -Path 'C:\Script\ListPc.csv' -Encoding UTF8
[string[]]$comps.TargetObject | Out-File mcomps.txt
после отроботке скрипта файл mcomps.txt пустой.

Kazun
22-02-2017, 10:22
Никто же не указывает версию PowerShell.



PS (STA) > "abc","dccd" | Get-ADComputer -Properties * -ErrorVariable comps
Get-ADComputer : Cannot find an object with identity: 'abc' under:
At line:1 char:16
+ "abc","dccd" | Get-ADComputer -Properties * -ErrorVariable comps
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (abc:ADComputer) [Get-ADComputer], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M
icrosoft.ActiveDirectory.Management.Commands.GetADComputer

Get-ADComputer : Cannot find an object with identity: 'dccd' under:
At line:1 char:16
+ "abc","dccd" | Get-ADComputer -Properties * -ErrorVariable comps
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (dccd:ADComputer) [Get-ADComputer], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M
icrosoft.ActiveDirectory.Management.Commands.GetADComputer

PS (STA) > [string[]]$comps.TargetObject
abc
dccd



Для v2:

$comps = @()
Get-Content d:\Script\PcList.txt | Foreach {
$pc = $null
$pc = Get-ADComputer $_ -Properties *
if($pc) {
$pc | select Name,IPv4Address,Enabled,LastLogonDate,CanonicalName,DistinguishedName,OperatingSystem
} else {
$comps += $_
}
} | Export-CSV -Path 'C:\Scripts\ListPc.csv' -Encoding UTF8

$comps | Out-File comps.txt

ejik_off
22-02-2017, 10:40
Никто же не указывает версию PowerShell. »
Прошу прощения, не подумал даже.
Огромное спасибо за помощь!!!!

Iska
23-02-2017, 03:06
Никто же не указывает версию PowerShell. »
Иногда ещё и версию ОС на клиенте не мешает указывать. Лично сталкивался с различным поведением одного и того же кода одной и той же версии языка на разных ОС (конкретно v.2.0 под Windows XP и Windows 7). Коллега greg zakharov не даст соврать (если ещё помнит тот эпизод).




© OSzone.net 2001-2012