Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AWS Powershell函数将AWSCredentials转换为System.Object_Powershell_Amazon Web Services - Fatal编程技术网

AWS Powershell函数将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 '

我有一个小包装器函数,它只将几个变量传递给New-Ec2Tag函数。当我运行它时,它会隐藏我的Amazon.Runtime.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"})

改为这样调用函数:

addTag$AwsCredentials$instanceid“Creator”“123456”

在Powershell中,函数参数不会使用括号和逗号调用,这就是为什么要将参数强制转换到对象数组中