当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)

作者:精品下载站 日期:2024-12-14 07:23:59 浏览:15 分类:玩电脑

Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)


在我之前的一篇博文 PowerShell:通过在所有域计算机上运行 systeminfo 记录您的环境中,我描述了如何通过运行 systeminfo 列出所有域计算机的重要系统信息。现在我想将它粘贴到一个函数中。该函数的名称为 Get-SystemInfo。真是一个惊喜。我真的是一个有创造力的人。 ? 就是这样。

目标

该方法是从所有域计算机和/或域服务器或从一台或多台计算机收集所有相关操作系统信息。它应该看起来像这样:

[玩转系统] Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)

[玩转系统] Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)

如前所述,我已经在之前的博客文章中准备了一些内容。现在我想更进一步。

获取系统信息

参数

Get-SystemInfo 使您能够定义范围或计算机名称。按 CTRL+空格键会列出所有选项。其中有两个特别有趣。

[玩转系统] Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)

仔细看看参数……

好吧,我不得不承认我不是一个非常关心错误和错误条件的专业开发人员。但我尝试提供一些帮助。注意scope参数的描述。

[玩转系统] Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)

或者只需键入 Get-SytemInfo -Scope,然后按 CTRL+空格键。

[玩转系统] Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)

这让我进入下一部分:使用 Get-SystemInfo。记住参数值。现在他们开始发挥作用……并采取行动……

Get-SystemInfo 实际应用:定义范围

查询所有服务器

[玩转系统] Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)

查询所有域控制器

[玩转系统] Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)

查询所有客户端计算机(WinRM 必须启用,在所有客户端操作系统上默认禁用)

[玩转系统] Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)

要通过组策略启用 WinRM,请参阅:

组策略:为 Windows 客户端操作系统(Windows 10、Windows 8、Windows 7)启用 WinRM

查询所有计算机

[玩转系统] Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)

你喜欢它?继续走吧,这不是终点……

Get-SystemInfo 实际操作:定义计算机名称

仅在一台计算机上运行

[玩转系统] Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)

在多台计算机上运行(用逗号分隔)

[玩转系统] Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)

这让我想到了这篇博文的最后一部分。其实,最重要的一点。函数本身。

功能

确保在域控制器或安装了 RSAT 的计算机上运行代码。

function Get-SystemInfo {
param
(
[parameter(HelpMessage='Enter the following values: LocalHost, ServerOnly, DCOnly, ClientOnly or AllComputer')]
[ValidateSet('LocalHost', 'ServerOnly', 'DCOnly', 'ClientOnly', 'AllComputer')]
$Scope, $ComputerName
)
$header='Host Name','OS','Version','Manufacturer','Configuration','Build Type','Registered Owner','Registered Organization','Product ID','Install Date','Boot Time','System Manufacturer','Model','Type','Processor','Bios','Windows Directory','System Directory','Boot Device','Language','Keyboard','Time Zone','Total Physical Memory','Available Physical Memory','Virtual Memory','Virtual Memory Available','Virtual Memory in Use','Page File','Domain','Logon Server','Hotfix','Network Card','Hyper-V'
If ($Scope -eq 'ServerOnly') {
Invoke-Command -ComputerName (Get-ADComputer -Filter {operatingsystem -like '*server*'}).Name {systeminfo /FO CSV | Select-Object -Skip 1} -ErrorAction SilentlyContinue | ConvertFrom-Csv -Header $header | Out-GridView
}
elseif ($Scope -eq 'DCOnly') {
Invoke-Command -ComputerName (Get-ADDomainController -Filter *).Name {systeminfo /FO CSV | Select-Object -Skip 1} -ErrorAction SilentlyContinue | ConvertFrom-Csv -Header $header | Out-GridView
}
elseif ($Scope -eq 'LocalHost')
{systeminfo /FO CSV | Select-Object -Skip 1 | ConvertFrom-Csv -Header $header | Out-GridView
}
elseif ($Scope -eq 'ClientOnly') {
Invoke-Command -ComputerName (Get-ADComputer -Filter {operatingsystem -notlike '*server*'}).Name {systeminfo /FO CSV | Select-Object -Skip 1} -ErrorAction SilentlyContinue | ConvertFrom-Csv -Header $header | Out-GridView
}
elseif ($Scope -eq 'AllComputer') {
Invoke-Command -ComputerName (Get-ADComputer -Filter *).Name {systeminfo /FO CSV | Select-Object -Skip 1} -ErrorAction SilentlyContinue | ConvertFrom-Csv -Header $header | Out-GridView
}
elseif ($ComputerName) {
Invoke-Command -ComputerName $ComputerName {systeminfo /FO CSV | Select-Object -Skip 1} -ErrorAction SilentlyContinue | ConvertFrom-Csv -Header $header | Out-GridView
}
}

剧本随着时间的推移而不断发展。我的一位追随者向我指出,使用 switch 而不是大量的 If 和 elseif 会更好。谢谢你的建议。这是带开关的功能。这样我们就可以节省一些线路。


function Get-SystemInfo {
param
(
[parameter(HelpMessage='Enter the following values: LocalHost, ServerOnly, DCOnly, ClientOnly or AllComputer')]
[ValidateSet('LocalHost', 'ServerOnly', 'DCOnly', 'ClientOnly', 'AllComputer')]
$Scope, $ComputerName
)
$header='Host Name','OS','Version','Manufacturer','Configuration','Build Type','Registered Owner','Registered Organization','Product ID','Install Date','Boot Time','System Manufacturer','Model','Type','Processor','Bios','Windows Directory','System Directory','Boot Device','Language','Keyboard','Time Zone','Total Physical Memory','Available Physical Memory','Virtual Memory','Virtual Memory Available','Virtual Memory in Use','Page File','Domain','Logon Server','Hotfix','Network Card','Hyper-V'
switch ($Scope)
{
'ServerOnly' {Invoke-Command -ComputerName (Get-ADComputer -Filter {operatingsystem -like '*server*'}).Name {systeminfo /FO CSV | Select-Object -Skip 1} -ErrorAction SilentlyContinue | ConvertFrom-Csv -Header $header | Out-GridView}
'DCOnly' {Invoke-Command -ComputerName (Get-ADDomainController -Filter *).Name {systeminfo /FO CSV | Select-Object -Skip 1} -ErrorAction SilentlyContinue | ConvertFrom-Csv -Header $header | Out-GridView}
'LocalHost' {systeminfo /FO CSV | Select-Object -Skip 1 | ConvertFrom-Csv -Header $header | Out-GridView}
'ClientOnly' {Invoke-Command -ComputerName (Get-ADComputer -Filter {operatingsystem -notlike '*server*'}).Name {systeminfo /FO CSV | Select-Object -Skip 1} -ErrorAction SilentlyContinue | ConvertFrom-Csv -Header $header | Out-GridView}
'AllComputer' {Invoke-Command -ComputerName (Get-ADComputer -Filter *).Name {systeminfo /FO CSV | Select-Object -Skip 1} -ErrorAction SilentlyContinue | ConvertFrom-Csv -Header $header | Out-GridView}
}
If ($ComputerName) {
Invoke-Command -ComputerName $ComputerName {systeminfo /FO CSV | Select-Object -Skip 1} -ErrorAction SilentlyContinue | ConvertFrom-Csv -Header $header | Out-GridView
}
}

使其永久化

如果您喜欢我的方法,请打开 PowerShell ISE。将该函数复制到您的 ISE 会话。在 C:\Program Files\Windows PowerShell\Modules 中创建一个文件夹,并将代码保存为 psm1 文件。确保您的文件名和文件夹名称匹配。

[玩转系统] Get-SystemInfo:列出所有计算机的系统配置 (systeminfo)

关闭 PowerShell。再次打开 PowerShell。该命令现在可供所有用户使用。享受 Get-SystemInfo 带来的乐趣!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯