首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用提供的powershell脚本从远程系统检索系统信息

使用提供的powershell脚本从远程系统检索系统信息
EN

Stack Overflow用户
提问于 2022-08-26 16:18:33
回答 1查看 71关注 0票数 1

我正在尝试创建一个脚本,从客户端工作站提取相关的系统信息,并提供以下内容:

online/offline

  • Displays

  • 检查工作站,以查看是否将屏幕上所请求的信息转储到

文件。

我之所以做一个写主机并将其转储到一个文件中,我希望能够让我们服务台中的任何人运行它,如果我们需要为了历史目的保留数据,我希望有一个文件副本。我很难将这些信息写到文件中,简单明了,我不知道该如何做(抱歉:D),这是我的代码,您能提供的任何帮助都将不胜感激。谢谢!

代码语言:javascript
复制
$Computers = "COMPUTERNAME"

Clear-Host

#Sets the array for the input of any string
$Outputmessage = @()

#Runs "foreach" loop statement and sets the computer variables, and performs WMI queries based on WMI Providers
foreach ($Computer in $Computers) {
    #Perform ping test on workstation prior to checking system information
    $pingtest = Test-Connection -ComputerName $Computer -Quiet -Count 1 -ErrorAction SilentlyContinue

    if ($pingtest) {
     
        #Runs WMI queries to get computer information to output on to the screen

        $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
        $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
        $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
        $computerCPU = get-wmiobject Win32_Processor -Computer $Computer
        $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
        $computerDiskType = Get-WmiObject Win32_Diskdrive | Where { $_.Model } | Select caption
        $computerDisplayCount = (Get-CimInstance -Namespace root\wmi -ClassName WmiMonitorBasicDisplayParams | where { $_.Active -like "True" }).Active.Count

                    
        #Displays on screen the system information requested translated to the below

        "-------------------------------------------------------"

        write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
        ""
        "Manufacturer: " + $computerSystem.Manufacturer
        "Model: " + $computerSystem.Model
        "Asset Tag: " + $computerBIOS.SerialNumber
        "CPU: " + $computerCPU.Name
        "HDD Capacity: " + "{0:N2}" -f ($computerHDD.Size / 1GB) + "GB"
        "HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace / $computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace / 1GB) + "GB)"
        "Disk Type: " + $computerDiskType.caption
        "RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory / 1GB) + "GB"
        "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
        "Connected Monitors: " + $computerDisplayCount
        "User logged In: " + $computerSystem.UserName
        
        ""
        "-------------------------------------------------------"

        $Result = $computerSystem.Name, $computerBIOS.Model, $computerOS.SerialNumber, $computerCPU.Name, $computerHDD.Size, $computerDiskType.caption, $computerDisplayCount, $computerSystem.UserName
            
    }
    else {
        $OutputMessage += "$Computer - OFFLINE"
    }
}

$Outputmessage + $Result | Out-File -FilePath "FILE PATH" -Encoding utf8 -Append

这是屏幕上显示的输出

代码语言:javascript
复制
-------------------------------------------------------
System Information for:  COMPUTERNAME

Manufacturer: Manufacturer
Model: System model
Asset Tag: serial number
CPU: CPU information
HDD Capacity: drive capacity
HDD Space: Free space
Disk Type: Drive type
RAM: Memory amount
Operating System: OS Version
Connected Monitors: Number of connected monitors
User logged In: DOMAIN\USERNAME

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

非常感谢你的帮助

EN

回答 1

Stack Overflow用户

发布于 2022-08-26 17:29:54

我会选择使用PSCustomObject来更好地组织您的数据。首先也是最重要的是,您的代码中有几个问题,您没有引用远程计算机,而是默认地将信息转发给您的计算机,而不是它们的。另一点需要说明的是连通性。仅仅因为你可以平一台机器,并不意味着你可以连接到它。

有了以上所述,考虑到其他人将使用这一点,我将在本例中创建一个可重用的函数:

代码语言:javascript
复制
Function Get-SystemInfo {
[cmdletbinding()]
    Param (
        [parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true)]
        [string[]]$ComputerName = $env:COMPUTERNAME
    )
    Begin 
    { 
        $PSCustomObject = [PSCustomObject]@{
            ComputerName = $null
            Status       = $null
            Manufacturer = $null
            Model        = $null
            AssetTag     = $null
            CPU          = $null
            HDDCapacity  = $null
            HDDSpace     = $null
            DiskType     = $null
            RAM          = $null
            OperatingSystem   = $null
            ConnectedMonitors = $null
            UserloggedIn = $null
        }
    }
    Process
    {
        foreach ($computer in $ComputerName)
        {
            try {
                $PSCustomObject.ComputerName = $computer
                
                $cimSession = New-CimSession -ComputerName $computer -ErrorAction 'Stop'
                $PSCustomObject.Status = 'Online - Connected'

                $computerSystem = Get-CimInstance -ClassName 'Win32_ComputerSystem' -CimSession $cimSession -Property 'Manufacturer','Model','TotalPhysicalMemory ','UserName'
                $PSCustomObject.Manufacturer = $computerSystem.Manufacturer
                $PSCustomObject.Model = $computerSystem.Model
                $PSCustomObject.RAM = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory / 1GB) + "GB"
                $PSCustomObject.UserloggedIn = $computerSystem.UserName

                $computerBIOS = Get-CimInstance -ClassName 'Win32_BIOS' -CimSession $cimSession
                $PSCustomObject.AssetTag = $computerBIOS.SerialNumber

                $computerOS = Get-CimInstance -ClassName 'Win32_OperatingSystem' -CimSession $cimSession
                $PSCustomObject.OperatingSystem = $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion

                $computerCPU = Get-CimInstance -ClassName 'Win32_Processor' -CimSession $cimSession
                $PSCustomObject.CPU = $computerCPU.Name

                $computerHDD = Get-CimInstance -ClassName 'Win32_LogicalDisk' -CimSession $cimSession -Filter 'drivetype=3'
                $PSCustomObject.HDDCapacity = "{0:N2}" -f ($computerHDD.Size / 1GB) + "GB"
                $PSCustomObject.HDDSpace  = "{0:P2}" -f ($computerHDD.FreeSpace / $computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace / 1GB) + "GB)"

                $computerDiskType = Get-CimInstance -ClassName 'Win32_Diskdrive' -CimSession $cimSession | Where-Object -FilterScript { $_.Model } 
                $PSCustomObject.DiskType = $computerDiskType.caption

                $computerDisplayCount = (
                    Get-CimInstance -Namespace 'root\wmi' -ClassName 'WmiMonitorBasicDisplayParams' -CimSession $cimSession | 
                    Where-Object -FilterScript { $_.Active -like "True" } 
                ).Active.Count
                $PSCustomObject.ConnectedMonitors = $computerDisplayCount
            }
            catch {
                $PSCustomObject.Status       = if (Test-Connection -ComputerName $computer -Count 1) { 'Online - Unable to Connect' } else { 'offline' }
                $PSCustomObject.Manufacturer = $null
                $PSCustomObject.Model        = $null
                $PSCustomObject.AssetTag     = $null
                $PSCustomObject.CPU          = $null
                $PSCustomObject.HDDCapacity  = $null
                $PSCustomObject.HDDSpace     = $null
                $PSCustomObject.DiskType     = $null
                $PSCustomObject.RAM          = $null
                $PSCustomObject.OperatingSystem   = $null
                $PSCustomObject.ConnectedMonitors = $null
                $PSCustomObject.UserloggedIn = $null
            }
            finally {
                $PSCustomObject
                if ($cimSession)
                {
                    Remove-CimSession -CimSession $cimSession -ErrorAction 'SilentlyContinue'
                }
            }
        }
    }
    End { }
}

当输入Get-SystemInfo -ComputerName 'ComputerOne','ComputerTwo'时,函数变得更加方便,而不是每次修改$computers变量。从这里,您可以将管道输送到Out-FileExport-Csv或您选择的任何其他导出cmdlet。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73503904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档