Using Set-AWScredential with PS through Visual Basic

Viewed 360

So I am trying to create some aws profiles using a pipeline to open powershell and run the command. It works perfectly when I use the command directly through PS but when trying to use the Pipeline it just says that the command does not exist:

The term 'Set-AWSCredential' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Here is the code I am using:

    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()

At this point I don't know if it is even possible to achieve this. Any help is aprpeciated.

EDIT#1:

So I kinda bypass this by importing 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()

Now it finds the script but I get a void method error:

Exception thrown: 'System.Management.Automation.CmdletInvocationException' in System.Management.Automation.dll An unhandled exception of type 'System.Management.Automation.CmdletInvocationException' occurred in System.Management.Automation.dll Method not found: 'Void Amazon.Runtime.AssumeRoleAWSCredentialsOptions.set_ProxySettings(System.Net.IWebProxy)'.

Which I don't know how to fix.

1 Answers

So I just gave up and did it the easy way.

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)
Related