如何从 powershell 中为 bash 设置环境变量?

Posted

技术标签:

【中文标题】如何从 powershell 中为 bash 设置环境变量?【英文标题】:How to set environment variables for bash from within powershell? 【发布时间】:2020-05-09 22:40:36 【问题描述】:

使用 powershell how do I set environment 变量(此处为 particularly "ApiSecret")以便 printenv 显示必要的变量?

(假设问题是 Visual Studio 和 bash 没有正确获取环境变量。可以从 bash easily 设置它们。)

我在 Linux 上:

thufir@dur:~/powershell/helloPSTwitterAPI$ 
thufir@dur:~/powershell/helloPSTwitterAPI$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.10
Release:    18.10
Codename:   cosmic
thufir@dur:~/powershell/helloPSTwitterAPI$ 

powershell 代码:

Import-Module PSTwitterAPI


Set-TwitterOAuthSettings -ApiKey $env:ApiKey -ApiSecret $env:ApiSecret -AccessToken $env:AccessToken -AccessTokenSecret $env:AccessTokenSecret

#Get-TwitterUsers_Lookup -screen_name 'mkellerman'

$TwitterStatuses = Get-TwitterStatuses_UserTimeline -screen_name 'mkellerman'
#$TwitterStatuses = [array]Get-TwitterStatuses_UserTimeline -screen_name 'mkellerman'

Foreach ($status in $TwitterStatuses) 
   Write-Host $status.text




Write-Host "done"

Visual Studio 代码:

PS /home/thufir> /home/thufir/powershell/helloPSTwitterAPI/twitter.ps1


WARNING: OAuthSettings with AccessToken '' already exists.
Invoke-RestMethod : "errors":["code":215,"message":"Bad Authentication data."]
At /home/thufir/.local/share/powershell/Modules/PSTwitterAPI/0.0.7/public/Invoke-TwitterAPI.ps1:34 char:5
+     Invoke-RestMethod @RestMethod_Params
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (Method: GET, Reques\u2026PowerShell/6.2.3
:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Invoke-RestMethod : "errors":["code":215,"message":"Bad Authentication data."]
At /home/thufir/.local/share/powershell/Modules/PSTwitterAPI/0.0.7/public/Invoke-TwitterAPI.ps1:34 char:5
+     Invoke-RestMethod @RestMethod_Params
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (Method: GET, Reques\u2026PowerShell/6.2.3
:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
done
PS /home/thufir> 

但标准 bash 控制台也不是:

bash 控制台:

thufir@dur:~/powershell/helloPSTwitterAPI$ 
thufir@dur:~/powershell/helloPSTwitterAPI$ powershell twitter.ps1 
Invoke-RestMethod : "errors":["code":215,"message":"Bad Authentication data."]
At /home/thufir/.local/share/powershell/Modules/PSTwitterAPI/0.0.7/public/Invoke-TwitterAPI.ps1:34 char:5
+     Invoke-RestMethod @RestMethod_Params
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (Method: GET, Reques\u2026PowerShell/6.2.3
:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Invoke-RestMethod : "errors":["code":215,"message":"Bad Authentication data."]
At /home/thufir/.local/share/powershell/Modules/PSTwitterAPI/0.0.7/public/Invoke-TwitterAPI.ps1:34 char:5
+     Invoke-RestMethod @RestMethod_Params
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (Method: GET, Reques\u2026PowerShell/6.2.3
:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
done
thufir@dur:~/powershell/helloPSTwitterAPI$ 

bash 控制台,但在 powershell 中:

PS /home/thufir/powershell/helloPSTwitterAPI> 
PS /home/thufir/powershell/helloPSTwitterAPI> ./twitter.ps1
WARNING: OAuthSettings with AccessToken '17639841-ZltXv9qW0zlW60WNy2MtcAZ0FEljMqExQQ8voyy1p' already exists.
RT @adamdriscoll: Just added some #UniversalAutomation documentation about pre-defined variables in UA jobs. ....
RT @adamdriscoll: #PowerShell #UniversalDashboard 2.8.2 is now available on the PowerShell Gallery. Lots of fixes, some improvements to Adm…
@psdevuk @adamdriscoll @psdbatools ????
@adamdriscoll ????
@BillKindle @McDonalds @Wendys Sad, but that’s what I’m going to do next time. It should be ‘BigMac with Bacon Bits… ....
I was excited to try out the new BigMac with Bacon... but horrible portion.. looks like cesar salad bacon bits...… ....
@WindosNZ PSTwitterAPI? ;)
@Marioperator Thanks for the shoutout ❤️
RT @adamdriscoll: Nice! Financial charts for UD! ...U #powershell h...
@TomatoApp1 Constantly having to bind/unbind MiaoMiao device. And now the app won’t even open after trying reinstal… ...
@adamdriscoll It shall get indexed and searchable in 15 minutes! I can just imagine your amazon shopping suggestions...
@adamdriscoll @LeeAlanBerg Pics or it didn’t happen
@SwiftOnSecurity @adbertram Did you end up finding a more elegant solution?
RT @racheldianeb: Had cake and wine tonight. 2 things I said I wouldn’t consume in Jan and would generally limit in 2020. It’s Jan 1st. So…
@adilio @sstranger Someone would probably be wrong.. ????
@AndrewPlaTech @sstranger You have nothing to lose.. I mean, clearly I lost.. ;)
Someone’s mother has four sons. North, South and East. What is the name of the fourth son. Private message me the n… ...:,...
RT @_youhadonejob1: Let's all take a moment to recognize this man, who was instrumental in thwarting the Nakatomi Plaza terrorist attacks o…
RT @EssentialSign_: For whoever needs this this evening. ...
RT @wongmjane: I'm working on something new on my website, check it out and lemme know what you think :D

http://localhost:3000
done
PS /home/thufir/powershell/helloPSTwitterAPI> 

(来自 bash,printenv 不显示脚本 depends 所在的 API 密钥。)

版本:1.41.1 提交:26076a4de974ead31f97692a0d32f90d735645c0 日期: 2019-12-18T15:04:31.999Z 电子:6.1.5 铬:76.0.3809.146 Node.js:12.4.0 V8:7.6.303.31-electron.0 操作系统:Linux x64 4.18.0-25-通用快照

【问题讨论】:

so ... powershell 代码在哪里?您显示的所有内容似乎都是错误和输出... 哦,对了,我不知道现在你可以在 linux 机器上运行 powershell。我更习惯于反过来,在 Windows 机器上运行 bash 脚本 【参考方案1】:

就上下文而言,这是一个解决方案,但它在 Ubuntu 上使用标准 bash

export ApiKey="57647625454354365"

检查 ApiKey 是否设置正确:

echo $ApiKey

https://askubuntu.com/a/1205233/45156

但仍然希望通过 powershell 来实现这一点:

https://askubuntu.com/q/1205227/45156

因为虽然我可以运行 pwsh twitter.ps1,并且 powershell 会正确获取 env 变量,但 Visual Studio 代码无法这样做。另见:

https://code.visualstudio.com/docs/editor/variables-reference

【讨论】:

以上是关于如何从 powershell 中为 bash 设置环境变量?的主要内容,如果未能解决你的问题,请参考以下文章

powershell 在PowerShell中为Azure RM Terraform设置环境变量

如何在 Powershell 中为“网络服务”帐户创建 Windows 服务?

如何在Windows上为Google云平台添加docker-registry secret(就是在PowerShell中,不是Bash)?

使用 env 在 bash 中为一个程序调用设置环境变量

设置Windows PowerShell环境变量

我可以将退出代码 %errorlevel% 从 WSL Bash 返回到 Powershell 或命令提示符吗?