首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >powershell v7 -函数管道问题

powershell v7 -函数管道问题
EN

Stack Overflow用户
提问于 2021-03-08 12:32:38
回答 2查看 41关注 0票数 0

我试图编写两个函数:

  • 第一个(Get- Lab )检索Lab对象
  • ,第二个( remove -Lab)删除Lab

实验室是在我的模块中定义的一个类。

当运行Get-Lab时,我用正确的类型正确地检索了我的实验室实例:

当我运行Remove-Lab -Lab (Get Mylab)时,正确地执行了以下操作:

但是当我试图通过管道传递Lab对象时,它失败了。

函数不会通过管道接收对象。但是,我已经将-Lab参数设置为ValueFromPipeline=$true必须使用的参数。

代码语言:javascript
复制
Function Remove-Lab{
[CmdletBinding(DefaultParameterSetName='Lab')]
param (
    [Parameter(ValueFromPipeline=$true,ParameterSetName='Lab',Position=0,Mandatory=$true)]
    [Lab]
    $Lab,
    
    # Parameter help description
    [Parameter(Position=1,Mandatory=$false)]
    [switch]
    $Force=$false
)

begin {
    Write-host ("`tLabName : {0}" -f $Lab.Name) -ForegroundColor Yellow

    if ($null -ne $Lab) {
        $LabToRemove = $Lab
    }

    if (-not [string]::IsNullOrEmpty($LabId)) {
        $LabToRemove = Get-Lab -Id $LabId
    }

    if (-not [string]::IsNullOrEmpty($Name)) {
        $LabToRemove = Get-Lab -Name $Name
    }

    if ($null -eq $LabToRemove) {
        throw "There is no Lab with specified characteristics. Please check your input"
    }
}

process {
    $DoRemoval = $true
    if ($Force.IsPresent -eq $false) {
        while ($null -eq $UserInput -or $UserInput -notin @('Y','N')) {
            $UserInput = Read-HostDefault -Prompt "Are you sure want to remove the selected Lab and all its components ? [Y]es, [N]o" -Default 'N'

            if ($UserInput -eq 'N') {
                $DoRemoval = $false
            }
        }

        Write-Host ("`tUser Input : {0}" -f $UserInput) -ForegroundColor Green
    }

    if ($DoRemoval -eq $true) {
        Write-Host ("`tAbout to Remove the following Lab : {0}" -f $LabToRemove.Name) -ForegroundColor Green
    }
}

end {

}

}

如下面所示,调试此函数时,$Lab参数为null。

你对这件事有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-08 13:07:38

begin运行在其他任何东西之前,包括管道参数绑定所以您需要将检查管道绑定参数(如$Lab)的代码移到process块:

代码语言:javascript
复制
Function Remove-Lab{
[CmdletBinding(DefaultParameterSetName='Lab')]
param (
    [Parameter(ValueFromPipeline=$true,ParameterSetName='Lab',Position=0,Mandatory=$true)]
    [Lab]
    $Lab,
    
    # Parameter help description
    [Parameter(Position=1,Mandatory=$false)]
    [switch]
    $Force=$false
)

process {
    Write-host ("`tLabName : {0}" -f $Lab.Name) -ForegroundColor Yellow

    if ($null -ne $Lab) {
        $LabToRemove = $Lab
    }

    if (-not [string]::IsNullOrEmpty($LabId)) {
        $LabToRemove = Get-Lab -Id $LabId
    }

    if (-not [string]::IsNullOrEmpty($Name)) {
        $LabToRemove = Get-Lab -Name $Name
    }

    if ($null -eq $LabToRemove) {
        throw "There is no Lab with specified characteristics. Please check your input"
    }

    $DoRemoval = $true
    if ($Force.IsPresent -eq $false) {
        while ($null -eq $UserInput -or $UserInput -notin @('Y','N')) {
            $UserInput = Read-HostDefault -Prompt "Are you sure want to remove the selected Lab and all its components ? [Y]es, [N]o" -Default 'N'

            if ($UserInput -eq 'N') {
                $DoRemoval = $false
            }
        }

        Write-Host ("`tUser Input : {0}" -f $UserInput) -ForegroundColor Green
    }

    if ($DoRemoval -eq $true) {
        Write-Host ("`tAbout to Remove the following Lab : {0}" -f $LabToRemove.Name) -ForegroundColor Green
    }
}
票数 0
EN

Stack Overflow用户

发布于 2021-03-08 12:50:55

由于该函数是在$LabId$Name上测试的,因此这些变量需要存在于函数中,而此时它们不存在。尝试将参数更改为:

代码语言:javascript
复制
[CmdletBinding(DefaultParameterSetName='LabId')]
param (
    [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName = $true, ParameterSetName='LabId',Position=0,Mandatory=$true)]
    [string]$LabId,

    [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName = $true, ParameterSetName='LabName',Position=0,Mandatory=$true)]
    [string]$Name,

    # Parameter help description
    [switch]$Force  # no need to set a switch to $false because if you don't send that param, the undelying value will be $false by default
)

然后移除

代码语言:javascript
复制
Write-host ("`tLabName : {0}" -f $Lab.Name) -ForegroundColor Yellow

if ($null -ne $Lab) {
    $LabToRemove = $Lab
}

这里的重要部分是ValueFromPipelineByPropertyName = $true声明

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66529940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档