forked from fleschutz/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-ps2bat.ps1
executable file
·47 lines (42 loc) · 1.22 KB
/
convert-ps2bat.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<#
.SYNOPSIS
Converts PowerShell scripts to batch files
.DESCRIPTION
This PowerShell script converts one or more PowerShell scripts to .bat batch files.
.PARAMETER Filepattern
Specifies the file pattern
.EXAMPLE
PS> ./convert-ps2bat *.ps1
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$Filepattern = "")
function Convert-PowerShellToBatch
{
param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]
[Alias("FullName")]
$Path
)
process
{
$encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $Path -Raw -Encoding UTF8)))
$newPath = [Io.Path]::ChangeExtension($Path, ".bat")
"@echo off`npowershell.exe -NoExit -encodedCommand $encoded" | Set-Content -Path $newPath -Encoding Ascii
}
}
try {
if ($Filepattern -eq "") { $Filepattern = read-host "Enter path to the PowerShell script(s)" }
$Files = get-childItem -path "$Filepattern"
foreach ($File in $Files) {
Convert-PowerShellToBatch "$File"
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}