Powershell 函数的管道输入
我有以下用于创建HDInsight群集的工作代码:Powershell 函数的管道输入,powershell,pester,Powershell,Pester,我有以下用于创建HDInsight群集的工作代码: New-AzureHDInsightClusterConfig -ClusterSizeInNodes $ClusterNodes ` -VirtualNetworkId ($VNet.Id) ` -SubnetName $subnet
New-AzureHDInsightClusterConfig -ClusterSizeInNodes $ClusterNodes `
-VirtualNetworkId ($VNet.Id) `
-SubnetName $subnetName `
-ClusterType $ClusterType | `
Set-AzureHDInsightDefaultStorage -StorageAccountName "$StorageAccountName.blob.core.windows.net" `
-StorageAccountKey $StorageAccountKey `
-StorageContainerName $StorageContainerName | `
New-AzureHDInsightCluster -Credential $ClusterCreds `
-Location $Location `
-Name $ClusterName `
-Version $HDInsightVersion
请注意,我使用的是管道。
现在我想编写一些自动化测试(使用)来测试这段代码。为了做到这一点,我将cmdlet调用封装在我所说的代理函数中,并使用splatting传递参数值,这使得为了测试的目的很容易模拟它们。下面是一个例子:
function Set-AzureHDInsightDefaultStorageProxy{
<#
.SYNOPSIS
Wrap Azure cmdlet Set-AzureHDInsightDefaultStorage thus enabling mocking
.DESCRIPTION
Wrap Azure cmdlet Set-AzureHDInsightDefaultStorage thus enabling mocking
#>
[CmdletBinding()]
Param(
$StorageAccountName,
$StorageAccountKey,
$StorageContainerName
)
Set-AzureHDInsightDefaultStorage @PSBoundParameters
}
New-AzureHDInsightClusterConfigProxy -ClusterSizeInNodes $ClusterNodes `
-VirtualNetworkId ($VNet.Id) `
-SubnetName $subnetName `
-ClusterType $ClusterType | `
Set-AzureHDInsightDefaultStorageProxy -StorageAccountName "$StorageAccountName.blob.core.windows.net" `
-StorageAccountKey $StorageAccountKey `
-StorageContainerName $StorageContainerName | `
New-AzureHDInsightClusterProxy -Credential $ClusterCreds `
-Location $Location `
-Name $ClusterName `
-Version $HDInsightVersion
但这失败了,也犯了同样的错误
很明显,我缺乏Powershell技能/知识,这让我很失望,所以我希望有人能告诉我如何重写我的函数,以成功地接受和使用管道输入
以下是我正在为其编写代理函数的cmdlet的定义:。我注意到,-Config参数设置为允许管道输入:
所以我想我需要在我的代理函数中指定相同的参数,但我不知道怎么做。好的,我想我已经解决了:
function Set-AzureHDInsightDefaultStorageProxy{
<#
.SYNOPSIS
Wrap Azure cmdlet Set-AzureHDInsightDefaultStorage thus enabling mocking
.DESCRIPTION
Wrap Azure cmdlet Set-AzureHDInsightDefaultStorage thus enabling mocking
#>
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$true)]$Config,
$StorageAccountName,
$StorageAccountKey,
$StorageContainerName
)
Set-AzureHDInsightDefaultStorage -Config $input -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey -StorageContainerName $StorageContainerName
}
函数集AzureHDInsightDefaultStorageProxy{
[CmdletBinding()]
Param(
[参数(ValueFromPipeline=$true)]$Config,
$StorageAccountName,
$StorageAccountKey,
$StorageContainerName
)
设置AzureHDInsightDefaultStorage-Config$input-StorageAccountName$StorageAccountName-StorageAccountKey$StorageAccountKey-StorageContainerName$StorageContainerName
}
它仍然无法正常工作,因为我没有在管道中传递来自前面cmdlet的正确值,因此出现错误:
错误:08/12/2014 12:19:55:At
E:\DeployUtilities.psm1:984
字符:21+集-AzureHDInsightDefaultStorageProxy-StorageAccountName…+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~[]异常:无法转换 将“System.Object[]”更改为类型“Microsoft.WindowsAzure.Manag” 所需的ement.HDInsight.Cmdlet.DataObjects.AzureHDInsightConfig' 参数'Config'。不支持指定的方法。-->明确规定 方法不受支持
但这是一个不同的(不言自明的)问题,我现在将努力解决它 你试过在你的参数列表中键入
[AzureHDInsightConfig]$config
吗?嗨,米奇·巴拉德利,没问题,我知道了。基本上也遇到了同样的问题,就在后来的管道中。用同样的方法修复了它,现在一切都好了。
function Set-AzureHDInsightDefaultStorageProxy{
<#
.SYNOPSIS
Wrap Azure cmdlet Set-AzureHDInsightDefaultStorage thus enabling mocking
.DESCRIPTION
Wrap Azure cmdlet Set-AzureHDInsightDefaultStorage thus enabling mocking
#>
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$true)]$Config,
$StorageAccountName,
$StorageAccountKey,
$StorageContainerName
)
Set-AzureHDInsightDefaultStorage -Config $input -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey -StorageContainerName $StorageContainerName
}