Доступ Первый объект в трубе PowerShell - PullRequest
0 голосов
/ 13 февраля 2012

я создал две функции в Powershell, первая функция получает ключ реестра, если существует, возвращает computername и boolan, вторая получает список установленных обновлений Windows и проверяет, существует ли какое-либо обновление .... и возвращает имя компьютера и boolan.allso .. теперь проблема в том, что когда я пытаюсь получить доступ к 1-му объекту в трубе, я получаю нулевое значение ...

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
 }
}

Спасибо за помощь Ohad

1 Ответ

0 голосов
/ 13 февраля 2012

Если я последую, вам нужно передать Get-RegKey в Foreach-Object и внутри foreach сохранить ссылку на нужный объект. Если у вас есть конвейер, состоящий из трех команд (например, получить что-то | сделать-что-то | установить-что-то), то третий командлет в конвейере (например, набор-что-то) не может получить доступ к объекту из вышестоящего командлета (например, получить-что-то ).

...