visual-studio - 通过 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)'。
我不知道如何解决。
解决方案
所以我就放弃了,用简单的方法做到了。
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)
推荐阅读
- html - 当我尝试在模式中获取输入值时,.val() 返回空字符串
- c++ - “omp single”和“omp task”如何提供并行性?
- excel - 如何根据变量更改表搜索的范围(VBA)
- r - 如何在R中操作基于纬度,经度,时间的数据框?
- sass - 带有 lib-sass 3.6.0 的 node-sass
- html - 使用 SASS 的 "&:hover" 中的 "&" 表示什么?
- tailwind-css - 如何在 Tailwind CSS Typography 插件中将 .prose 类更改为 .Normal?
- vue.js - Vue-trix Editor setSelectedRange 不起作用 - 仅在文本开头设置光标
- node.js - 在 fetch api 中使用带有 mangodb 的节点更新数据
- html - 如何在html中将图像对齐到右上角