Skip to content

Instantly share code, notes, and snippets.

@skunkie
Created March 13, 2018 09:03
Show Gist options
  • Save skunkie/74b6ea65b632079e5e7e2a1ba6fa95cb to your computer and use it in GitHub Desktop.
Save skunkie/74b6ea65b632079e5e7e2a1ba6fa95cb to your computer and use it in GitHub Desktop.
Wrap a PowerShell script into BAT/CMD file
# 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