使用Powershell收集服务器信息

function Get-OSInfo
        ([string]$Server)
{  
   
    $object = Get-WmiObject  win32_computersystem -ComputerName $Server
    $status = $object.Status
    $domain = $object.Domain
    $osversion = (Get-WmiObject Win32_OperatingSystem -ComputerName $Server).version
    $ip = (get-wmiobject win32_networkadapterconfiguration -filter ipenabled=true -ComputerName $Server | Select-Object -ExpandProperty IPAddress)[0]
    $osinfo = New-Object -TypeName PSObject
    $osinfo | Add-Member NoteProperty ServerName  $Server
    $osinfo | Add-Member NoteProperty Status      $status
    $osinfo | Add-Member NoteProperty Domain      $domain
    $osinfo | Add-Member NoteProperty OSVersion   $osversion
    $osinfo | Add-Member NoteProperty IPAdress    $ip
    return $osinfo
}

$computers = Get-Content c:\works\server.txt
$result = @()
foreach ($item in $computers)
{
    $check = Test-Connection $item -Quiet -count 2
    if($check -eq $true)
    {
        $a = Get-OSInfo -Server $item
        $result += $a
    }
    else
    {
        Write-Error "服务器 $item 网络中断或已宕机 "
    } 
}
$result | ft

使用Powershell收集服务器信息_第1张图片

你可能感兴趣的:(powershell)