[{"data":1,"prerenderedAt":728},["ShallowReactive",2],{"blog:2016:developer-tips":3,"blogMore-Development":714,"comments-developer-tips":727},{"id":4,"title":5,"body":6,"category":697,"commentCount":698,"date":699,"description":12,"excerpt":700,"extension":701,"filenames":702,"hidden":703,"image":702,"meta":704,"minutes":188,"navigation":705,"path":706,"seo":707,"showCategory":702,"stem":708,"tags":709,"updated":702,"url":711,"wordCount":712,"__hash__":713},"content\u002Fblog\u002F2016\u002Fdeveloper-tips.md","Random tips for PowerShell, Bash & AWS",{"type":7,"value":8,"toc":687},"minimark",[9,13,16,21,29,39,54,61,64,70,74,89,274,281,284,288,291,302,336,345,438,442,449,453,523,527,664,668,677,683],[10,11,12],"p",{},"Now that I am again freelancing, I find myself solving unusual issues, many of which had no online solutions.",[10,14,15],{},"Given these no doubt plague other developers, let’s share!",[17,18,20],"h2",{"id":19},"pass-quoted-args-from-batcmd-files-to-powershell","Pass quoted args from BAT\u002FCMD files to PowerShell",[10,22,23,24,28],{},"Grabbing args from a batch\u002Fcommand files is easy (use ",[25,26,27],"code",{},"%*",") but have you ever tried passing them to PowerShell like:",[30,31,36],"pre",{"className":32,"code":34,"language":35},[33],"language-text","powershell \"Something\" \"%*\"\n","text",[25,37,34],{"__ignoreMap":38},"",[10,40,41,42,45,46,49,50,53],{},"Unfortunately, if one of your arguments has quotes around it (a filename containing a space perhaps), it becomes two separate arguments. e.g. ",[25,43,44],{},"\"My File.txt\""," now becomes ",[25,47,48],{},"My"," and ",[25,51,52],{},"File.txt",".",[10,55,56,57,60],{},"PowerShell will only preserve it if you use the -f option (to run a .PS1 file) and that requires a relaxed policy via ",[25,58,59],{},"Set-ExecutionPolicy"," so is a no-go for many people.",[10,62,63],{},"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!",[30,65,68],{"className":66,"code":67,"language":35},[33],"SET MYPSARGS=%*\n...\npowershell -ArgumentList \"$env:MYPSARGS\"\n",[25,69,67],{"__ignoreMap":38},[17,71,73],{"id":72},"get-bash-script-path-as-windows-path","Get Bash script path as Windows path",[10,75,76,77,80,81,84,85,88],{},"While Cygwin ships with ",[25,78,79],{},"cygpath"," to convert ",[25,82,83],{},"\u002Fc\u002Fsomething"," to ",[25,86,87],{},"c:\\Something"," etc. MSYS Bash shells do not have this. You can get it another way there however:",[30,90,94],{"className":91,"code":92,"language":93,"meta":38,"style":38},"language-bash shiki shiki-themes everforest-light dracula","#!\u002Fbin\u002Fsh\npushd \"$(dirname \"$0\")\" &gt; \u002Fdev\u002Fnull\nif command -v \"cygpath\" &gt; \u002Fdev\u002Fnull; then\n  WINPWD=\"\"$(cygpath . -a -w)\"\"\nelse\n  WINPWD=\"\"$(pwd -W)\"\"\nfi\npopd &gt; \u002Fdev\u002Fnull\necho $WINPWD\n","bash",[25,95,96,105,150,186,219,225,246,252,264],{"__ignoreMap":38},[97,98,101],"span",{"class":99,"line":100},"line",1,[97,102,104],{"class":103},"sSX4p","#!\u002Fbin\u002Fsh\n",[97,106,108,112,116,120,124,127,131,133,136,138,141,144,147],{"class":99,"line":107},2,[97,109,111],{"class":110},"s6Vpi","pushd ",[97,113,115],{"class":114},"sciFF","\"",[97,117,119],{"class":118},"sFDqs","$(",[97,121,123],{"class":122},"sS4Kt","dirname",[97,125,126],{"class":114}," \"",[97,128,130],{"class":129},"s7cAX","$0",[97,132,115],{"class":114},[97,134,135],{"class":118},")",[97,137,115],{"class":114},[97,139,140],{"class":110}," &",[97,142,143],{"class":122},"gt",[97,145,146],{"class":110},"; ",[97,148,149],{"class":122},"\u002Fdev\u002Fnull\n",[97,151,153,157,161,165,167,170,172,174,176,178,181,183],{"class":99,"line":152},3,[97,154,156],{"class":155},"smiwp","if",[97,158,160],{"class":159},"saSZQ"," command",[97,162,164],{"class":163},"sQ7z_"," -v",[97,166,126],{"class":114},[97,168,79],{"class":169},"sJQOs",[97,171,115],{"class":114},[97,173,140],{"class":110},[97,175,143],{"class":122},[97,177,146],{"class":110},[97,179,180],{"class":122},"\u002Fdev\u002Fnull",[97,182,146],{"class":110},[97,184,185],{"class":155},"then\n",[97,187,189,193,197,200,202,204,208,211,214,216],{"class":99,"line":188},4,[97,190,192],{"class":191},"s2G2r","  WINPWD",[97,194,196],{"class":195},"s9HRq","=",[97,198,199],{"class":114},"\"\"",[97,201,119],{"class":110},[97,203,79],{"class":122},[97,205,207],{"class":206},"sJFhe"," .",[97,209,210],{"class":163}," -a",[97,212,213],{"class":163}," -w",[97,215,135],{"class":110},[97,217,218],{"class":114},"\"\"\n",[97,220,222],{"class":99,"line":221},5,[97,223,224],{"class":155},"else\n",[97,226,228,230,232,234,236,239,242,244],{"class":99,"line":227},6,[97,229,192],{"class":191},[97,231,196],{"class":195},[97,233,199],{"class":114},[97,235,119],{"class":110},[97,237,238],{"class":159},"pwd",[97,240,241],{"class":163}," -W",[97,243,135],{"class":110},[97,245,218],{"class":114},[97,247,249],{"class":99,"line":248},7,[97,250,251],{"class":155},"fi\n",[97,253,255,258,260,262],{"class":99,"line":254},8,[97,256,257],{"class":110},"popd &",[97,259,143],{"class":122},[97,261,146],{"class":110},[97,263,149],{"class":122},[97,265,267,270],{"class":99,"line":266},9,[97,268,269],{"class":159},"echo",[97,271,273],{"class":272},"s3Ipq"," $WINPWD\n",[10,275,276,277,280],{},"This solution works by switching the working directory to the one the script is in ",[25,278,279],{},"\"$(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.",[10,282,283],{},"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.",[17,285,287],{"id":286},"json-encoding-in-api-gateway-mapping-templates","JSON encoding in API Gateway mapping templates",[10,289,290],{},"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.",[10,292,293,294,301],{},"All of ",[295,296,300],"a",{"href":297,"rel":298},"https:\u002F\u002Fdocs.aws.amazon.com\u002Fapigateway\u002Flatest\u002Fdeveloperguide\u002Fmodels-mappings.html",[299],"nofollow","Amazon’s example templates are exploitable"," via JSON injection. Just put a double-quote in a field and start writing any JSON payload.",[10,303,304,308,309,312,313,315,316,319,320,323,324,329,330,315,333,207],{},[305,306,307],"strong",{},"Amazon must fix this and encode by default"," like other templating systems have done, such as ASP.NET Razor. Until then some recommend the Amazon-provided ",[25,310,311],{},"$util.escapeJavaScript()"," however while it encodes ",[25,314,115],{}," as ",[25,317,318],{},"\\\""," it also produces ",[305,321,322],{},"illegal"," ",[295,325,328],{"href":326,"rel":327},"https:\u002F\u002Fwww.json.org\u002F",[299],"JSON"," by encoding ",[25,331,332],{},"'",[25,334,335],{},"\\'",[10,337,338,339,344],{},"The mapping language is ",[295,340,343],{"href":341,"rel":342},"https:\u002F\u002Fvelocity.apache.org\u002Fengine\u002Fdevel\u002Fvtl-reference-guide.html",[299],"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:",[30,346,350],{"className":347,"code":348,"language":349,"meta":38,"style":38},"language-javascript shiki shiki-themes everforest-light dracula","#set($i = $input.path('$'))\n{\n   \"safeString\": \"$i.unsafeString.replaceAll(\"\\\"\"\", \"\\\\\"\"\")\n}\n","javascript",[25,351,352,388,393,433],{"__ignoreMap":38},[97,353,354,357,360,363,365,368,371,374,377,380,383,385],{"class":99,"line":100},[97,355,356],{"class":110},"#",[97,358,359],{"class":122},"set",[97,361,362],{"class":110},"($i ",[97,364,196],{"class":195},[97,366,367],{"class":110}," $input",[97,369,53],{"class":370},"sdCVw",[97,372,373],{"class":122},"path",[97,375,376],{"class":110},"(",[97,378,332],{"class":379},"spsLl",[97,381,382],{"class":206},"$",[97,384,332],{"class":379},[97,386,387],{"class":110},"))\n",[97,389,390],{"class":99,"line":107},[97,391,392],{"class":110},"{\n",[97,394,395,398,401,403,406,408,411,413,416,419,422,424,427,429],{"class":99,"line":152},[97,396,397],{"class":379},"   \"",[97,399,400],{"class":206},"safeString",[97,402,115],{"class":379},[97,404,405],{"class":110},": ",[97,407,115],{"class":379},[97,409,410],{"class":206},"$i.unsafeString.replaceAll(",[97,412,115],{"class":379},[97,414,415],{"class":110},"\\",[97,417,418],{"class":379},"\"\"\"",[97,420,421],{"class":206},", ",[97,423,115],{"class":379},[97,425,426],{"class":110},"\\\\",[97,428,418],{"class":379},[97,430,432],{"class":431},"sMoiV",")\n",[97,434,435],{"class":99,"line":188},[97,436,437],{"class":110},"}\n",[17,439,441],{"id":440},"show-active-known-ips-on-the-local-network","Show active known IPs on the local network",[10,443,444,445,448],{},"I’m surprised more people don’t know how useful ",[25,446,447],{},"arp -a"," is, especially if you pipe it into ping…",[450,451,452],"h3",{"id":93},"Bash",[30,454,456],{"className":91,"code":455,"language":93,"meta":38,"style":38},"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\u002F^.*bytes from \u002F\u002Fp'\n",[25,457,458],{"__ignoreMap":38},[97,459,460,463,465,468,471,474,477,480,482,484,487,490,493,496,499,502,504,506,509,512,515,517,520],{"class":99,"line":100},[97,461,462],{"class":122},"arp",[97,464,210],{"class":163},[97,466,467],{"class":195}," |",[97,469,470],{"class":122}," grep",[97,472,473],{"class":163}," -o",[97,475,476],{"class":114}," '",[97,478,479],{"class":169},"[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}",[97,481,332],{"class":114},[97,483,467],{"class":195},[97,485,486],{"class":122}," xargs",[97,488,489],{"class":163}," -L1",[97,491,492],{"class":206}," ping",[97,494,495],{"class":163}," -c",[97,497,498],{"class":272}," 1",[97,500,501],{"class":163}," -t",[97,503,498],{"class":272},[97,505,467],{"class":195},[97,507,508],{"class":122}," sed",[97,510,511],{"class":163}," -n",[97,513,514],{"class":163}," -e",[97,516,476],{"class":114},[97,518,519],{"class":169},"s\u002F^.*bytes from \u002F\u002Fp",[97,521,522],{"class":114},"'\n",[450,524,526],{"id":525},"powershell","PowerShell",[30,528,531],{"className":529,"code":530,"language":525,"meta":38,"style":38},"language-powershell shiki shiki-themes everforest-light dracula","(arp -a) -match \"dynamic\" | Foreach { ping -n 1 -w 1000 ($_ -split \"\\s+\")[1] } | where { $_ -match \"Reply from \" } | % { $_.replace(\"Reply from \",\"\") }\n",[25,532,533],{"__ignoreMap":38},[97,534,535,538,541,544,547,549,552,554,557,560,563,565,568,571,574,577,580,583,586,589,592,594,597,599,602,604,607,610,613,616,618,620,623,625,628,630,633,635,638,640,642,644,648,650,652,654,656,659,661],{"class":99,"line":100},[97,536,537],{"class":110},"(arp ",[97,539,540],{"class":195},"-",[97,542,543],{"class":110},"a) ",[97,545,546],{"class":195},"-match",[97,548,126],{"class":114},[97,550,551],{"class":169},"dynamic",[97,553,115],{"class":114},[97,555,467],{"class":556},"s6jqV",[97,558,559],{"class":155}," Foreach",[97,561,562],{"class":110}," { ping ",[97,564,540],{"class":195},[97,566,567],{"class":110},"n ",[97,569,570],{"class":272},"1",[97,572,573],{"class":195}," -",[97,575,576],{"class":110},"w ",[97,578,579],{"class":272},"1000",[97,581,582],{"class":110}," (",[97,584,382],{"class":585},"svV8Q",[97,587,588],{"class":191},"_",[97,590,591],{"class":195}," -split",[97,593,126],{"class":114},[97,595,596],{"class":169},"\\s+",[97,598,115],{"class":114},[97,600,601],{"class":110},")[",[97,603,570],{"class":272},[97,605,606],{"class":110},"] } ",[97,608,609],{"class":556},"|",[97,611,612],{"class":155}," where",[97,614,615],{"class":110}," { ",[97,617,382],{"class":585},[97,619,588],{"class":191},[97,621,622],{"class":195}," -match",[97,624,126],{"class":114},[97,626,627],{"class":169},"Reply from ",[97,629,115],{"class":114},[97,631,632],{"class":110}," } ",[97,634,609],{"class":556},[97,636,637],{"class":155}," %",[97,639,615],{"class":110},[97,641,382],{"class":585},[97,643,588],{"class":191},[97,645,647],{"class":646},"sSKRk",".replace",[97,649,376],{"class":110},[97,651,115],{"class":114},[97,653,627],{"class":169},[97,655,115],{"class":114},[97,657,658],{"class":556},",",[97,660,199],{"class":114},[97,662,663],{"class":110},") }\n",[17,665,667],{"id":666},"wrapping-up","Wrapping up",[10,669,670,671,676],{},"I just want to mention that if you are doing anything on a command-line, be it Bash, OS X, PowerShell or Command\u002FBatch then ",[295,672,675],{"href":673,"rel":674},"https:\u002F\u002Fss64.com",[299],"SS64"," is a site worth visiting as they have great docs on many of these things!",[10,678,679],{},[680,681,682],"em",{},"[)amien",[684,685,686],"style",{},"html pre.shiki code .sSX4p, html code.shiki .sSX4p{--shiki-default:#939F91;--shiki-default-font-style:italic;--shiki-dark:#6272A4;--shiki-dark-font-style:inherit}html pre.shiki code .s6Vpi, html code.shiki .s6Vpi{--shiki-default:#5C6A72;--shiki-dark:#F8F8F2}html pre.shiki code .sciFF, html code.shiki .sciFF{--shiki-default:#8DA101;--shiki-dark:#E9F284}html pre.shiki code .sFDqs, html code.shiki .sFDqs{--shiki-default:#5C6A72;--shiki-dark:#F1FA8C}html pre.shiki code .sS4Kt, html code.shiki .sS4Kt{--shiki-default:#8DA101;--shiki-dark:#50FA7B}html pre.shiki code .s7cAX, html code.shiki .s7cAX{--shiki-default:#5C6A72;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic}html pre.shiki code .smiwp, html code.shiki .smiwp{--shiki-default:#F85552;--shiki-dark:#FF79C6}html pre.shiki code .saSZQ, html code.shiki .saSZQ{--shiki-default:#DFA000;--shiki-dark:#8BE9FD}html pre.shiki code .sQ7z_, html code.shiki .sQ7z_{--shiki-default:#DFA000;--shiki-dark:#BD93F9}html pre.shiki code .sJQOs, html code.shiki .sJQOs{--shiki-default:#8DA101;--shiki-dark:#F1FA8C}html pre.shiki code .s2G2r, html code.shiki .s2G2r{--shiki-default:#5C6A72;--shiki-dark:#BD93F9}html pre.shiki code .s9HRq, html code.shiki .s9HRq{--shiki-default:#F57D26;--shiki-dark:#FF79C6}html pre.shiki code .sJFhe, html code.shiki .sJFhe{--shiki-default:#DFA000;--shiki-dark:#F1FA8C}html pre.shiki code .s3Ipq, html code.shiki .s3Ipq{--shiki-default:#DF69BA;--shiki-dark:#BD93F9}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sdCVw, html code.shiki .sdCVw{--shiki-default:#939F91;--shiki-dark:#F8F8F2}html pre.shiki code .spsLl, html code.shiki .spsLl{--shiki-default:#DFA000;--shiki-dark:#E9F284}html pre.shiki code .sMoiV, html code.shiki .sMoiV{--shiki-default:#DFA000;--shiki-default-font-style:inherit;--shiki-default-text-decoration:inherit;--shiki-dark:#FF5555;--shiki-dark-font-style:italic;--shiki-dark-text-decoration:underline}html pre.shiki code .s6jqV, html code.shiki .s6jqV{--shiki-default:#F57D26;--shiki-dark:#F8F8F2}html pre.shiki code .svV8Q, html code.shiki .svV8Q{--shiki-default:#939F91;--shiki-dark:#BD93F9}html pre.shiki code .sSKRk, html code.shiki .sSKRk{--shiki-default:#35A77C;--shiki-dark:#F8F8F2}",{"title":38,"searchDepth":107,"depth":107,"links":688},[689,690,691,692,696],{"id":19,"depth":107,"text":20},{"id":72,"depth":107,"text":73},{"id":286,"depth":107,"text":287},{"id":440,"depth":107,"text":441,"children":693},[694,695],{"id":93,"depth":152,"text":452},{"id":525,"depth":152,"text":526},{"id":666,"depth":107,"text":667},"Development",0,"2016-04-28T23:27:23+00:00","[object Object]","md",null,false,{},true,"\u002Fblog\u002F2016\u002Fdeveloper-tips",{"title":5,"description":12},"blog\u002F2016\u002Fdeveloper-tips",[710,526],"AWS","\u002Fblog\u002F2016\u002Fdeveloper-tips\u002F",792,"y6OQRYsHurT-Eq96ffrrtINtTvNig-sLHtk4rkJEEP0",[715,719,723],{"title":716,"date":717,"url":718},"Transactions in the MongoDB EF Core Provider","2025-10-25","\u002Fblog\u002F2025\u002Fmongodb-explicit-transactions\u002F",{"title":720,"date":721,"url":722},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","\u002Fblog\u002F2025\u002Fmongodb-queryable-encryption\u002F",{"title":724,"date":725,"url":726},"Lazy Loading with EF Core Proxies","2025-04-02","\u002Fblog\u002F2025\u002Fef-proxies\u002F",[],1780900526295]