在编写带有特殊字符“ ::”的类方法时,我遇到了变量扩展问题。我目前已经通过重新定义“ $ this”解决了该问题。下面列出的“身份验证”方法中具有标准变量的变量:
[AwsCreds]Authenticate() {
Set-AWSCredentials -ProfileName $this.aws_profile_name
$aws_acct = $this.aws_account_number
$aws_acct_id = $this.aws_account_userid
$sts_creds = Get-STSSessionToken `
-SerialNumber arn:aws:iam::"$aws_acct":mfa/"$aws_acct_id" `
-TokenCode $this.aws_duo_token | `
select-object AccessKeyId, SecretAccessKey, SessionToken
Set-AWSCredential -AccessKey $sts_creds.AccessKeyId -SecretKey $sts_creds.SecretAccessKey `
-SessionToken $sts_creds.SessionToken -StoreAs "$($this.aws_profile_name)_temp"
Write-Information -MessageData "$($this.aws_profile_name)_temp profile created." -InformationAction Continue
return $this
}
我在想有一种更好的方法来解决此问题,但是例如使用“ $($ his.aws_account_userid)”似乎无法正确扩展。欢迎任何建议!