2

Using information I found here, I am still unable to get this to work.

I have created the following .bat file, to call the PowerShell script on Windows 10 Professional.

for /f "delims=" %%a in ('powershell.exe -executionpolicy remotesigned ".\dang.ps1"') do (set "$crap=%%a")
powershell.exe .\dang.ps1
echo %$crap%

You will notice that it calls the PowerShell script two different ways.

This is the PowerShell script which is being launched from the batch file.

$crap="Pain in my butt"
return $crap

The strange thing is about this. When the batch file tries to launch the PowerShell script from the for /f loop, I receive the following error message:

C:\Users\pgmrd\My Drive\DLSR Notes and backup\Software\Scripts>(set "$crap=program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again." )

C:\Users\pgmrd\My Drive\DLSR Notes and backup\Software\Scripts>(set "$crap=At line:1 char:1" )

C:\Users\pgmrd\My Drive\DLSR Notes and backup\Software\Scripts>(set "$crap=+ .\dang.ps1

:\Users\pgmrd\My Drive\DLSR Notes and backup\Software\Scripts>(set "$crap=+ ~~~~~~~~~~~" )

C:\Users\pgmrd\My Drive\DLSR Notes and backup\Software\Scripts>(set "$crap=    + CategoryInfo          : ObjectNotFound: (.\dang.ps1String) [], CommandNotFoundException" )

C:\Users\pgmrd\My Drive\DLSR Notes and backup\Software\Scripts>(set "$crap=    + FullyQualifiedErrorId : CommandNotFoundException" )

C:\Users\pgmrd\My Drive\DLSR Notes and backup\Software\Scripts>(set "$crap= " )

BUT, when I launch the same PowerShell script, (see last line of batch file), without using the for /f, it has no problem executing the script.

C:\Users\pgmrd\My Drive\DLSR Notes and backup\Software\Scripts>powershell.exe .\dang.ps1
Pain in my butt

The scripts I typed are very stripped down so that I could concentrate on making it work. BUT, the final batch file runs some backups. Will launch the PowerShell script to read a xml file and return a file path. Then use that file path in the rest of the backup batch file.

Compo
  • 36,585
  • 5
  • 27
  • 39

1 Answers1

3
:: Note the use of -File
for /f "delims=" %%a in ('powershell.exe -executionpolicy remotesigned -File ".\dang.ps1"') do set "$crap=%%a"

Use the -File parameter of powershell.exe, the Windows PowerShell CLI, when invoking a script file (.ps1), which ensures that:

  • script file paths with spaces are correctly recognized
  • all subsequent arguments are passed through to the script without additional interpretation (except possibly by the calling shell).

Note that pwsh, the PowerShell (Core) 7+ CLI, now defaults to -File, whereas powershell.exe defaults to -Command, which is what happened in your case.

  • Passing a "..."-enclosed .ps1 script path to -Command is not enough, because unescaped " chars. are stripped before PowerShell interprets the resulting argument(s) as PowerShell code - see this answer for details.
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Thank you so much mklement0, I was feeling stupid. Still do actually. I thought I tried the -file before. But I don't think I wrapped .\dang.ps1 in ". This just made my powershell script very portable between different dos batch files. I was going to write the output to a file and then read that, but that just leaves junk files laing around. And there is a ton of information in that xml file that I can use for other applications. Again, thank you.. Dan – OldAndRetired Aug 16 '23 at 00:51
  • Glad to hear it helped, @OldAndRetired; my pleasure. Note that you can help future readers by clearly signaling which answer, if any, solved your problem, namely by [accepting](https://meta.stackexchange.com/a/5235/248777) it. – mklement0 Aug 16 '23 at 01:27