访问管道powershell中的第一个对象

访问管道powershell中的第一个对象,powershell,powershell-2.0,pass-by-value,Powershell,Powershell 2.0,Pass By Value,我在Powershell中创建了两个函数,第一个函数获取注册表项,如果存在,则返回computername和boolan,第二个函数获取已安装windows更新的列表,并验证是否存在某些更新。。。。并返回computername和boolan allso。。 现在的问题是,当我试图访问管道中的第一个对象时,我得到了null Function Get-RegKey { <# .SYNOPSIS "By OhadH 2012" .DESCRIPTION Read Key Fro

我在Powershell中创建了两个函数,第一个函数获取注册表项,如果存在,则返回computername和boolan,第二个函数获取已安装windows更新的列表,并验证是否存在某些更新。。。。并返回computername和boolan allso。。 现在的问题是,当我试图访问管道中的第一个对象时,我得到了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{ param( [参数(必需=$true,位置=0,ValueFromPipeline=$true)][String]$strMachine, [参数(必需=$true,位置=1)][字符串]$Location, [参数(必需=$true,位置=2)][字符串]$strValue, [参数(必需=$true,位置=3)][String]$strValueToSearch ) 开始{$obj=新对象psobject} 过程{ 试一试{ $obj |添加成员NoteProperty'strMachine'$strMachine-Force $reg=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$strMachine) $regKey=$reg.OpenSubKey($Location,$true) $REGRAD=$regKey.GetValue($strValue) 如果($RegRead-eq$strValueToSearch){$obj |添加成员NoteProperty'RegExist'$true-Force} else{$obj|添加成员NoteProperty'RegExist'$false-Force} } catch[System.Exception]{$obj |添加成员NoteProperty'RegExist'!!错误!!“-Force} } 结束{return$obj} } 函数集RegKey{ param( [参数(必需=$true,位置=0,ValueFromPipeline=$true)][String]$strMachine, [参数(必需=$true,位置=1)][字符串]$Location, [参数(必需=$true,位置=2)][字符串]$strValue, [参数(必需=$true,位置=3)][字符串]$strValueToSet ) 开始{$obj=新对象psobject} 过程{ 试一试{ $obj |添加成员NoteProperty'strMachine'$strMachine-Force $reg=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$strMachine) $reg.CreateSubKey($Location)|输出空值 $regKey=$reg.OpenSubKey($Location,$true) $regKey.SetValue($strValue,$strValueToSet,“String”) } catch[System.Exception]{} } 结束{} } 函数获取WindowsUpdate{ param( [参数(必需=$true,位置=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][String]$strMachine, [参数(必需=$true,位置=1,ValueFromPipeline=$true)][String]$strUpdate ) 开始{$obj=新对象psobject} 过程{ 试一试{ $obj |添加成员NoteProperty'strMachine'$strMachine-Force 如果((gwmi-ComputerName$strMachine Win32_QuickFixEngineering |?{$\.hotfix-like$strUpdate+“*”})-ne$null) {$obj |添加成员NoteProperty'KBInstalled'$true-Force} else{$obj|添加成员NoteProperty'KBInstalled'$false-Force} } catch[System.Exception]{$obj |添加成员NoteProperty'KBInstalled'!!错误!!“-Force} } 结束{return$obj} } $subnet='10.0.0' 对于($i=1;$i-le 250;$i++){ $cntIP=$subnet+$i if($cntIP=($cntIP | Ping Host-icmp | where{$\响应}).IPAddress){ 设置注册表项-strMachine$cntIP“软件\\MyKey”-strValue“类型”-strValueToSet“服务器” 获取RegKey-strMachine$cntIP“SOFTWARE\\MyKey”-strValue“Type”-strValueToSearch“Server”|获取WindowsUpdate-strUpdate“KB2633952”|选择strMachine,**@{N=“RegExist”;E={$\ RegExist},**KBInstalled } }
感谢您的帮助Ohad

如果我遵循,您需要将Get RegKey管道连接到Foreach对象,并在Foreach内部保存对所需对象的引用。如果您有一个由三个命令组成的管道(例如get something | do something | set something),那么管道中的第三个cmdlet(例如set something)无法从上游cmdlet(例如get something)访问对象。

这一切的哪一部分给您带来了问题?预期的行为是什么?我如何尝试访问管道中的第一个元素<代码>获取RegKey-strMachine$cntIP“SOFTWARE\\MyKey”-strValue“Type”-strValueToSearch“Server”|获取WindowsUpdate-strUpdate“KB2633952”|选择strMachine,@{N=“RegExist”;E={$\ RegExist},KBInstalled我想从第一个管道获取RegExist值。如果我一个接一个地得到函数,我可以访问所有属性。。。但是在管道中,我无法访问RegExistP.S中的值:RegExist是一个获取RegKey return的值。你是指类似的东西吗<代码>PS C:\>获取RegKey-strMachine$cntIP“SOFTWARE\\MyKey”-strValue“Type”-strValueToSearch“Server”{Get-WindowsUpdate$\uu.strMachine-strUpdate“KB2633952”};选择strMachine,@{N=“RegExist”;E={$.RegExist},KBInstalled。。。附言:你的博客很棒……托达·奥哈德:)。是的,正是这样,在foreach内部,您可以将当前对象分配给一个变量,然后在代码中使用它。我重新思考了以最“Powershell”的方式汇集值的方法,我发现:)
Get RegKey-strMachine$cntIP“SOFTWARE\\MyKey”-strValue“Type”-strValueToSearch“Server”|选择strMachine,@{N=“KBInstalled”;E={(获取WindowsUpdate-strMachine$\.strMachine-strUpdate“KB2633952”).KBInstalled},RegExist
感谢您的帮助。。。