PDA

Показать полную графическую версию : [решено] Инфа об ОС и отчет об ошибках


s_admin
23-03-2009, 11:47
Здрастье!
Вобщем есть такая проблема...написал тут простенький скрипт, и столкнулся вот с чем.
trap { write-host "Error connecting to $computer" -fore red
"$computer" | out-file c:\errors.txt -append continue }
$computers = Get-Content "D:\PC.txt"
foreach ( $computer in $computers ) {
get-wmiobject win32_operatingsystem -comp $computer -ea stop }
В файлике PC.txt есть имена машин: comp1, comp2, comp3. comp1 и comp3 существуют в сети, comp2 - нет. По идее, скрипт должен выдать ошибку на втором, записать ее в лог и продолжить скрипт с comp3. Но после comp2, выполнение скрипта завершается.... чего не так я сделал?
Если делать вот так:
trap { write-host "Error connecting to $computer" -fore red
"$computer" | out-file c:\errors.txt -append continue }
$computer = "comp1" get-wmiobject win32_operatingsystem -comp $computer -ea stop
$computer = "comp2" get-wmiobject win32_operatingsystem -comp $computer -ea stop
$computer = "comp3" get-wmiobject win32_operatingsystem -comp $computer -ea stop
то все работает. А вот с условием foreach нет.
Заранее спасибо.

Petya V4sechkin
23-03-2009, 12:22
s_admin, внутри цикла работает:
$computers = Get-Content "D:\PC.txt"
foreach ( $computer in $computers ) {
trap {
write-host "Error connecting to $computer" -fore red
"$computer" | out-file C:\errors.txt -append
continue
}
get-wmiobject win32_operatingsystem -comp $computer -ea stop
}

Все дело в области:
Перехват ошибок с помощью –ErrorAction (–EA) в Windows PowerShell (http://oszone.net/8762/Windows_PowerShell)

s_admin
23-03-2009, 12:26
Блин, стыдно, что сам не догадался.... :blush:
Спасибо огромное!




© OSzone.net 2001-2012