I was following a guide to connect a database to kubernetes:
https://itnext.io/basic-postgres-database-in-kubernetes-23c7834d91ef
after installing Kubernetes (minikube) on Windows 10 64 bit:
https://minikube.sigs.k8s.io/docs/start/
I am encountering an issue with 'base64' where the DB is trying to connect and store the password. As PowerShell doesn't recognise it. I was wondering if anyone has any ideas how I could either fix this and still use windows or an alternative means that would enable me to continue with the rest of the guide?
Error Code:
base64 : The term 'base64' 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.
At line:1 char:131
+ ... postgresql -o jsonpath="{.data.postgresql-password}" | base64 --decod ...
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (base64:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
export : The term 'export' 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.
At line:1 char:1
+ export POSTGRES_PASSWORD=$(kubectl get secret --namespace default pos ...
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (export:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Windows Powershell Error message
The base64 cli found in Mac OS and some *nix distros is not available on Windows.
You could write a small function named base64 that mimics the behavior of the base64 unix tool though:
function base64 {
# enumerate all pipeline input
$input |ForEach-Object {
if($MyInvocation.UnboundArguments -contains '--decode'){
# caller supplied `--decode`, so decode
$bytes = [convert]::FromBase64String($_)
[System.Text.Encoding]::ASCII.GetString($bytes)
} else {
# default mode, encode ascii text as base64
$bytes = [System.Text.Encoding]::ASCII.GetBytes($_)
[convert]::ToBase64String($bytes)
}
}
}
This should work as a drop-in replacement for conversion between ASCII/UTF7 text and base64:
PS ~> 'Hello, World!' |base64 --encode
SGVsbG8sIFdvcmxkIQ==
PS ~> 'Hello, World!' |base64 --encode |base64 --decode
Hello, World!
To use with your existing scripts, simple dot-source a script with the function definition in your shell before executing the others:
PS ~> . .\path\to\base64.ps1
The above will work from a script as well. If you have a multi-line paste-aware shell (Windows' default Console Host with PSReadLine should be okay), you can also just paste the function definition directly into the prompt :)
You're trying to execute command lines on Windows that were written for Unix-like platforms, both in terms of:
the external utilities they expect (base64)
the shell syntax they use (export POSTGRES_PASSWORD=$(...); written for POSIX-compatible shells such as bash).
Mathias' helpful answer shows you how to emulate a base64 utility in PowerShell, and you may even be able to emulate the export shell command with additional, nontrivial effort, but not all cases can be handled that way, such as a command line that uses \" to escape " characters (this will break PowerShell's syntax, try echo "3\" of snow.").
Therefore, I suggest either running your commands as-is via WSL, if feasible, or taking the time to translate the command lines into PowerShell-native equivalents.
Related
I am trying to run a CMD task inside of a PowerShell script that will open up a new tab in Google Chrome. I am running this command inside an Azure DevOps pipeline.
The task I am attempting to run is:
start chrome --user-data-dir="ChromeProfiles\Profile$profile" --disable-default-apps --new-window "$($reportHtmlFile)"
When I run this command from my local command prompt, a new tab opens and works as expected. To run it from my PowerShell window I run:
cmd /c echo start chrome --user-data-dir="ChromeProfiles\Profile$profile" --disable-default-apps --new-window "$($reportHtmlFile)" | cmd.exe
Both the above commands work as expected however, trying to run them from Azure DevOps I am getting an error saying:
+ ... " --disable-default-apps --new-window "$($reportHtmlFile)"" | cmd.exe
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: ".
At D:\Agent\instance01\Workspace\20\s\pbi-load-test-tool\Run_Load_Test_Only.ps1:59 char:1
+
Missing closing ')' in expression.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
I have tried the following:
"start chrome --user-data-dir="ChromeProfiles\Profile$profile" --disable-default-apps --new-window "$($reportHtmlFile)"" | cmd.exe
& "start chrome --user-data-dir="ChromeProfiles\Profile$profile" --disable-default-apps --new-window "$($reportHtmlFile)" | cmd.exe"
Is there a syntactical error or is this a shortcoming with Azure DevOps?
What's Going on:
Your arguments are getting mangled because they contain quotes.
When you run this the way you're running it now, PowerShell will try to handle the quotes in the arguments before passing it to the exe.
The solution: Use Splatting
Splatting is a part of the PowerShell language that lets you pass structured arguments to a command. You can Splat a [Hashtable] to provide named arguments (if you're calling a function or cmdlet). You can also Splat an [Object[]] to provide arguments positionally.
Create an array out of all of your arguments:
In your case, that would be:
$startArgs = #(
# The user-data-dir probably wants double quotes
# so we use backticks to embed them.
"--user-data-dir=`"ChromeProfiles\Profile$profile`""
'--disable-default-apps'
'--new-window'
"$($reportHtmlFile)"
)
Call with splatting:
start #startArgs
Doing things this way will ensure that each argument is sent exactly the way you want it, with complete control over how arguments will be quoted.
Also: avoid $profile
$profile is the name of an automatic variable in PowerShell pointing to the PowerShell profile. I suspect you're interested in a chrome profile, not a PowerShell profile, so I would pick a different variable name that would better describe your purpose, rather than risk having an automatic variable provide a bad result.
I'm using the zsh history plugin, which adds the timestamp of the command I entered. The raw file of the .zsh_history text file looks like below:
: 1582469132:0;jupyterlab
: 1582469132:0;jupyter notebook
: 1582469132:0;jupyterlab
: 1582469132:0;jupyter lab
: 1582469132:0;jupyter notebook
: 1582469132:0;ls
I just don't understand the reason for using this format that why every line item starts with a colon :?
I'm pretty sure this plugin is not the only one using this format. I've seen SWIFT messages using this format and some other file which I couldn't remember the name.
I'm not positive, but I believe the leading colon allows you to execute the history file as a script.
: is a do-nothing command: it ignores its arguments then completes with an exit status of 0. In this case, the argument is the string <timestamp>:0; the semicolon is a command terminator.
You can try it out at the prompt:
% : 1235:0;echo hello
hello
Thus, executing this file as a script would have the same affect as executing
jupyterlab
jupyter notebook
jupyterlab
jupyter lab
jupyter notebook
ls
It's unlikely you would want to execute an arbitrary history file as a script, but a file containing a specially crafted history could be useful.
I'm trying to write a Windows Powershell script but when I write $ curl wttr.in for example, I get the expected output however, when I do $a=curl wttr.in;echo $a I just get gibberish. I'm using the curl executable located in C:\Windows\System32\ since I removed the default Powershell aliases related with Invoke-WebRequest (curl and wget). Is there something I'm doing wrong?
Here is what I mean:
curl wttr.in (expected output)
$a=curl wttr.in;echo $a (wrong output)
I believe it has to do with encoding. A workaround would be simply add Out-String when capturing
$a = C:\Windows\system32\curl.exe wttr.in | Out-String
$a
I could not test it (response was "no more querys"), but you can force the output encoding into a specific encoding
Encode a string in UTF-8
may take some testing to find the right output.
Using Rasa open source I tried to execute (Windows Powershell) this command rasa run action & rasa shell it generate some error like this:
At line:1 char:17
+ rasa run action & rasa shell
+ ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to
pass it as part of a string.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : AmpersandNotAllowed
In PowerShell you can use a semicolon to run 2 commands (note it should be rasa run actions):
rasa shell; rasa run actions
In cmd you can still use ampersand.
However this won't work on Windows where commands are run sequentially. One solution is to have a terminal session for each command (you also don't mix up logs and can restart only one if you need to)
You are trying to run 2 different commands on the shell. shell does not know "&".
Rasa run command is used to run the rasa framework as a server on localhost:5005
whereas
rasa shell is used to run on the chatbot on the terminal, the server will be started automatically.
I would prefer to run the command in 2 different shell to reduce the debug confusion
I have a PowerShell script that produces a text file. At the end, I would like to copy this file to a Linux server.
From CMD.EXE, I can use PSCP (from Putty), it works and copies the file.
But from PowerShell, either interactively or from a PowerShell batch, PSCP has no visible effect: no error messages and the file is not copied.
Even if I run simply .\PSCP.EXE without arguments, on the CMD command line it displays the options, but from PowerShell it does nothing.
Can PSCP be used from inside PowerShell?
Executing a program from within PowerShell should work identically to CMD, but depending upon how that program produces its output (does it write to STDOUT, STDERR, other?) that may behave differently.
I've been using Rebex's components for FTPS & SFTP within .NET apps & PowerShell scripts; the SFTP package includes an SCP class. Yes, it costs money, but depending upon your usage it may be worthwhile.
Just attempted to automate PSCP from PowerShell. Remember to use pscp's -batch parameter so that, should you do something like enter the wrong password, you won't get asked for input.
$Cmd = "pscp -l username -pw password -batch c:\folder\file.txt server:/home/user1"
Invoke-Expression "& $( $Cmd )"
Otherwise your script will just grind to a halt.
Yes - most any executable can be called from PowerShell. There isn't anything peculiar about pscp.exe in this regard. You may need to preface it with the call operator - the ampersand - &:
PS C:\>& "C:\Program Files (x86)\Putty\pscp.exe" -V
pscp: Release 0.62
The above is direct output from my PowerShell prompt. The call operator is particularly helpful if the path to your executable contains spaces - the call operator is used to tell PowerShell to treat what would be considered a string as something it should try to execute instead.
Please include the full command your are trying to execute as it will help in providing a better answer. You may have a problem with your PATH variable or something else weird if you don't get any output.
If using pscsp from inside a script, e.g. perl
no ampersand
quote like this "my password"
e.g.
"C:\Program Files\Putty\pscp.exe" -C -p -pw "password" /local_dir/file_to_copy user#hostname:/remote_directory
in perl (beware that \ is an escape char in a "string" )
$cmd = q("C:\Program Files\Putty\pscp.exe" -C -p -pw "password" /local_dir/file_to_copy user#hostname:/remote_directory);
system($cmd);