Last active
June 10, 2025 07:07
-
-
Save mastersign/b5e2dfc12a3cccdce0ed5acbe79656ae to your computer and use it in GitHub Desktop.
Wrapping PowerShell script into Batch file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| :: Dieses Batch-File kann als Drop-Place für PS1-Dateien genutzt werden. | |
| :: Man kann eine PS1-Datei per Drag&Drop auf die CMD-Datei fallen lassen. | |
| :: Dadurch wird ConvertTo-BatchFile.ps1 aufgerufen und der Pfad der PS1-Datei als Parameter übergeben. | |
| :: Diese Datei muss im selben Ordner wie ConvertTo-BatchFile.ps1 liegen. | |
| :: Das Prinzip funktioniert auch für eine Windows-Verknüpfung auf diese CMD-Datei. | |
| @powershell -NoProfile -NoLogo -ExecutionPolicy RemoteSigned -File "%~dpn0.ps1" -PsFile "%~1" -Destination "%~dpn1.cmd" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Konvertiert ein PowerShell-Skript in eine Batch-Datei | |
| .DESCRIPTION | |
| Dieses Skript nimmt eine PowerShell-Datei entgegen und kopiert sie in eine | |
| Batch-Datei. Die Kopie wird dabei um einen Header ergänzt, der, als Batch | |
| aufgerufen, das ursprüngliche Skript wieder in eine temporäre Datei | |
| extrahiert und per PowerShell ausführt. | |
| .PARAMETER PsFile | |
| Name des zu konvertierenden PowerShell-Skripts | |
| .PARAMETER Destination | |
| Name und Pfad der resulierenden Batch-Datei. Wenn nicht angegeben, wird Name | |
| und Pfad der PowerShell-Datei übernommen und nur die Endung von .ps1 in .cmd | |
| geändert. | |
| .INPUTS | |
| Ein PowerShell-Skript | |
| .OUTPUTS | |
| Eine Batch-Datei, die das PowerShell-Skript enthält und sich wie jede andere | |
| Batch-Datei starten lässt. | |
| .NOTES | |
| Version: 1.1 | |
| Author: Hajo Schulz, hos@ct.de, Tobias Kiertscher | |
| Copyright: (C) 2021 Heise Medien GmbH & Co. KG, c't | |
| Creation Date: 2021/03/25 | |
| Purpose/Change: Initial script development | |
| .LINK | |
| https://gist.github.com/mastersign/b5e2dfc12a3cccdce0ed5acbe79656ae | |
| #> | |
| [CmdletBinding()] | |
| Param ( | |
| [parameter(Position=0, ValueFromPipeline, Mandatory=$true)] | |
| [String]$PsFile, | |
| [parameter(Position=1, Mandatory=$false)] | |
| [String]$Destination | |
| ) | |
| $inFile = Get-Item $PsFile -ErrorAction Stop | |
| $outFile = $Destination | |
| if (!$outFile) { | |
| $outFile = ($inFile.FullName) -replace '\.ps1$', '.cmd' | |
| } | |
| $text = Get-Content $inFile -Raw | |
| $header = @' | |
| @ECHO OFF | |
| %WINDIR%\System32\more +{0} "%~f0" > "%TEMP%\%~n0.ps1" | |
| powershell -NoProfile -ExecutionPolicy Bypass -File "%TEMP%\%~n0.ps1" %* | |
| SET ps_status=%ERRORLEVEL% | |
| DEL "%TEMP%\%~n0.ps1" | |
| EXIT /B %ps_status% | |
| *** Ab hier PowerShell *** | |
| '@ | |
| $header = $header -f ($header.Split("`n").Length - 1) | |
| $text = $header + $text | |
| Set-Content -Path $outFile -Value $text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment