我正在使用下面的 PowerShell (v7) 脚本来自定义 AWS SSO 登录流程。它基于有效的 .net 实现:
$ErrorActionPreference = "Stop"
Import-Module -Name "AWSPowerShell.NetCore"
$profileName = "my-sso-profile"
$chain = New-Object Amazon.Runtime.CredentialManagement.CredentialProfileStoreChain
$credentials = $null
$chain.TryGetAWSCredentials($profileName, [ref]$credentials)
$ssoCredentials = [Amazon.Runtime.SSOAWSCredentials]$credentials
$ssoCredentials.Options.ClientName = "Example-SSO-Script"
$ssoCredentials.Options.SsoVerificationCallback = [System.Action[Amazon.Runtime.SsoVerificationArguments]]{
param($x)
# Launch SSO Login Flow in Browser
Start-Process $x.VerificationUriComplete
}
Set-AWSCredential -Credential $ssoCredentials
# Print details about current Credential
Get-StsCallerIdentity
错误
当我在 Powershell (v7) 中运行此脚本时,出现异常:
此线程中没有可用于运行脚本的运行空间。 您可以在 System.Management.Automation.Runspaces.Runspace 类型的 DefaultRunspace 属性中提供一个。您尝试调用的脚本块是: param($x) ...cationUriComplete
看起来在调用我的脚本委托时出现问题$ssoCredentials.Options.SsoVerificationCallback
问题
如何配置我的脚本/PowerShell 会话,以便我的SsoVerificationCallback
委托执行而不会引发错误?
研究
我猜这个问题与调用 SsoVerificationCallback 委托的 C# 代码有关,它现在是一个 PSScriptBlock(?),并且在 C# 和 PowerShell 之间来回编组出现问题。
环顾 SO,SsoVerificationCallback
调用不是 Async,尽管它是在 async method 内部调用的GetSsoTokenAsync
,所以我不认为Runspace issus using async APIs from Powershell适用。
而且我没有直接拨打网络电话,因此请在博客中提供提示,例如https://www.agilepointnxblog.com/powershell-error-there-is-no-runspace-available-to-run-scripts-in-this -thread/推荐的设置[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
也没有产生任何影响。