-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.ps1
43 lines (31 loc) · 1.27 KB
/
demo.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
#
# Lighting Demo - Using PowerShell.exe -EncodedCommand argument
#
# Use your PowerShell Fu from batch scripts by encoding the PowerShell Command(s)
#
# Easy to do in PowerShell and from Command Prompt
(Get-Date).Hour
# Easy to do in PowerShell, not so easy from Command Prompt
"[Hour:Minute:Second] is [$((Get-Date).Hour):$((Get-Date).Minute):$((Get-Date).Second)]"
# Copy and paste the powershell.exe encoded command example
$command = '"[Hour:Minute:Second] is [$((Get-Date).Hour):$((Get-Date).Minute):$((Get-Date).Second)]"'
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
# ConvertTo-Batch Demo
. .\ConvertTo-Batch.ps1
ConvertTo-Batch -Command $command | clip
ConvertTo-Batch -Command $command -BatchEnvVarName HOUR_MIN_SEC | clip
# Batch is_admin.bat
ConvertTo-Batch -Command (Get-Content .\Test-ForAdmin.ps1 -Raw) -BatchEnvVarName IS_RUNNING_AS_ADMIN | clip
# Quick Recap
#
# 1. Practical - add some PowerShell to an existing batch file w/out rewriting the batch
#
# 2. Effective - doesn't get tripped up on quoting/escaping
#
# 3. Portable - allows you to embed PowerShell within a Batch File (no external PowerShell file)
#
# Get 'ConvertTo-Batch' function from my GitHub
#
# https://github.com/ryan-leap/PsBatch
#