首页 > 解决方案 > 通过 Visual Basic 将 Set-AWScredential 与 PS 结合使用

问题描述

所以我正在尝试使用管道创建一些 aws 配置文件来打开 powershell 并运行命令。当我直接通过 PS 使用该命令时,它可以完美运行,但在尝试使用 Pipeline 时,它​​只是说该命令不存在:

术语“Set-AWSCredential”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。

这是我正在使用的代码:

    Dim accessKey = accessKeyTxt.Text()
    Dim secretKey = secretAccessTxt.Text()
    Dim profileName = profileNameTxt.Text()

    Dim runspace As Runspace = RunspaceFactory.CreateRunspace()
    runspace.Open()
    Dim pipeline As Pipeline = runspace.CreatePipeline()
    pipeline.Commands.AddScript("Set-AWSCredential -ProfileLocation $env:USERPROFILE\.aws\credentials -StoreAs " & profileName & " -AccessKey " & accessKey & " -SecretKey " & secretKey)
    pipeline.Commands.Add("Out-String")
    pipeline.Invoke()

    runspace.Close()

在这一点上,我不知道是否有可能实现这一点。任何帮助都是合适的。

编辑#1:

所以我通过导入 AWS.Tools.Common 来绕过这个:

Dim pipeline As Pipeline = runspace.CreatePipeline()
        pipeline.Commands.AddScript("Set-ExecutionPolicy RemoteSigned")
        pipeline.Commands.Add("Out-String")
        pipeline.Invoke()

        Dim pipeline2 As Pipeline = runspace.CreatePipeline()
        pipeline2.Commands.AddScript("Import-module AWS.Tools.Common")
        pipeline2.Commands.Add("Out-String")
        pipeline2.Invoke()

        Dim pipeline3 As Pipeline = runspace.CreatePipeline()
        pipeline3.Commands.AddScript("Set-AWSCredential -ProfileLocation $env:USERPROFILE\.aws\credentials -StoreAs test100 -AccessKey test1 -SecretKey test1")
        pipeline3.Commands.Add("Out-String")
        pipeline3.Invoke()

现在它找到了脚本,但我得到一个 void 方法错误:

抛出异常:System.Management.Automation.dll 中的“System.Management.Automation.CmdletInvocationException” System.Management.Automation.dll 中发生“System.Management.Automation.CmdletInvocationException”类型的未处理异常 找不到方法:“无效亚马逊” .Runtime.AssumeRoleAWSCredentialsOptions.set_ProxySettings(System.Net.IWebProxy)'。

我不知道如何解决。

标签: visual-studioamazon-s3

解决方案


所以我就放弃了,用简单的方法做到了。

Dim accessKey = accessKeyTxt.Text()
        Dim secretKey = secretAccessTxt.Text()
        Dim profileName = profileNameTxt.Text()
        Dim awsPath = Environment.GetEnvironmentVariable("USERPROFILE")

        Dim inputString As String = "
[" & profileName & "]
aws_access_key_id=" & accessKey & "
aws_secret_access_key=" & secretKey & "
region=us-east-1
"
        My.Computer.FileSystem.WriteAllText("" & awsPath & "\.aws\credentials", inputString, True)

推荐阅读