2017-05-25 67 views
0

我有一个小的包装函数,它只是将几个变量传递给New-Ec2Tag函数。当我运行它时,它会将我的Amazon.Runtime.AWSCredentials 转换为System.Object [],然后尝试将其转换回来并生成错误。AWS Powershell函数将AWSCredentials转换为System.Object

有没有办法让这个功能起作用?

功能

function addTag ($awscred, $instanceID, $TagName, $TagValue){ 
    Write-output $awscred.gettype() 
    New-Ec2Tag -region 'ap-southeast-2' -Resource $instanceID -Tag @{Key=$TagName;Value=$TagValue} -Credential $AwsCredentials} 

命令我跑

write-output $AwsCredentials.gettype() 
addTag($AwsCredentials,$instanceid,"Creator","123456") 

命令输出

IsPublic IsSerial Name          BaseType                              
-------- -------- ----          --------                              
True  False SessionAWSCredentials     Amazon.Runtime.AWSCredentials                         
True  True  Object[]         System.Array  

New-EC2Tag : Cannot convert 'System.Object[]' to the type 'Amazon.Runtime.AWSCredentials' required by parameter 'Credential'. Specified method is not supported. 

命令的工作,如果我没有在一个函数把它包装

New-Ec2Tag -region 'ap-southeast-2' -Credential $AwsCredentials -Resource $instanceID -Tag (@{Key="Name";Value="test"}) 

回答

0

调用函数像这样来代替:

addTag $AwsCredentials $instanceid "Creator" "123456"

在PowerShell中,函数参数不使用括号和逗号叫,这就是为什么你的参数被铸造成一个对象数组。

+0

谢谢,我不能相信我忽视了 – Shadowzee

相关问题