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



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

  1. Ping工作站以查看是否联机/脱机
  2. 在屏幕上显示请求的信息
  3. 将请求的数据转储到文件

我之所以要写主机并转储到文件,是因为我希望能够让我们服务台的任何人运行它,如果我们需要保留数据以用于历史目的,则可以拥有文件副本。我在将信息写入文件时遇到了问题,简单明了,我不知道如何做到这一点(对不起:D(这是我的代码,如果您能提供任何帮助,我们将不胜感激。非常感谢。

$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 rootwmi -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

这是屏幕上显示的输出

-------------------------------------------------------
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: DOMAINUSERNAME
-------------------------------------------------------

非常感谢您的帮助

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

以上所述,考虑到这将被其他人使用,我将在这种情况下创建一个可重复使用的函数:

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 'rootwmi' -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。

最新更新