AWS Powershell函数将AWSCredentials转换为System.Object
我有一个小包装器函数,它只将几个变量传递给New-Ec2Tag函数。当我运行它时,它会隐藏我的Amazon.Runtime.AWSCredentials 进入System.Object[],然后尝试将其转换回并生成错误 有没有办法让这个功能正常工作 作用AWS Powershell函数将AWSCredentials转换为System.Object,powershell,amazon-web-services,Powershell,Amazon Web Services,我有一个小包装器函数,它只将几个变量传递给New-Ec2Tag函数。当我运行它时,它会隐藏我的Amazon.Runtime.AWSCredentials 进入System.Object[],然后尝试将其转换回并生成错误 有没有办法让这个功能正常工作 作用 function addTag ($awscred, $instanceID, $TagName, $TagValue){ Write-output $awscred.gettype() New-Ec2Tag -region '
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"})
改为这样调用函数:
addTag$AwsCredentials$instanceid“Creator”“123456”
在Powershell中,函数参数不会使用括号和逗号调用,这就是为什么要将参数强制转换到对象数组中