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:
  1. use implicit array creation.
    1. to generate numbered for-loops, whether or not use counter variable
    2. to bundle list of inputs, strings
  2. use foreach commandlet
  3. use expandable strings
  4. 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

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Event ID 64,77,1008 Certificates Events Windows Server 2008, 2008R2