下午,所以我有一個運行良好的腳本,但有一個警告。如果計算機.txt 檔案中列出的設備不再存在于 AD 中,則腳本將出錯,并且該計算機將從輸出網格中丟失。
當我試圖讓它在 CSV 中匹配時,這使得它非常煩人,因為它缺少行。
cls Get-Content -Path "$home\Desktop\computers.txt" | ForEach-Object {Get-ADComputer $_ -Properties Name, CanonicalName, Enabled, IPv4Address, OperatingSystem, OperatingSystemVersion, LastLogonTimeStamp, Description | select-object Name, CanonicalName, Enabled, IPv4Address, OperatingSystem, OperatingSystemVersion, @{Name="LastLogonTime"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('dd-MM-yyy_hh:mm:ss')}}, Description} | Out-GridView
我嘗試添加一個try and catch,但是因為這不是終止的,我認為這不會起作用。有沒有人能夠幫助它正常輸出所有機器,如果它出現錯誤(機器 x 丟失),它仍然會添加一行并只說錯誤或什么?
uj5u.com熱心網友回復:
有幾種方法可以做到這一點,但我發現最簡單的方法是讓用戶Try...Catch為未找到的專案輸入一個虛擬值:
Get-Content -Path "$home\Desktop\computers.txt" | ForEach-Object {
Try {
Get-ADComputer $_ -Properties CanonicalName, IPv4Address, OperatingSystem, OperatingSystemVersion, LastLogonTimeStamp, Description -ErrorAction Stop | Select-Object Name, CanonicalName, Enabled, IPv4Address, OperatingSystem, OperatingSystemVersion, @{Name="LastLogonTime"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('dd-MM-yyy_hh:mm:ss')}}, Description
}
Catch {
[pscustomobject]@{Name=$_;CanonicalName='';Enabled='';IPv4Address='';OperatingSystem='';OperatingSystemVersion='';LastLogonTime='';Description=''}
}
} | Out-GridView
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/528442.html
標籤:电源外壳活动目录单片机
