Author Topic: Calling a function within a Powershell Script  (Read 221 times)

bettman

  • Newbie
  • *
  • Posts: 43
Calling a function within a Powershell Script
« on: August 19, 2023, 03:00:53 pm »
Greetings,

I have a powershell script which contains various functions, one of which is used to uninstall software. The command line to invoke this from a DOS batch file is as follows:

powershell.exe  -executionpolicy bypass-command "& { .'%~dp0Install_App.ps1'; UninstallApp }"

I have looked for a Winbatch example which describes how to do this but could not find one. I then thought of just invoking Powershell using Runwait (or something similar) but could not figure out how to format the bolded string section so Winbatch would accept it.

Any help would be appreciated.

td

  • Tech Support
  • *****
  • Posts: 4383
    • WinBatch
Re: Calling a function within a Powershell Script
« Reply #1 on: August 19, 2023, 07:35:19 pm »
There are multiple ways to properly escape the parameter line. Here is but one:

Code: Winbatch
str = "-executionpolicy bypass-command ""& { .'%%~dp0Install_App.ps1'; UninstallApp }"""
Message("Escaped Line", str)
exit



"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

  • Tech Support
  • *****
  • Posts: 4383
    • WinBatch
Re: Calling a function within a Powershell Script
« Reply #2 on: August 21, 2023, 07:08:41 am »
A bug in the forum's WinBatch Script rendering extension converts single quotes to their HTML equivalent so the the '  sequence is actually a single quote. It should be the following:

"-executionpolicy bypass-command ""& { .'%%~dp0Install_App.ps1'; UninstallApp }"""
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade