Passing strings with spaces to PowerShell - 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)?

Related

How to use a GitLab variable to represent the directory to a command in 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?

How to read a text file to a variable in batch and pass it as a parameter to a powershell script

I have a powershell script that generates a report, and I have connected it to an io.filesystemwatcher. I am trying to improve the error handling capability. I already have the report generation function (which only takes in a filepath) within a try-catch loop that basically kills word, excel and powerpoint and tries again if it fails. This seems to work well but I want to embed in that another try-catch loop that will restart the computer and generate the report after reboot if it fails a second consecutive time.
I decided to try and modify the registry after reading this article: https://cmatskas.com/configure-a-runonce-task-on-windows/
my plan would be, within the second try-catch loop I will create a textfile called RecoveredPath.txt with the file path being its only contents, and then add something like:
Set-ItemProperty "HKLMU:\Software\Microsoft\Windows\CurrentVersion\RunOnce" -Name '!RecoverReport' -Value "C:\...EmergencyRecovery.bat"
Before rebooting. Within the batch file I have:
set /p RecoveredDir=<RecoveredPath.txt
powershell.exe -File C:\...Report.ps1 %RecoveredDir%
When I try to run the batch script, it doesn't yield any errors but doesn't seem to do anything. I tried adding in an echo statement and it is storing the value of the text file as a variable but doesn't seem to be passing it to powershell correctly. I also tried adding -Path %RecoveredDir% but that yielded an error (the param in report.ps1 is named $Path).
What am I doing incorrectly?
One potential problem is that not enclosing %RecoveredDir% in "..." would break with paths containing spaces and other special chars.
However, the bigger problem is that using mere file name RecoveredPath.txt means that the file is looked for in whatever the current directory happens to be.
In a comment your state that both the batch file and input file RecoveredPath.txt are located in your desktop folder.
However, it is not the batch file's location that matters, it's the process' current directory - and that is most likely not your desktop when your batch file auto-runs on startup.
Given that the batch file and the input file are in the same folder and that you can refer to a batch file's full folder path with %~dp0 (which includes a trailing \), modify your batch file to look as follows:
set /p RecoveredDir=<"%~dp0RecoveredPath.txt"
powershell.exe -File C:\...Report.ps1 "%RecoveredDir%"

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" ....

Running a perl script in a batch file

i have a perl script that is used in updating my awstats logs of my website. The script works fine if i just paste it in cmd (Windows) but the moment i paste it in a batch file, it messes up the format of the files generated (they should be prepended with current date/time). The code is:
perl C:\PROGRA~2\AWStats\tools\awstats_buildstaticpages.pl -config=mywebsite -update -awstatsprog=C:\PROGRA~2\AWStats\wwwroot\cgi-bin\awstats.pl -dir=C:\myfolder\stats\reports -builddate=%YYYY%MM -buildpdf=C:\PROGRA~2\HTMLDOC\ghtmldoc.exe -staticlinksext=asp`
The resulting files generated is mysite.201008.asp if i paste it in cmd and execute BUT In a batch file with the same script, my resulting file is mysite.MM.asp.
Any idea why this is happening?
The problem is caused by %YYYY%MM.
"%" is a special symbol in batch files. You need to escape it by doubling it: %%YYYY%%MM.
It appears you have to escape the '%' characters.
The command shell doing variable substitution on %YYYY% which I'm guessing is not defined in your environment, so it substitutes the empty string for that "variable".
Unfortunately, there are no opaque quotes in the Windows shell.