我正在嘗試獲取單個 AD 域的每個 Active Directory (AD) 域控制器的作業系統安裝日期作為清單報告的一部分。感謝這篇文章,下面代碼塊中顯示的所有內容都可以在任何 AD 環境中運行。“安裝日期”列在網格視圖中顯示空白資料。要獲得安裝日期,它需要一個 PS-remoting 會話進入每個目標域控制器。經過相當多的研究,不確定我做錯了什么,所以在這里發帖。
# Import AD module
Import-Module ActiveDirectory
# Get your ad domain
$DomainName = (Get-ADDomain).DNSRoot
$AllDCs = Get-ADDomainController -Filter * -Server $DomainName | Select-Object Hostname,Ipv4address,isGlobalCatalog,Site,Forest,OperatingSystem
# Create empty DataTable object
$DCTable = New-Object System.Data.DataTable
# Add columns
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[0].Caption = "Hostname"
$DCTable.Columns[0].ColumnName = "Hostname"
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[1].Caption = "IPv4Address"
$DCTable.Columns[1].ColumnName = "IPv4Address"
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[2].Caption = "isGlobalCatalog"
$DCTable.Columns[2].ColumnName = "isGlobalCatalog"
$DCTable.Columns[2].DataType = "Boolean"
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[3].Caption = "Site"
$DCTable.Columns[3].ColumnName = "Site"
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[4].Caption = "Forest"
$DCTable.Columns[4].ColumnName = "Forest"
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[5].Caption = "OperatingSystem"
$DCTable.Columns[5].ColumnName = "OperatingSystem"
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[6].Caption = "PingStatus"
$DCTable.Columns[6].ColumnName = "PingStatus"
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[7].Caption = "InstallDate"
$DCTable.Columns[7].ColumnName = "InstallDate"
# Loop each DC
ForEach($DC in $AllDCs)
{
{
(invoke-command (Get-WmiObject Win32_OperatingSystem).ConvertToDateTime( (Get-WmiObject Win32_OperatingSystem).InstallDate ))
}
$ping = ping $DC.Hostname -n 1 | Where-Object {$_ -match "Reply" -or $_ -match "Request timed out" -or $_ -match "Destination host unreachable"
}
switch ($ping)
{
{$_ -like "Reply*" } { $PingStatus = "Success" }
{$_ -like "Request timed out*"} { $PingStatus = "Timeout" }
{$_ -like "Destination host unreachable*"} { $PingStatus = "Unreachable" }
default { $PingStatus = "Unknown" }
}
$DCTable.Rows.Add( $DC.Hostname,
$DC.Ipv4address,
$DC.isGlobalCatalog,
$DC.Site,
$DC.Forest,
$DC.OperatingSystem,
$PingStatus,
$InstallDate
)| Out-Null
}
# Display results in console
$DCTable | Sort-Object Site | Out-GridView
#Creating head style
$Head = @"
<style>
body {
font-family: "Arial";
font-size: 8pt;
}
th, td, tr {
border: 1px solid #e57300;
border-collapse: collapse;
padding: 5px;
text-align: center;
}
th {
font-size: 1.2em;
text-align: left;
background-color: #003366;
color: #ffffff;
}
td {
color: #000000;
}
.even { background-color: #ffffff; }
.odd { background-color: #bfbfbf; }
h6 { font-size: 12pt;
font-color: black;
font-weight: bold;
}
text { font-size: 10pt;
font-color: black;
}
}
</style>
"@
uj5u.com熱心網友回復:
如評論中所述,您需要:
- 通過以下方式確保
Invoke-Command在目標 DC 上實際執行陳述句:- 圍繞中的陳述句
{...}(創建腳本塊,而不是在本地執行) - 傳遞
-ComputerName $DC.Hostname到Invoke-Command
- 圍繞中的陳述句
- 將該呼叫的輸出分配給
$InstallDate
ForEach($DC in $AllDCs)
{
$ping = ping $DC.Hostname -n 1 | Where-Object {$_ -match "Reply" -or $_ -match "Request timed out" -or $_ -match "Destination host unreachable"}
switch ($ping)
{
{$_ -like "Reply*" } { $PingStatus = "Success" }
{$_ -like "Request timed out*"} { $PingStatus = "Timeout" }
{$_ -like "Destination host unreachable*"} { $PingStatus = "Unreachable" }
default { $PingStatus = "Unknown" }
}
$InstallDate = if($PingStatus -eq 'Success'){
Invoke-Command { (Get-WmiObject Win32_OperatingSystem).ConvertToDateTime( (Get-WmiObject Win32_OperatingSystem).InstallDate )) } -ComputerName $DC.Hostname
}
$DCTable.Rows.Add( $DC.Hostname,
$DC.Ipv4address,
$DC.isGlobalCatalog,
$DC.Site,
$DC.Forest,
$DC.OperatingSystem,
$PingStatus,
$InstallDate
)| Out-Null
# ...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/366190.html
上一篇:azure中的服務主體監控
