【发布时间】:2012-02-13 13:01:44
【问题描述】:
我在 Powershell 中创建了两个函数,第一个函数函数获取注册表项,如果存在则返回计算机名和布尔值,第二个函数获取已安装的 Windows 更新列表并验证是否存在某些更新......并返回计算机名和布尔值还有.. 现在的问题是,当我尝试访问管道中的第一个对象时,我得到了 null...
Function Get-RegKey {
<#
.SYNOPSIS
"By OhadH 2012"
.DESCRIPTION
Read Key From Registry and create it if not exist
.PARAMETER LiteralPath
.EXAMPLE
Get-RegKey -strMachine "127.0.0.1" -Location "Software\\MyKey" -strValue "Type" -strValueToSearch "Server"
Read The Registry key from "HKLM\Software\MyKey" the value Type and search for the value "Server"
.EXAMPLE
Get-RegKey "127.0.0.1" "Software\\MyKey" "Type" "Server"
Read The Registry key from "HKLM\Software\MyKey" the value Type and search for the value "Server"
By Postion and not by value name
.NOTES
Author: OhadH
Date: Feb 09, 2012
#>
param (
[parameter(Mandatory = $true,Position=0,ValueFromPipeline=$true)][String]$strMachine,
[parameter(Mandatory = $true,Position=1)][String]$Location,
[parameter(Mandatory = $true,Position=2)][String]$strValue,
[parameter(Mandatory = $true,Position=3)][String]$strValueToSearch
)
begin { $obj = New-Object psobject }
process {
try {
$obj | Add-Member NoteProperty 'strMachine' $strMachine -Force
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $strMachine)
$regKey = $reg.OpenSubKey($Location,$true)
$RegRead = $regKey.GetValue($strValue)
if ($RegRead -eq $strValueToSearch) { $obj | Add-Member NoteProperty 'RegExist' $true -Force }
else { $obj | Add-Member NoteProperty 'RegExist' $false -Force }
}
catch [System.Exception] { $obj | Add-Member NoteProperty 'RegExist' "!!Error!!" -Force }
}
end { return $obj }
}
Function Set-RegKey {
<#
.SYNOPSIS
"By OhadH"
.DESCRIPTION
Create Registry Key
.PARAMETER LiteralPath
.EXAMPLE
Set-RegKey -strMachine "127.0.0.1" -Location "Software\\MyKey" -strValue "Type" -strValueToSet "Server"
Read The Registry key from "HKLM\Software\MyKey" the value Type and search for the value "Server"
.EXAMPLE
Get-RegKey "127.0.0.1" "Software\\MyKey" "Type" "Server"
Read The Registry key from "HKLM\Software\MyKey" the value Type and search for the value "Server"
By Postion and not by value name
.NOTES
Author: OhadH
Date: Feb 09, 2012
#>
param (
[parameter(Mandatory = $true,Position=0,ValueFromPipeline=$true)][String]$strMachine,
[parameter(Mandatory = $true,Position=1)][String]$Location,
[parameter(Mandatory = $true,Position=2)][String]$strValue,
[parameter(Mandatory = $true,Position=3)][String]$strValueToSet
)
begin { $obj = New-Object psobject }
process {
try {
$obj | Add-Member NoteProperty 'strMachine' $strMachine -Force
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $strMachine)
$reg.CreateSubKey($Location) | Out-Null
$regKey = $reg.OpenSubKey($Location,$true)
$regKey.SetValue($strValue,$strValueToSet,"String")
}
catch [System.Exception] { }
}
end { }
}
Function Get-WindowsUpdate {
<#
.SYNOPSIS
"By OhadH"
.DESCRIPTION
Get specific Installed KB
.PARAMETER LiteralPath
.EXAMPLE
Get-WindowsUpdate -strMachine 127.0.0.1 -strUpdate "KB2633952"
Get if KB2633952* installed on local machine
.EXAMPLE
Get-WindowsUpdate 127.0.0.1 "KB2633952"
Get if KB2633952* installed on local machine
.NOTES
Author: OhadH
Date: Feb 09, 2012
#>
param (
[parameter(Mandatory = $true,Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][String]$strMachine,
[parameter(Mandatory = $true,Position=1,ValueFromPipeline=$true)][String]$strUpdate
)
begin { $obj = New-Object psobject }
process {
try {
$obj | Add-Member NoteProperty 'strMachine' $strMachine -Force
if ((gwmi -ComputerName $strMachine Win32_QuickFixEngineering | ? {$_.HotFixID -like $strUpdate+"*"}) -ne $null)
{ $obj | Add-Member NoteProperty 'KBInstalled' $true -Force }
else { $obj | Add-Member NoteProperty 'KBInstalled' $false -Force }
}
catch [System.Exception] { $obj | Add-Member NoteProperty 'KBInstalled' "!!Error!!" -Force }
}
end { return $obj }
}
$subnet = '10.0.0.'
for ($i=1; $i -le 250; $i++) {
$cntIP = $subnet + $i
if ($cntIP = ($cntIP | Ping-Host -icmp | where { $_.Responding }).IPAddress) {
Set-RegKey -strMachine $cntIP "SOFTWARE\\MyKey" -strValue "Type" -strValueToSet "Server"
Get-RegKey -strMachine $cntIP "SOFTWARE\\MyKey" -strValue "Type" -strValueToSearch "Server" | Get-WindowsUpdate -strUpdate "KB2633952" | select strMachine,**@{N="RegExist";E={$_.RegExist}},**KBInstalled
}
}
感谢奥哈德的帮助
【问题讨论】:
-
这一切的哪一部分给你带来了问题?预期的行为是什么?
-
当我尝试访问管道中的第一个元素时...
Get-RegKey -strMachine $cntIP "SOFTWARE\\MyKey" -strValue "Type" -strValueToSearch "Server" | Get-WindowsUpdate -strUpdate "KB2633952" | select strMachine,@{N="RegExist";E={$_.RegExist}},KBInstalled我想从第一个管道中获取 RegExist 值。如果我一一获得功能,我可以访问所有属性...但是在管道中我无法访问 RegExist 中的值 -
P.S: RegExist 是 Get-RegKey 返回的值
标签: powershell powershell-2.0 pass-by-value