【问题标题】:Access The 1st object in the pipe powershell访问管道 powershell 中的第一个对象
【发布时间】: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


【解决方案1】:

如果我遵循,您需要通过管道将 Get-RegKey 传递给 Foreach-Object,并在 foreach 中保存对所需对象的引用。如果您的管道由三个命令组成(例如 get-something | do-something | set-something),则管道中的第三个 cmdlet(例如 set-something)无法从上游 cmdlet 访问对象(例如 get-something )。

【讨论】:

  • 你的意思是这样的吗? PS C:\&gt; Get-RegKey -strMachine $cntIP "SOFTWARE\\MyKey" -strValue "Type" -strValueToSearch "Server" | % { Get- WindowsUpdate $_.strMachine -strUpdate "KB2633952"} | select strMachine,@{N="RegExist";E={$_.RegExist}},KBInstalled... PS:你的博客很棒...
  • 户田奥哈德 :)。是的,完全一样,在 foreach 中,您可以将当前对象分配给一个变量,然后在代码中使用它。
  • 我重新思考了如何以最“Powershell”的方式汇集价值,这就是我发现的:) Get-RegKey -strMachine $cntIP "SOFTWARE\\MyKey" -strValue "Type" -strValueToSearch "Server" | select strMachine,@{N="KBInstalled";E={(Get-WindowsUpdate -strMachine $_.strMachine -strUpdate "KB2633952").KBInstalled}},RegExist 谢谢你的帮助...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-30
  • 2021-06-09
  • 1970-01-01
  • 2017-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多