PowerShell V3 Geek Week

Labcenter is hosting a PowerShell V3 Geek Week June, 11 to June, 15 in Stockholm, Sweden.

The course covers new features in Windows PowerShell V 3.0, and a large number of techniques you can use to automate your Windows environments. If you are you working on Microsoft platforms and want to be in the forefront with the latest technology from Microsoft, this is a lab you just can’t miss.

If your interested in taking the course, check out this link: http://www.labcenter.se/Labs#lab=Windows_PowerShell_V3_Geekweek

Rating 4.33 out of 5
[?]

Grouping your Snippets in PowerShell ISE

One way of grouping your snippets in PowerShell ISE is to create a custom Add-On menu item. The example below demonstrates a function that groups your snippets based on the author (company) who wrote it. If the author property is empty, the snippets will be placed under a “unknown” submenu.

Clicking on Add-Ons/Grouped Snippets gives you the view shown above.

If you click on a specific Snippet, the Snippet code will be placed in your current file.

The following function is used to display a custom “Grouped Snippets” Menu Item.

 

function Group-TSSnippet {
  $menuText = "Grouped Snippets"
  $psISE.PowerShellTabs | ForEach-Object {
    $psTab = $_
    [array]$psTab.AddOnsMenu.Submenus |
     Where-Object {$_.DisplayName -eq $menuText} |
      ForEach-Object {
       $psTab.AddOnsMenu.Submenus.Remove($_) | Out-Null
      }

    $menu = $pstab.AddOnsMenu.Submenus.Add($menuText, $null, $null)
    $psTab.Snippets |
     Group-Object Author | Select Name |
      ForEach-Object {
      if([string]::IsNullOrEmpty($_.Name)) {
        $name = "Unknown"
      } else {
        $name = $_.Name
      }
      $menu.Submenus.Add($name,$null,$null) | Out-Null
    }
    foreach($snippet in $psTab.Snippets) {
      if([string]::IsNullOrEmpty($snippet.Author)) {
        $name = "Unknown"
      } else {
        $name = $snippet.Author
      }
      # Get Snippy Group
      $subMenu = $menu.Submenus | Where-Object { $_.DisplayName -eq $name }
      $codeFragment = "`n@'`n" + $snippet.CodeFragment + "`n'@`n"
      $action = '$psise.CurrentFile.Editor.InsertText(' +  $codeFragment + ')'
      $subMenu.Submenus.Add(
        $snippet.DisplayTitle,[scriptblock]::Create($action), $null
      ) | Out-Null
    }
  }
}

 

You can run the function by simply typing:

 

 PS > Group-TSSnippet 

 

Click here to download the function

Rating 4.00 out of 5
[?]

How to Search Spotify Using PowerShell & Spotify’s Metadata API

Windows PowerShell V3 includes a command, Invoke-WebRequest, which we can use to grab information using Spotify’s Metadata API.


PS > Invoke-WebRequest http://ws.spotify.com/search/1/track.json?q=Black+Label+Society


StatusCode        : 200
StatusDescription : OK
Content           : {"info": {"num_results": 417, "limit": 100, ..
                    "spotify:album:396vQgjfPsC0lisMX...
RawContent        : HTTP/1.1 200 OK
                    Vary: Accept-Charset
                    X-Varnish: 1580131781 1580105616
                    Age: 1908
                    Access-Control-Allow-Origin: *
                    Content-Length: 51642
                    Content-Type: application/json; charset=utf-8
                    Date: Thu, 01 ...
Forms             : {}
Headers           : {[Vary, Accept-Charset], [X-Varnish, 1580131..
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 51642

Let's check out the information in the Content property.


PS > Invoke-WebRequest http://ws.spotify.com/search/1/track.json?q=Black+Label+Society | 
  Select-Object -ExpandProperty Content


{"info": {"num_results": 417, "limit": 100, "offset": 0, 
"query": "Black Label Society", "type": "track", "page": 1}, 
"tracks": [{"album": {"released": "2009", "href": 
"spotify:album:396vQgjfP
sC0lisMXkrQ2A", "name": "Skullage", "availability": 
{"territories": "AT CH DE DK FI IS IT NO PT SE"}}, "name": 
"In This River", "popularity": "0.56160", "external-ids": 
[{"type": "isrc", "id": "USAR50451581"}], "length": 
235.466, "href": "spotify:track:1LgNG6JFuM3E4......

The output from isn’t as nicely formatted as you might have hoped, however, using the cmdlet ConvertFrom-Json converts the json document to usable object.


PS > $bls = Invoke-WebRequest http://ws.spotify.com/search/1/track.json?q=Black+Label+Society | 
Select-Object -ExpandProperty Content |
ConvertFrom-Json

Now we can simply call on a property and the information is displayed nicely formatted.


PS > $bls.info

num_results : 417
limit       : 100
offset      : 0
query       : Black Label Society
type        : track
page        : 1

Here’s an example on how you can display information about each track


PS > $bls.tracks

album        : @{released=2009; href=spotify:album:396vQgjfPsC0lisMXkrQ2A; name=Skullage; availability=}
name         : In This River
popularity   : 0.56160
external-ids : {@{type=isrc; id=USAR50451581}}
length       : 235.466
href         : spotify:track:1LgNG6JFuM3E4R7YsJgdf5
artists      : {@{href=spotify:artist:0zfT626RwO6zN3RDYeRit5; name=Black Label Society}, @{href=spotify:artist:1AeC9AuzqGc3IXMC2T5xny; name=Zakk Wylde}}
track-number : 10

album        : @{released=2010; href=spotify:album:7yji7GJSmvIpjMoEykLQq7; name=Order Of The Black; availability=}
name         : Crazy Horse
popularity   : 0.48597
external-ids : {@{type=isrc; id=USKO11000885}}
length       : 246.2
href         : spotify:track:2quyhkBRqiv56dQcfuLops
artists      : {@{href=spotify:artist:0zfT626RwO6zN3RDYeRit5; name=Black Label Society}}
track-number : 1

Rating 4.60 out of 5
[?]