Random tips for PowerShell, Bash & AWS
- 📅
- 📝 647 words
- 🕙 3 minutes
- 📦 Development
- 🏷️ AWS, PowerShell
Now that I am again freelancing, I find myself solving unusual issues, many of which had no online solutions.
Given these no doubt plague other developers, let’s share!
Pass quoted args from BAT/CMD files to PowerShell
Grabbing args from a batch/command files is easy — use %*
— but have you ever tried passing them to PowerShell like:
powershell "Something" "%*"
Unfortunately, if one of your arguments has quotes around it (a filename containing a space perhaps), it becomes two separate arguments. e.g. "My File.txt"
now becomes My
and File.txt
.
PowerShell will only preserve it if you use the -f option (to run a .PS1 file) and that requires a relaxed policy via Set-ExecutionPolicy
so is a no-go for many people.
Given you can’t make PowerShell do the right thing with the args the trick here is — to not pass them as args at all!
SET MYPSARGS=%*
...
powershell -ArgumentList "$env:MYPSARGS"
Get Bash script path as Windows path
While Cygwin ships with cygpath
to convert /c/something
to c:\Something
etc. MSYS Bash shells do not have this. You can get it another way there however:
#!/bin/sh
pushd "$(dirname "$0")" > /dev/null
if command -v "cygpath" > /dev/null; then
WINPWD=""$(cygpath . -a -w)""
else
WINPWD=""$(pwd -W)""
fi
popd > /dev/null
echo $WINPWD
This solution works by switching the working directory to the one the script is in "$(dirname "$0")"
and then capturing the print-working-directory command output using the -W option that grabs it in Windows format. It then pops the working directory to make sure it goes back to where it was.
Note that this uses forward slashes as a directory separator still. Many tools and apps are okay with that but some older ones are not.
JSON encoding in API Gateway mapping templates
If you use Amazon’s AWS Lambda you’ll also find yourself touching API Gateway. While most of it is great, the mapping templates are deficient in that they do not encode output by default despite specifying the MIME types.
All of Amazon’s example templates are exploitable via JSON injection. Just put a double-quote in a field and start writing any JSON payload.
Amazon must fix this — encode by default like other templating systems have done, such as ASP.NET Razor. Until then some recommend the Amazon-provided $util.escapeJavaScript()
however while it encodes "
as \"
it also produces illegal JSON by encoding '
as \'
.
The mapping language is Apache Velocity Template Language (VTL), and while not extendable, the fine print reveals that it internally uses Java strings and does not sandbox them which let’s us utilize Java’s replace functionality:
#set($i = $input.path('$'))
{
"safeString": "$i.unsafeString.replaceAll("\""", "\\""")
}
Show active known IPs on the local network
I’m surprised more people don’t know how useful arp -a
is, especially if you pipe it into ping…
Bash
arp -a | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | xargs -L1 ping -c 1 -t 1 | sed -n -e 's/^.*bytes from //p'
PowerShell
(arp -a) -match "dynamic" | Foreach { ping -n 1 -w 1000 ($_ -split "\s+")[1] } | where { $_ -match "Reply from " } | % { $_.replace("Reply from ","") }
Wrapping up
I just want to mention that if you are doing anything on a command-line, be it Bash, OS X, PowerShell or Command/Batch then SS64 is a site worth visiting as they have great docs on many of these things!
[)amien
0 responses to Random tips for PowerShell, Bash & AWS