获得Pc&使用一个脚本远程监控Info

时间:2015-07-09 18:01:29

标签: powershell

代码1远程获取PC信息,代码2远程获取监视器信息。如何将代码2合并到代码1中?我希望输出看起来像PC信息,然后是Monitor Info。我试图合并,但有很多错误。

代码1记录所有信息,例如查询和未查询的计算机。

    Code 1:  Courtesy of Alexander Obersht


    $ArrComputers = gc .\computernames.txt
    $OutputLog = ".\output.log" 
    $NotRespondingLog = ".\notresponding.log" 
    $ErrorActionPreference = "Stop" 

    Clear-Host

     ForEach ($Computer in $ArrComputers) 
    {
try
{
    $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
    $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
    $Version = Get-WmiObject -Namespace "Root\CIMv2" `
        -Query "Select * from Win32_ComputerSystemProduct" `
        -computer $computer | select -ExpandProperty version
}
catch
{
    $Computer | Out-File -FilePath $NotRespondingLog -Append -Encoding UTF8
    continue
}

$Header = "System Information for: {0}" -f $computerSystem.Name

write-host $Header -BackgroundColor DarkCyan
$Header | Out-File -FilePath $OutputLog -Append -Encoding UTF8

$Output = (@"
-------------------------------------------------------

   Model: {0} 
  Serial Number: {1} 
  Version: {2} 

   -------------------------------------------------------

  "@) -f $computerSystem.Model, $computerBIOS.SerialNumber, $Version


Write-Host $Output
$Output | Out-File -FilePath $OutputLog -Append -Encoding UTF8
   }


   Code 2: 


   $users = gc .\computernames1.txt

   gwmi WmiMonitorID -Namespace root\wmi -computername $users |
   Select PSComputerName,
     @{n="Model";e={[System.Text.Encoding]::ASCII.GetString($_.UserFriendlyName -ne 00)}},
     @{n="Serial Number";e={[System.Text.Encoding]::ASCII.GetString($_.SerialNumberID -ne 00)}} |
     Format-List | Out-File '.\report.csv'

1 个答案:

答案 0 :(得分:0)

从上一个问题更新了我的解决方案:

$ArrComputers = gc .\computernames.txt
$OutputLog = ".\output.log" # Main log
$NotRespondingLog = ".\notresponding.log" # Logging "unqueried" hosts

$ErrorActionPreference = "Stop" # Or add '-EA Stop' to Get-WmiObject queries
Clear-Host

ForEach ($Computer in $ArrComputers) 
{
    try
    {
        $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
        $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
        $Version = Get-WmiObject -Namespace "Root\CIMv2" `
            -Query "Select * from Win32_ComputerSystemProduct" `
            -computer $computer | select -ExpandProperty version
        $MonitorInfo = gwmi WmiMonitorID -Namespace root\wmi `
            -computername $Computer `
            | Select PSComputerName, `
                @{n="Model";e={[System.Text.Encoding]::ASCII.GetString(`
                    $_.UserFriendlyName -ne 00)}},
                @{n="Serial Number";e={[System.Text.Encoding]::ASCII.GetString(`
                    $_.SerialNumberID -ne 00)}}     
    }
    catch
    {
        $Computer | Out-File -FilePath $NotRespondingLog -Append -Encoding UTF8
        continue
    }

    $Header = "System Information for: {0}" -f $computerSystem.Name

    # Outputting and logging header.
    write-host $Header -BackgroundColor DarkCyan
    $Header | Out-File -FilePath $OutputLog -Append -Encoding UTF8

    $Output = (@"
-------------------------------------------------------

Model          : {0} 
Serial Number  : {1} 
Version        : {2} 
Monitor Model  : {3}
Monitor Serial : {4}

-------------------------------------------------------

"@) -f $computerSystem.Model, $computerBIOS.SerialNumber, $Version, `
       $MonitorInfo.Model, $MonitorInfo."Serial Number"

    # Ouputting and logging WMI data
    Write-Host $Output
    $Output | Out-File -FilePath $OutputLog -Append -Encoding UTF8
}