PowerShell:清单报告显示每个 AD 域控制器的安装日期数据为空白

PowerShell: Inventory Report Shows Blank Data for Installation Date of Every AD Domain Controller

我正在尝试获取单个 AD 域的每个 Active Directory (AD) 域控制器的操作系统安装日期作为清单报告的一部分。感谢 this article,下面代码块中显示的所有内容都可以在任何 AD 环境中运行。但是,“安装日期”列在网格视图中显示空白数据。要获得安装日期,它需要一个 PS-远程会话到每个目标域控制器。经过大量研究后不确定我在这方面做错了什么,所以在这里发帖。

# 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>
"@

如评论中所述,您需要:

  • 确保您在目标 DC 上实际执行了 Invoke-Command 语句,方法是:
    • 围绕{...}中的语句(创建一个脚本块,而不是在本地执行)
    • -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
    # ...

}