英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论