2012-02-13 104 views
0

的第一個對象我在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 
} 
} 

感謝您的幫助轄

+0

所有這些的哪一部分給你帶來問題?預期的行爲是什麼? – 2012-02-13 13:13:55

+0

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

+0

PS:** RegExist **是Get-RegKey返回的值 – OhadH 2012-02-13 13:31:49

回答

0

如果我遵循,您需要將Get-RegKey傳遞給Foreach-Object,並在foreach中保存對所需對象的引用。如果你有一個由三個命令組成的管道(例如get-something | do-something | set-something),那麼管道中的第三個cmdlet(例如set-something)不能從上游cmdlet訪問對象(例如get-something )。

+0

你的意思是這樣的嗎? 'PS C:\> Get-RegKey -strMachine $ cntIP「SOFTWARE \\ MyKey」-strValue「Type」-strValueToSearch「Server」| %{Get-WindowsUpdate $ _。strMachine -strUpdate「KB2633952」} |選擇strMachine,@ {N = 「RegExist」; E = {$ _ RegExist}},KBInstalled' ... P.S:你有一個偉大的博客...... – OhadH 2012-02-13 14:02:13

+0

戶轄:)。是的,就像這樣,在foreach中你可以將當前對象分配給一個變量並在代碼中稍後使用它。 – 2012-02-13 14:27:16

+0

我做了一個反思,以最「Powershell」方式彙集價值的方式,這是我發現的:) 'Get-RegKey -strMachine $ cntIP「SOFTWARE \\ MyKey」-strValue「Type」-strValueToSearch「服務器「|選擇strMachine,@ {N =「KBInstalled」; E = {(Get-WindowsUpdate -strMachine $ _。strMachine -strUpdate「KB2633952」)。KBInstalled}},RegExist' 謝謝你的幫助... – OhadH 2012-02-13 15:27:50