PowerShell 询问远程系统

PowerShell interrogate a remote system

我编写了一个查询本地系统的函数。只是以有用的格式收集我能得到的任何信息。所以我想知道可以用这种方式询问其他系统的功能。 function sys-remote <$ip-or-hostname> 这样它就可以尝试 return 尽可能多地了解有关该系统的信息。这真的只是一个想法,我想有几点会有用:

• 有了 IP 地址,我们如何才能以最类似于 PowerShell 的方式解析主机名?

• 是否提供了IP地址的主机名,我们能不能解析尽可能多的信息。即 MAC 地址、主机名、IP(如果我们可以看到这些,可能还有其他 IP 地址)?

• 我们能否恢复该系统上的共享驱动器,以便查看可能要连接的共享列表。

• 系统信息如何,总是需要 WinRM,还是 WMI 或 CIM 可以满足下面的大部分内容?

• 也许 return 如果可能的话,还有一个以逗号分隔的列表,列出该远程系统上打开的任何端口?

• 如果远程系统是 Linux 怎么办?从我们的 Windows 系统远程查询的 Linux 系统中,我们可以合理地获得多少上述内容(我猜 WinRM 和 WMI 已经过时了,但也许 CIM 仍然可行?)?

一般来说,return 从诊断的角度来看,这样的信息转储会非常有用,因为它会提供大量关于系统工作的信息。像上面的任何东西(或者实际上任何其他我在这里没有想到的有用的东西来检查)将不胜感激。

function sys {
    $System = get-wmiobject -class "Win32_ComputerSystem"
    $Mem = [math]::Ceiling($System.TotalPhysicalMemory / 1024 / 1024 / 1024)

    $wmi = gwmi -class Win32_OperatingSystem -computer "."
    $LBTime = $wmi.ConvertToDateTime($wmi.Lastbootuptime)
    [TimeSpan]$uptime = New-TimeSpan $LBTime $(get-date)
    $s = "" ; if ($uptime.Days -ne 1) {$s = "s"}
    $uptime_string = "$($uptime.days) day$s $($uptime.hours) hr $($uptime.minutes) min $($uptime.seconds) sec"

    $job_cpu         = Start-Job -ScriptBlock { (Get-WmiObject -Class Win32_Processor).Name }
    $job_cpu_cores   = Start-Job -ScriptBlock { (Get-WmiObject -Class Win32_Processor).NumberOfCores }
    $job_cpu_logical = Start-Job -ScriptBlock { (Get-WmiObject -Class Win32_Processor).NumberOfLogicalProcessors }
    ""
    "Hostname:        $($System.Name)"
    "Domain:          $($System.Domain)"
    "PrimaryOwner:    $($System.PrimaryOwnerName)"
    "Make/Model:      $($System.Manufacturer) ($($System.Model))"  #     "ComputerModel:  $((Get-WmiObject -Class:Win32_ComputerSystem).Model)"
    "SerialNumber:    $((Get-WmiObject -Class:Win32_BIOS).SerialNumber)"
    "PowerShell:      $($PSVersionTable.PSVersion)"
    "Windows Version: $($PSVersionTable.BuildVersion),   Windows ReleaseId: $((Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name 'ReleaseId').ReleaseId)"
    "Display Card:    $((Get-WmiObject -Class:Win32_VideoController).Name)"
    "Display Driver:  $((Get-WmiObject -Class:Win32_VideoController).DriverVersion),   Description: $((Get-WmiObject -Class:Win32_VideoController).VideoModeDescription)"
    "Last Boot Time:  $([Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem | select 'LastBootUpTime').LastBootUpTime)),   Uptime: $uptime_string"
    $IPDefaultAddress = @(Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DefaultIpGateway})[0].IPAddress[0]
    $IPDefaultGateway = @(Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DefaultIpGateway})[0].DefaultIPGateway[0]
    "Default IP:      $IPDefaultAddress / $IPDefaultGateway"
    Get-Netipaddress | where AddressFamily -eq IPv4 | select IPAddress,InterfaceIndex,InterfaceAlias | sort InterfaceIndex
    ""
    Wait-Job $job_cpu         | Out-Null ; $job_cpu_out = Receive-Job -Job $job_cpu
    Wait-Job $job_cpu_cores   | Out-Null ; $job_cpu_cores_out = Receive-Job -Job $job_cpu_cores 
    Wait-Job $job_cpu_logical | Out-Null ; $job_cpu_logical_out = Receive-Job -Job $job_cpu_logical
    "CPU:   $job_cpu_out"
    "CPU Cores:   $job_cpu_cores_out,      CPU Logical Cores:   $job_cpu_logical_out"
    # Get-PSDrive | sort -Descending Free | Format-Table
    gwmi win32_logicaldisk | Format-Table DeviceId, VolumeName, @{n="Size(GB)";e={[math]::Round($_.Size/1GB,2)}},@{n="Free(GB)";e={[math]::Round($_.FreeSpace/1GB,2)}}
    gwmi win32_winsat | select-object CPUScore,D3DScore,DiskScore,GraphicsScore,MemoryScore,WinSPRLevel | ft   # removed ,WinSATAssessmentState
    get-WmiObject -class Win32_Share | ft
}

没有理由从头开始做这种事情。您正在做的事情有许多现有的脚本。通过微软 powershellgallery.com。

PowerShell Script For Desktop Inventory Basic script to collect desktop inventory.

PowerShell Hardware Inventory Script Scenario:PowerShell hardware Inventory Script. Have you ever wanted to have an inventory without the hassle of going to each finding the information needed to fill the information for your inventory? It is important to keep your inventory up to date. Every time there is a change y

DownloadGet-Inventory.ps1

您可以只获取脚本并在 PowerShell 远程会话中使用 Invoke-Command(在本地和远程计算机上运行命令。)获取远程计算机信息。