Powershell在远程计算机上查找RAM信息,并将零件号保存到不同的变量中



我有一个简单的脚本,可以从远程计算机中提取RAM部件号并在谷歌上搜索它。但它没有按预期工作。如果远程计算机中只安装了 1 个 RAM 模块,它工作得很好 谷歌打开零件号的搜索结果,耶!

如果远程计算机中安装了 1 个以上的 RAM 模块,则会在 Google 中搜索变量中的第一个部件号。第 2 个、第 3 个、第 4 个部件号将作为地址输入到 Chrome 选项卡 2、3、4 中。

如何让 Chrome 通过谷歌搜索所有零件编号?

我的脚本:

$ComputerName = Read-Host "Write Computer Name"
Get-WmiObject Win32_PhysicalMemory -computername $ComputerName
$ToChrome = Read-Host 'Do you want to search Google for the Partnumber(s)? Y Or N'
if ($ToChrome -eq 'Y') {$Partnumber = Get-WmiObject Win32_PhysicalMemory -computername $ComputerName | select -expandproperty Partnumber
Start-Process "chrome.exe" ("https://www.google.com/search?q=$Partnumber")}
if ($ToChrome -eq 'n') {Continue}

这是因为chrome.exe将部件号之间的空格解释为新地址。

我冒昧地用try&catch,日志文件输出和计算机名作为参数来拉皮条脚本,以便您可以将其称为Get-MemoryPropertyAndSearchWithGoogle.ps1 -ComputerName ComputerName1

对于我的测试,我使用了设备定位器属性,因为我的零件号为空。

#Get-MemoryPropertyAndSearchWithGoogle.ps1
Param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string]$ComputerName
)
$ErrorPreference='Stop'
$ErrorActionPreference='Stop'
$LogFilePath = "C:Temp$((Get-Date).ToString("yyyy-MM-dd"))$($ComputerName)Get-MemoryPropertyAndSearchWithGoogle.log"
[string]$LogFileString = ""
#$Property = "PartNumber"
$Property = "DeviceLocator"
$ErrorExists = $false
$ComputerMemoryObjects = @()
try
{
$ComputerMemoryObjects = Get-WmiObject Win32_PhysicalMemory -ComputerName $ComputerName -Property *
$LogFileString += "$((Get-Date).ToString("yyyy-MM-dd_HH:mm:ss"))#INF#Get-WmiObject Win32_PhysicalMemory -ComputerName $($ComputerName)`n"
}
catch
{
$LogFileString += "$((Get-Date).ToString("yyyy-MM-dd_HH:mm:ss"))#ERR#$($error[0].exception.message)`n"
$ErrorExists = $true
}
[string]$SearchString = ""
foreach ($SingleComputerMemoryObject in $ComputerMemoryObjects) 
{
if ($SearchString)
{
$SearchString += "+OR+"
}
$SearchString += "$($SingleComputerMemoryObject.$Property)"
}
$ToChrome = Read-Host 'Do you want to search Google for the Partnumber(s)? Y Or N'
if ($ToChrome -eq 'Y') 
{
if ($SearchString)
{
try
{
Start-Process "chrome.exe" ("https://www.google.com/search?q=$($SearchString)")
$LogFileString += "$((Get-Date).ToString("yyyy-MM-dd_HH:mm:ss"))#INF#chrome.exe started with searchstring:`"$($SearchString)`"`n"
}
catch
{
$LogFileString += "$((Get-Date).ToString("yyyy-MM-dd_HH:mm:ss"))#ERR#$($error[0].exception.message)`n"
$ErrorExists = $true
}
}
else
{
$LogFileString += "$((Get-Date).ToString("yyyy-MM-dd_HH:mm:ss"))#INF#`$SearchString is empty`n"
}
}
if (!($ErrorExists))
{
$LogFileString += "$((Get-Date).ToString("yyyy-MM-dd_HH:mm:ss"))#INF#ScriptCompletedWithoutErrors`n"
}
$LogFileString | Out-File $LogFilePath
$LogFileString

您可以从Get-WmiObject获得多个对象。如果你想为它们每个人做点什么,你需要一个循环。

此外,将放入 URL 的内容进行 URL 编码是一个好主意。 也许把它放在双引号里不会有什么坏处。

Add-Type -AssemblyName System.Web   # for [System.Web.HttpUtility]::UrlEncode()
$ComputerName = Read-Host "Write Computer Name"
$installed_memory = Get-WmiObject Win32_PhysicalMemory -ComputerName $ComputerName | Select-Object Manufacturer,PartNumber,SerialNumber,DeviceLocator,Capacity
$installed_memory | Format-Table -AutoSize
$ToChrome = Read-Host 'Do you want to search Google for the Partnumber(s)? Y Or N'
if ($ToChrome -eq 'Y') {
$unique_numbers = $installed_memory.Partnumber.Trim() | Sort-Object -Unique
foreach ($number in $unique_numbers) {
$query = [System.Web.HttpUtility]::UrlEncode('"' + $number + '"')        
Start-Process chrome.exe "https://www.google.com/search?q=$query"
}
}

Powershell有一个方便的功能:当你有一个对象数组时,你可以一次性从所有这些对象中查询嵌套属性。

例如,如果$installed_memory中有 4 个Win32_PhysicalMemory对象,则

$installed_memory.Partnumber.Trim()

一步为您提供 4 个易于修剪的零件号。

最新更新