I am trying to assign an environment variable in Windows from the contents of a RSA Private Key File.
In Linux this is easily accomplished with the export command
export TF_VAR_secretkey=`cat /path-to-file/file.txt`
I cannot find anything to get something similar to work on a Windows 11 Machine.
I read this article
How to set an env variable with multiline file content in windows command prompt
I tried using a cmd file to run the terraform plan with the following
#echo off
SetLocal EnableDelayedExpansion
set TF_VAR_secretkey=
for /F "delims=" %%i in (C:\path-to-file\file.txt) do set TF_VAR_secretkey=!TF_VAR_secretkey!%%i
terraform plan -out=main.plan
EndLocal
The only way I have been able to get it to work is setting in a variable file for Terraform with
secretkey = "C:\\path-to-file\\file.txt"
But this is not optimal because then I need to add this to every terraform workspace/folder, where I just want to hold this variable in the environment until I close the terminal session.
I also tried to copy the output from the export of Linux as an system environment variable but still got the error that the PEM file is empty which always tells me there is something wrong with the secretkey value
│ Error: failed to get signature context: File '' does not contain PEM data
│
│ with provider["registry.terraform.io/ciscodevnet/intersight"],
│ on provider.tf line 21, in provider "intersight":
│ 21: provider "intersight" {
│
The Answer was that instead of trying to print the contents of the file I simply needed to point to the file, which is different than Linux.
$env:TF_VAR_secretkey=`$HOME\path-to-file\file.txt`
Related
I'm trying to generate a csv running a SSIS package.
First, I've created a string variable DATA_PATH (same name as windows system variable). Then, I've set a Process Task which run an echo to a Windows system variable like this:
The output of the command set my variable DATA_PATH correctly.
When I run the package, it throws the error "[Destino de archivo plano 1 [44]] Error: Cannot open the datafile (...)". But, if I hardcode my SSIS variable with the same exact file path as the echo %DATA_PATH% output, the package run correctly, without any error.
I'm using UnxUtils for running Bash commands in Windows CMD. Bash files are stored in C:\Users\username\win_bash\. I have added that folder to windows PATH so I can access it from cmd no mater in which folder am I. I would like to overwrite default Windows echo command. Currently I can do that with specifying whole path to the Bash echo command: C:\Users\win_bash\echo.exe. Can I make Bash's echo default, so I could just type echo without the whole path to the command?
Yes, You can change the system path look for the default path after EDITING the default path you can change or remove the system variable
EDIT path with C:\Users\win_bash\echo.exe
I'm an Linux user you have try this solution below, Make sure to copy the default path and setting before applying changes so you can revert back if anything goes wrong.
https://www.java.com/en/download/help/path.html
I am using following batch file to download and unzip the google-cloud-sdk,
file name: download.bat
set home=%USERPROFILE%
echo %home%
%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe -Command "(New-Object Net.WebClient).DownloadFile('https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-179.0.0-windows-x86_64-bundled-python.zip', '%home%/google-cloud-sdk.zip');& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('%home%/google-cloud-sdk.zip', '%home%/google-cloud-sdk'); }"
and I am using following batch file to install the SDK silently on windows,
file name: install_win.bat
call %USERPROFILE%\google-cloud-sdk\google-cloud-sdk\install.bat --path-update=true --usage-reporting=false --command-completion=false
After these steps are completed successfully when I try to authorize my serice account from another batch file using the following command,
gcloud auth activate-service-account --key-file {json fila path}
It fails and tells me that the glcoud is not recognized internal or external command.
Can anyone please help me solve this error?
I think the error is because of the lack of environment variable in windows with the bin file of SDK.
How to set that from the batch file?
The ...cloud...\install.bat routine is called, hence it inherits the calling routine's environment. It does not communicate any changes back to the calling routine.
Since I'd assume that the install.bat is supplied by a 3rd party, hence cannot be altered, then you would need to retrieve the new path for the current cmd instance.
call %USERPROFILE%\...\install.bat ...
FOR /f "delims=" %%a IN ('echopath') DO SET "newpath=%%a"
where echopath.bat is simply one line:
echo %path%
Provided echopath.bat is reachable - and it must be a separate file, not an internal routine - the new path should be returned (to newpath in this instance - I'll let you guess what changes you'd need to make to change that to path...)
Since executing an external batch loads the environment anew, the modified environment path will be reported by echopath and hence assigned to the variable nominated.
I have a batch file that looks like the following (call it "awsCopy.bat"):
"C:\Program Files\Amazon\AWSCLI\Aws.exe" s3 cp "s3://xxxs3bucketxxx/file.csv" "\\networkDrive\folder\file.csv"
When I run this batch file it works perfectly, copying my file from s3 and downloading it to my specified directory. (I have run AWS configure already to store my keys in the .../user/.aws/config file).
When I try to run the batch file from R with system("awsCopy.bat"), however, the command completes with status 1, does not copy the file and I get the following message:
Unable to locate credentials
Completed 1 part(s) with ... file(s) remaining
I've tried adding set commands in the batch file to no avail:
set aws_access_key_id = xxxxxxx
set aws_secret_access_key = yyyyyy
Has anyone else had (and solved) this issue? (I'm running windows 7) Thanks.
According to the system documentation (on a language I have no idea what it is) on Windows, System does NOT invoke the shell (unlike Unix). It says to use the shell command.
Or in other languages we could use your system call like so (but the docs say use System2 call as it's easier)
cmd /c "c:\somedfolder\batch.bat
I finally figured out the issue. I was executing this script from a network drive, but my credentials were set up only on my local machine. (I guess the setting the keys was just re-setting them locally.) Saving "awsCopy.bat" locally does the trick. Running system, shell and system2 on the batch file path all work.
I would like to read the hostname of my device and generate a file with such name. However, I am new in Windows Batch, I don't even understand how to use of those variable, read file......
Here is what I want to do:
CD C:\WINDOWS\SYSTEM32\
CMD.EXE hostname -> to a HostName variable e.g. called abc ::I hope it will save my computer name to a variable string
echo It success to function >> C:\%abc%.txt :: I hope it can generate a file with the sting in variable
PAUSE
I have thought that if it is not function with cmd command, so I tried the alternative method:
CD C:\WINDOWS\SYSTEM32\
CMD.EXE hostname >> C:\HostName.tmp
-AND THEN READ BACK the C:\HostName.tmp Here
-And then save to a variable again e.g.
echo It success to function >> C:\HostName_variable.txt ::It generate a file with the sting in variable
PAUSE
It seems not successful =[.
Added:
I would like to create a file with the name of hostname. e.g. if my hostname is "abc", it will output a file as "abc.csv" something like this.
You can get it two ways:
Environment variables are a few pre-configured variables containing general system state, and one of them is COMPUTERNAME, which you can send straight to a file like:
echo %COMPUTERNAME% > hostname_file.txt
Or you can store it in another variable, first, then echo the content of that variable to a file:
set myHostName=%HOSTNAME%
echo %myHostName% > hostname_file.txt
The other way is running the hostname.exe program which prints the current computer name, and redirect the output to a file:
hostname > hostname_file.txt
It's harder to run hostname.exe and store the result in a variable, you need to abuse a for loop to do that:
for /f %f in ('hostname') do set myHostName=%f
echo %myHostName% > hostname_file.txt
NB. What you are doing isn't quite how you describe it - a file isn't the same thing as a variable:
You write:
CD C:\WINDOWS\SYSTEM32\
CMD.EXE hostname >> HostName_variable ::It will save my computer name to a variable string
echo It success to function >> C:\HostName_variable.txt ::It generate a file with the string in variable
PAUSE
cmd.exe is unnecessary because you're already in a command prompt when you run it, you don't need another one to run. You don't need to change to c:\windows\system32 first, because Windows always looks there for programs to run. >> is adding to the end of a file if it exists, it won't always create a new file, > will always create a new file and if one already exists, will overwrite it. Once it has sent the output to a file, it's not in a variable.