Created
March 13, 2018 09:03
-
-
Save skunkie/74b6ea65b632079e5e7e2a1ba6fa95cb to your computer and use it in GitHub Desktop.
Wrap a PowerShell script into BAT/CMD 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
# 1. Remove lines containing only comments and empty lines | |
# 2. Remove a comment after some code, e.g. some code # a comment | |
# 3. Join a line with a trailing { to the next line, e.g. if (a condition) {$ | |
# 4. Join a line beginning with a } character to the previous line, e.g. ^}$, or ^ } | |
# 5. Replace the character ` when it is used to break code in two lines, with a space, e.g. somecode `$ | |
# 6. Replace all \r\n (carriage return and new line characters) with '; ' | |
$text = (Get-Content -Path .\SetStaticIP.ps1 | Where-Object {$_ -notmatch "^\s*?#|^$"} | ForEach-Object {$_.Trim()} | Out-String) ` | |
-replace '\s*?#.*?\r\n', '' ` | |
-replace '{\s*?\r\n\s*?', '{' ` | |
-replace '\r\n\s*?}', '}' ` | |
-replace '\s*?`\s*?\r\n\s*?', ' ' ` | |
-replace '\r\n', '; ' | |
$('@ECHO OFF' | |
'powershell.exe -Command "{0}"' -f $text | |
) | Out-File -Encoding ascii -FilePath .\SetStaticIP.cmd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment