Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variables for bash from within powershell?

Using powershell how do I set the environment variables (here, particularly "ApiSecret") so that printenv will show the necessary variables?

(Operating on the assumption that the problem is that Visual Studio and bash aren't picking up the environment variables properly. They can be set from bash easily enough.)

I'm on 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 code:

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 Code:

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> 

but then neither is the standard bash console:

bash console:

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 console, but within 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> 

(From bash, printenv doesn't show the API keys which the script depends upon.)

Version: 1.41.1 Commit: 26076a4de974ead31f97692a0d32f90d735645c0 Date: 2019-12-18T15:04:31.999Z Electron: 6.1.5 Chrome: 76.0.3809.146 Node.js: 12.4.0 V8: 7.6.303.31-electron.0 OS: Linux x64 4.18.0-25-generic snap

like image 473
Thufir Avatar asked Jan 23 '20 15:01

Thufir


1 Answers

For context, this is a solution, but it's using standard bash on Ubuntu:

export ApiKey="57647625454354365"

to check if ApiKey is set correctly do:

echo $ApiKey

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

But still looking to accomplish this with powershell:

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

Because while I can run pwsh twitter.ps1, and powershell will correctly pick up the env variables, visual studio code fails to do so. See also:

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

like image 101
Thufir Avatar answered Nov 15 '22 03:11

Thufir