Tuesday, October 30, 2012

"Hello" PowerShell

Playing around with Windows PowerShell today. As usual, the first thing that people learn is the infamous "Hello World" program. So I tried scripting an array of string in PS (plus color!)

PS C:\> 0..15 | % {Write-Host -ForegroundColor $_ "HELLO Robert Jin!!!"}

Here's a breakdown of what's happening:
  • 0..15 - This is a range operator with an array of numbers from 0 to 15
  • Write-Host - Operator to print text to the screen
  • -ForegroundColor - The color of the text
  • $_ - The numeric value that is required for the the previous command (ForegroundColor)
  • "<String>" - The string of text to display
ForegroundColor also have a few pre-set color schemes. Here's the list:

-ForegroundColor 0 = Black
-ForegroundColor 1 = Dark Blue
-ForegroundColor 2 = Dark Green
-ForegroundColor 3 = Dark Cyan
-ForegroundColor 4 = Dark Red
-ForegroundColor 5 = Dark Magenta
-ForegroundColor 6 = Dark Yellow (Not sure why this is showing as 'White')
-ForegroundColor 7 = Gray
-ForegroundColor 8 = Dark Gray
-ForegroundColor 9 = Blue
-ForegroundColor 10 = Green
-ForegroundColor 11 = Cyan
-ForegroundColor 12 = Red
-ForegroundColor 13 = Magenta
-ForegroundColor 14 = Yellow
-ForegroundColor 15 = White

Well, that's it for now =)

4 comments:

  1. Ah, now that you've started with PowerShell you can do some cool things with Tridion.

    When you have a moment, see Dominic Cronin's PowerShell stuff, visit the Practice wiki on restarting Tridion or working with AppData, and when ready try Andrey's PowerShell mediator.

    ReplyDelete
  2. Not to mention Peter Kjaer's work over at tridion-powershell-modules

    ReplyDelete
  3. Hey Robert,

    I'm want to use powershell to migrate metadata to Tridion in way that requires as little effort as possible - laziness is the driver of progress, you know?


    Do you think it is possible to set up a powershell script to:

    1- recognize an email (received in outlook) with specific keyword(s) in the subject line

    2- read the contents of the email (tab-separated metadata) and copy it to an excel/csv file and save it in a specified location on your computer/server

    3- recognize a new .csv file in the specified location on your computer/server and read the matadata (list of IDs) in the .csv file, then create a new component in Tridion with the "video ID field" populated with the IDs from the .csv file.


    Hopefully more than just video ID could be read form the email/.csv file.


    I have a feeling that reading and writing text from an email (in outlook) is possible using powershell and porting the 'metadata' to Tridon sounds do-able - but creating new components in the right location(s)/level(s) of Tridion sounds more complex, maybe that could be based on the subject line of the email?


    Powershell and Tridion sounds so good together!


    ReplyDelete