Using PowerShell to quickly write repetitive code
i started using powershell couple of months ago, have grown love it. want share 1 useful thing i've been doing it: using of cardinal features write code in other languages. in short, use piping, intuitive enumeration, , expandable strings generate lines of repetitive code slight variations. 2 frequent examples variable declarations , case statements. key points:
- use implicit array creation.
- to generate numbered for-loops, whether or not use counter variable
- to bundle list of inputs, strings
- use foreach commandlet
- use expandable strings
- use clip utility pipe output straight clipboard
example one: declaring bunch of constants in actionscript (flash) class:
$i = 0; "new_game", "new_level", "wait_for_close", "title", "instructions", "game_over" | ` foreach { "public static const state_system_$($_.toupper())`:int = $i;"; $i++ } | clip
output (already in clipboard):
public static const state_system_new_game:int = 0; public static const state_system_new_level:int = 1; public static const state_system_wait_for_close:int = 2; public static const state_system_title:int = 3; public static const state_system_instructions:int = 4; public static const state_system_game_over:int = 5;
example two: generate case statement c#: these commands issued interactively (i use powershell ise), since pasting output in your ide in between:
$animals = ("bear", "cheetah", "monkey", "cobra", "penguin", "lion") #define enum "public enum animals {`r`n`t" + [string]::join(",`r`n`t", $animals ) + "`r`n}" | clip #generate switch case $animals | foreach { @" case animals.$_`: feedanimals($($_)array); break; "@ } | clip
output:
// generated enum: public enum animals { bear, cheetah, monkey, cobra, penguin, lion } public function feedanimals(animals animal) // enum overload { switch(animal) { // generated case statments: case animals.bear: feedanimals(beararray); break; case animals.cheetah: feedanimals(cheetaharray); break; case animals.monkey: feedanimals(monkeyarray); break; case animals.cobra: feedanimals(cobraarray); break; case animals.penguin: feedanimals(penguinarray); break; case animals.lion: feedanimals(lionarray); break; } }
example three: generate xml: here creating list of 365 generic day elements random number of appointment elements:
[xml]$caltest = new-object xml $calroot = $caltest.appendchild($caltest.createelement("calendarroot")) $firstday = get-date "1/1/2011" $r = new-object random (0..364) | foreach { $day = $calroot.appendchild($caltest.createelement("day")); $day.setattribute("date", ($firstday.adddays($_)).tostring("yyyy-mm-dd")); (0..$r.next(1,5)) | foreach { $appt = $day.appendchild($caltest.createelement("appointment")); $appt.setattribute("id", $_.tostring() ); } } $caltest.save("c:\temp\caltest.xml")
jmh
in case haven't figured out yet... if want create string array, instead of typing quotes, because object , strings have bunch of useful methods, might easier like
$nums="1 2 3 4 5 6".split() or
$nums="1,2,3,4,5".split(",") if need spaces in elements.
"" | get-member (to see methods of string)
Windows Server > Windows PowerShell
Comments
Post a Comment