Execute Powershell script in temporary folder

Currently I'm trying to create an update that executes a powershell script after the update is complete.

Due to the fact, that this doesn't work out of the box, I created a .cmd file that starts powershell like mentioned in this SO answer and added both files (.cmd and .ps1) to the temporary folder within wyBuild.

The .cmd file is marked to be executed and on the .ps1 I don't have any options. When I now run the update it executes the .cmd file, but afterwards it opens the .ps1 file in notepad. I would guess, this happens, cause wyBuild opens all files within the temporary folder with the default action registered in windows. And for a .ps1 file this is open in notepad.

So how can I prevent that wyBuild executes the default action for a file provided within the temporary folder or how can I execute a .ps1 file without that annoying side effect?

After some research I found an ugly workaround.

I removed the .ps1 file from the temporary folder and within the .cmd file I use the parameter -EncodedCommand instead of -File to directly inject the desired script into the batch file.

But getting the script as encoded command is a little bit tricky, cause it has to be unicode (two-byte chars) as Base64 string and not UTF-8 (one-byte chars), so Notepad++ can't be used for decoding.

Also an encoded command ignores line-breaks, which means, the commands have to end with a semicolon, to separate them.

So first take your .ps1 file and put a semicolon at the end of each command, then use the following script to convert it to base64:

$command = Get-Content .\myScript.ps1$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)$encodedCommand = [Convert]::ToBase64String($bytes)$encodedCommand

Copy the output of the file into your batch file:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -EncodedCommand ""...content of $encodedCommand...""' -Verb RunAs}"

And then you can execute your batch file which runs the desired Powershell script. Quite lengthy and not comfortable, but it works.

If anyone has a better idea, you're welcome.

We use a batch file + a powershell script file in the temporary folder that calls powershell - without encoding - like this:command line switch on the batchfile: %wu-temp% "%basedir%" first param the temporary working directory of wyupdate (%temp%\w....), second param the folder where wyupdate/your solution is running in

content batchfile (checking whether last character is backslash):====================SET execPath=%~2IF %execPath:~-1%==\ SET execPath=%execPath:~0,-1%powershell -file "%~1powershellfile.ps1" -executingScriptDirectory "%execPath%"====================content powershell script:====================param( [string]$executingScriptDirectory = "yoursolutionfolder")

$executingScriptDirectory=(get-item -Path $executingScriptDirectory).fullname

do your stuff ....=======================