How to use a GitLab variable to represent the directory to a command in PowerShell - powershell

I am writing a PowerShell script for a GitLab CICD pipeline that will deploy a file to our TEST box via FTP. This works by using a command to execute an FTP client (winSCP.exe) with a text file as input.
The basic code works, however I decided to replace the hardcoded values and paths with GitLab variables for greater security and to simplify changes. Most of the variable additions worked, but using a variable to replace the path to the FTP client failed - see below:
./$FTPCLIENTDIRECTORY : The term './$FTPCLIENTDIRECTORY' 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.
Right now this section of code works:
'./Resources/WinSCP.exe /log="$LOGFILEDIRECTORY" /ini=nul /script="$SCRIPTDIRECTORY" /parameter $FTPUSERNAME $FTPPASSWORD $FTPCERTIFICATE $FTPFILE'
I am trying to get something more like:
'./$FTPCLIENTDIRECTORY /log="$LOGFILEDIRECTORY" /ini=nul /script="$SCRIPTDIRECTORY" /parameter $FTPUSERNAME $FTPPASSWORD $FTPCERTIFICATE $FTPFILE'
I'm guessing that the ./ here operates as some kind of escape sequence that prevents YAML from recognizing the variable, however nothing none of the YAML or PowerShell escape characters I've used to escape-the-escape characters have worked.
Do you have any insight into why this is happening and or what a solution might be?

Related

Renaming objects in Google Cloud Storage

I am backing up footage for my video company in google cloud storage. I did a dumb, and my object name has spaces in it and an &. So I need to download this 700gb project, and PowerShell won't do it because it thinks the text after the & is a command and fails the command. What are my options for renaming this object? Or if there's another thing I am missing, please let me know. My problem is using an & in my naming structure, so if anyone knows of any way to rename that would be fantastic.
Other info: Downloading using the command that the download button gives me. I just pasted that into my command prompt and get the error " 'Joe' is not recognized as an internal or external command,
operable program or batch file."
I have tried using "gsutil -m mv gs://my_bucket/oldprefix gs://my_bucket/newprefix" this code with the paths changed to change the name but this also fails because of the spaces in the file path

How do I invoke a command using a variable in powershell?

I want to invoke the shutdown.exe executable in powershell and it is located on C:\WINDOWS\System32\shutdown.exe.
Since we already got the C:\WINDOWS in the variable $env:windir I want to just use it and concatenate the rest of the path into the command. I was trying this:
PS C:\Users\shina> .\$env:windir\System32\shutdown.exe -s
But I got the following error:
.\$env:windir\System32\shutdown.exe: The term '.\$env:windir\System32\shutdown.exe' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
How can I accomplish this?
You can use:
& $env:windir\System32\shutdown.exe -s
Or
. $env:windir\System32\shutdown.exe -s
(note the space).
& is the call operator. You can read more about it here:
Runs a command, script, or script block. The call operator, also known as the "invocation operator", lets you run commands that are stored in variables and represented by strings or script blocks.
. is the dot sourcing operator. You can read more about it here:
Runs a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope, overriding existing ones.
The dot sourcing operator is followed by a space. Use the space to distinguish the dot from the dot (.) symbol that represents the current directory.

Passing strings with spaces to PowerShell

Ultimately, I want to be able to call my PS script from VBA (Excel), but the easiest way to do that seems to be with system batch commands - so I'm testing my script with a BAT file.
The script returns a text file with the contents of a webpage, sans HTML tags. It works fine called alone with default parameters; it worked fine with no spaces in the output path parameter, but I've had no luck using a path that includes a space.
PS1 script, boiled down:
param ( [string]$outputPathName="" )
$outputPathName | Out-File "D:\Documents\Google Drive\out.txt"
BAT file to test it:
powershell.exe '"D:\Documents\Google Drive\GetWebPage.ps1"' -outputPathName '"D:\Documents\Google Drive\out.txt"'
I get the error message "Unexpected token '-outputPathName' in expression or statement." (I also get a similar error for the argument, but if the parameter name were recognized I might be able to get past this.)
How can I pass a named argument in from BAT script file to PS1 script file (with spaces in the path and argument)?

Passing parameter from TFS Build Process Template to Powershell Script, with two consecutive spaces

So I have created a build process template in TFS 2012 that has to pass a path name to a Powershell Script, which in turn concatenates files in the specified directory.
Some of these path names might have two consecutive spaces, which has turned out to be a problem.
When I invoke the powershell script, I enclose the path name in single quotes, and the command that is executed looks something like this.
powershell C:\psScript.ps1 'C:\tmp\two spaces\myFolder'.
However, when I try and open the directory in Powershell, I get the following error:
Get-Item : Cannot find path 'C:\tmp\two spaces\myFolder'
because it does not exist.
The two spaces seem to have become one, and the path can't be found.
Does anyone know what might be causing this?

How can I execute an external program with parameters in PowerShell?

I have read this answer stackoverflow answer and it get's me there half way. Here is what I need to do.
Execute this command:
"c:\myexe.exe <c:\Users\Me\myanswerfile.txt"
If I run that straight from within my powershell script
&'c:\myexe.exe <c:\Users\Me\myanswerfile.txt'
I get this error:
The term 'C:\myexe.exe <c:\Users\Me\myanswerfile.txt' 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, verif that the path is correct and try again.
Now I have tried several variations of this including placing the original command in a variable called $cmd and then passing the
If I append the '<' to the $cmd variable the command fails with a similar error as the first one.
I'm stumped. Any suggestions?
If you want to run a program, just type its name and parameters:
notepad.exe C:\devmy\hi.txt
If you want to run an exe and redirect stdin to it which your example seems to be an attempt of, use:
Get-Content c:devmy\hi.txt | yourexe.exe
If you need to specify the full path to the program then you need to use ampersand and quotes otherwise powershell thinks you are defining a plain string:
&"C:\Program Files (x86)\Notepad++\notepad++.exe"
Simply use & operator
& "Program\path\program.exe" "arg1" "arg2" ....