的第一個對象我在PowerShell中創建了兩個功能,第一個功能函數得到一個註冊表項,如果存在,返回計算機名和boolan,第二個獲得安裝Windows更新的列表,如果一些更新存在驗證。 ...並返回計算機名和boolan allso .. 現在的問題是,我煥嘗試進入管道中我得到空的第一個對象...訪問在管道的PowerShell
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
}
}
感謝您的幫助轄
所有這些的哪一部分給你帶來問題?預期的行爲是什麼? – 2012-02-13 13:13:55
whan我嘗試訪問管道中的第一個元素...'Get-RegKey -strMachine $ cntIP「SOFTWARE \\ MyKey」-strValue「Type」-strValueToSearch「Server」| Get-WindowsUpdate -strUpdate「KB2633952」|選擇strMachine,@ {N =「RegExist」; E = {$ _。RegExist}},KBInstalled'我想從第一個管道獲取** RegExist **值。如果我逐個獲取函數,我可以訪問所有屬性......但在管道中,我無法訪問** RegExist **中的值** – OhadH 2012-02-13 13:22:09
PS:** RegExist **是Get-RegKey返回的值 – OhadH 2012-02-13 13:31:49