如何优化Get-ComputerInfo命令?

huangapple go评论58阅读模式
英文:

How can I optimize the command Get-ComputerInfo?

问题

每当我从这个命令中获取信息片段时,Windows将运行整个函数,这需要时间。脚本可以运行,但速度很慢。由于代码的其余部分编写方式,我无法将Get-ComputerInfo存储为字符串,因为我仍然需要从中提取字符串,而不使用子字符串的繁琐方式。在这里我有哪些选择?

我的代码示例如下:

# 获取用户名并格式化
$USRNAME = Get-ComputerInfo -Property WindowsRegisteredOwner | Out-String
$USRNAME = $USRNAME.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
$USRNAME = $USRNAME[2]

# 获取BIOS序列号并格式化
$BIOSSN = Get-ComputerInfo -Property BiosSeralNumber | Out-String
$BIOSSN = $BIOSSN.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
$BIOSSN = $BIOSSN[2]

# 获取BIOS制造商并格式化
$MANUFAC = Get-ComputerInfo -Property CsManufacturer | Out-String
$MANUFAC = $MANUFAC.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
$MANUFAC = $MANUFAC[2]

# 获取BIOS型号并格式化
$MODEL = Get-ComputerInfo -Property CsModel | Out-String
$MODEL = $MODEL.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
$MODEL = $MODEL[2..25]

如前所述,我尝试将其输出为字符串,但变得混乱。

英文:

Every time I get a snippet of info from this command, Windows will run the whole function, which takes time. The script works, but its slow. Because of the way the rest of this code is written, I cannot store Get-ComputerInfo as a string because I still need to pull strings from it without the tediousness of using substrings. What are my options here?

A sample of my code is here:

# Get username and format
$USRNAME = Get-ComputerInfo -Property WindowsRegisteredOwner | Out-String
$USRNAME = $USRNAME.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
$USRNAME = $USRNAME[2]


# Get BIOS S/N and format
$BIOSSN = Get-ComputerInfo -Property BiosSeralNumber | Out-String
$BIOSSN = $BIOSSN.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
$BIOSSN = $BIOSSN[2]


# Get BIOS Manufacturer and format
$MANUFAC = Get-ComputerInfo -Property CsManufacturer | Out-String
$MANUFAC = $MANUFAC.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
$MANUFAC = $MANUFAC[2]


# Get BIOS Model and format
$MODEL = Get-ComputerInfo -Property CsModel | Out-String
$MODEL = $MODEL.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
$MODEL = $MODEL[2..25]

As mentioned previously, I tried to output as a string and it got messy.

答案1

得分: 3

调用 Get-ComputerInfo 一次,将输出对象存储在变量中,最后重复使用它:

> 我仍然需要从中提取字符串,而不需要使用子字符串的麻烦

你确实不需要 - Get-ComputerInfo 输出的是一个对象,其中每个信息都被整齐地存储在单独的属性中 - 要获取 WindowsRegisteredOwner 的值,只需使用 . 成员访问运算符引用 WindowsRegisteredOwner 属性,其他属性也是同理:

$allInfo = Get-ComputerInfo -Property WindowsRegisteredOwner, BiosSeralNumber, CsManufacturer, CsModel

$USRNAME = $allInfo.WindowsRegisteredOwner
$BIOSSN  = $allInfo.BiosSeralNumber
$MANUFAC = $allInfo.CsManufacturer
$MODEL   = $allInfo.CsModel
英文:

Call Get-ComputerInfo once, the store the output object in a variable, and then finally reuse that:

> I still need to pull strings from it without the tediousness of using substrings

You really don't - Get-ComputerInfo outputs an object that has each piece of information neatly stored in separate properties - to get the WindowsRegisteredOwner value, simply use the . member access operator to dereference the WindowsRegisteredOwner property, and so on for the remaining properties:

$allInfo = Get-ComputerInfo -Property WindowsRegisteredOwner, BiosSeralNumber, CsManufacturer, CsModel

$USRNAME = $allInfo.WindowsRegisteredOwner
$BIOSSN  = $allInfo.BiosSeralNumber
$MANUFAC = $allInfo.CsManufacturer
$MODEL   = $allInfo.CsModel

huangapple
  • 本文由 发表于 2023年2月14日 00:37:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定