Use Invoke-WebRequest to get your physical location

Posted on Updated on

Hi all,

This is predominantly going to be a script sharing post.   I’ve got a fever for these APIs so expect to see me crank out a bunch of new cmdlettes in the next coming weeks.  I’ve already got one for finding a restaurant in your area using an awesome opentable-scraping API I found on heroku.  Anyway…send me requests, this is soo much fun!

Unlike my previous post, this API is totally free, so you can copy and paste and start using this code today!  Why did I want to do this?  Well in a conversation with a fellow redditor about a way to get Weather Info, he wondered how to do a GPS or geolocation lookup within the script, to have it update as he moved around.

I naturally had to see what I could do!

Read the rest of this entry »

Help your peers shine! Nominations for PowerShell Heroes are open

Aside Posted on

Do you know someone who has been helping the community, active on the forums, or otherwise kicking PowerShell butt and hasn’t been nominated for an MVP or other award?

PowerShell.org is rolling out their second round of nominations for PowerShell heroes.  Big congrats to Mark Schill and Francois Xavier Cat, both of whom I’ve spoken with and are well known around PowerShell circles!

Nominations are availble here: http://powershell.org/wp/2014/10/07/accepting-nominations-for-2015-powershell-heroes/

Get the daily forecast in your console, with PowerShell

Posted on Updated on

Today I saw a post on reddit about scraping a webpage for weather information. Reading over the comments, I had an idea, and I quickly looked away wanting to try this myself before seeing what someone else posted.

http://www.reddit.com/r/PowerShell/comments/2jitvn/help_with_screen_scraping_or_interpreting_web/

If you don’t care about how this is done, skip to the bottom for the code, here is a preview of the finished product:

weather_00
Read the rest of this entry »

Download all the PowerShell summit videos…with PowerShell

Aside Posted on

This is just a rehash of a previous post of using YoutubeDL to parse playlists and download stuff, but here is doing it to download the European PowerShell Summit Videos.

$url = "https://www.youtube.com/playlist?list=PLfeA8kIs7Coehjg9cB6foPjBojLHYQGb_"

$playlisturl = $url

$VideoUrls= (invoke-WebRequest -uri $Playlisturl).Links | ? {$_.HREF -like "/watch*"} |  `
? innerText -notmatch ".[0-9]:[0-9]." | ? {$_.innerText.Length -gt 3} | Select innerText, `
@{Name="URL";Expression={'http://www.youtube.com' + $_.href}} | ? innerText -notlike "*Play all*" | ? innerText -notlike "*Play*"

ForEach ($video in $VideoUrls){
    Write-Host ("Downloading " + $video.innerText)
    .\youtube-dl.exe $video.URL
    }

Useful PowerShell ISE Snippets

Posted on

Hi guys,

One of the best features of the PowerShell ISE is the presence of Snippets, a great engine to fill in code-snippets for common tasks.  I pretty much use this every single day!

Snippets0Negative1
Hit Control+J to display the snippet menu. You can hover over for more info and detailed syntax.

 

Making a new Snippet is kind of difficult though.  I found this wonderful little snippet that allows you to easily create more snippets.  I know.  Snippets in Snippets?  Not sure if it is more Ludacris or Inception though…

Rob the PowerShell DBA had this awesome snippet on his blog.

$snippet1 = @{
    Title = "New-Snippet";
    Description = "Create a New Snippet";
    Text = @"
`$snippet = @{
    Title = `"Put Title Here`";
    Description = `"Description Here`";
    Text = @`"
    Code in Here 
`"@
}
New-IseSnippet @snippet
"@
}
New-IseSnippet @snippet1 –Force

From <http://sqldbawithabeard.com/2014/09/09/powershell-snippets-a-great-learning-tool/>

What does it do? Well, after running this in the ISE, you can hit Control+J and bam, an easy to fill in form to create a nice new Snippet.

Snippets00
You’ll use this on a weekly basis!

Fill in the values and hit F5 to create the snippet.

I love this so much, here are a bunch of snippets I use everywhere.

Custom Objects using Try/Catch

I made a whole blog post about doing this. It’s now my go-to standard when making information gathering tools.

$snippet = @{
    Title = "Try/Catch/Custom Objects";
    Description = "A great way to get good streamlined output while Try/Catching";
    Text = @"
        try {`$a=Get-WmiObject -Class Win32_ComputerSystem -ErrorAction Stop -ComputerName $name} 
   catch{`$a= [pscustomobject]@{Name=`$name;Domain="Access Denied"}}
   [pscustomobject]@{RUM_Name=`$name;ReplyName=`$a.Name;Domain=`$a.Domain} 
"@
}
New-IseSnippet @snippet

Quick PS Custom Object

Sometimes you just need to grab a bunch of info about a thing and make it a single object. Here’s how:

$snippet = @{
    Title = "Custom Object";
    Description = "I always forget how to do this!";
    Text = @"
       #Add more columns to the object by adding extra key/values
      [pscustomobject]@{Name=$name;Domain="Access Denied"}
"@
}

New-IseSnippet @snippet

Progress Bar within ForEach Loop

This looks easy but I would forget how to do it constantly.

$snippet = @{
    Title = "Write Progress Sample";
    Description = "Progress and how to do it";
    Text = @"
    #sample range of numbers
    `$users = (1..13000)

    #setting up base number
    `$i=0
    
    ForEach (`$user in `$users){
        #increment
        `$i++ 

        #Round the numbers up for a nice output and then Write-Progress
        Write-Progress -Activity "Processing `$user" -PercentComplete ((`$i/`$users.Count) * 100) -Status ("`$i out of " + `$users.Count +" completed "+[math]::Round(((`$i/`$users.Count) * 100),2) +" %")
        }
"@
}
New-IseSnippet @snippet

PowerShell V1 Custom Object Format

Sometimes you have to work on ancient systems and forget how to make old-school custom objects. Never again! This example is based on capturing the output of Get-MailboxStatistics within $mbx.

 $snippet = @{
       Title = "PS 2.0 Custom Objects";
       Description = "Old Fashioned Custom Objects";
       Text = @"
       `$ObjectProperties = @{
Name = `$user
RecipientType=`$mbx.RecipientType
LastLoggedOnUserAccount=`$mbxstat.LastLoggedOnUserAccount
LastLogOffTime=`$mbxstat.LastLogOffTime
LastLogonTime=`$mbxstat.LastLogonTime
}
`$obj = New-Object PSObject -Property `$ObjectProperties

"@
   }
   New-IseSnippet @snippet

Old-School Custom Objects using Try/Catch

A repeat of my first Custom Object loop, this time with Pre-V2 objects

$snippet = @{
    Title = "Old School try/catch custom object ";
    Description = "Using try/catch to create custom objects is a great way to capture information succinctly.  However, the [PSCustomObject] Accelerator/casting only work on PS 3 and up.  This example uses old school Items to get around that";
    Text = @"
    `$users | ForEach-Object { 
    `$name = `$_
    try {`$a=Get-mailbox `$name -erroraction Stop} 
   catch{   `$ObjectProperties = @{
        Name = `$name
        HiddenFromAddressListsEnabled="MBX Not Found"
        }
        `$a = New-Object PSObject -Property `$ObjectProperties}


       `$ObjectProperties = @{
            Name = `$name
            HiddenFromAddressListsEnabled=`$a.HiddenFromAddressListsEnabled
            }
        New-Object PSObject -Property `$ObjectProperties
   }
"@
}
New-IseSnippet @snippet

Display a Popup Prompt

This is a shorty, but a useful one!

$snippet = @{
    Title = "Popup Message";
    Description = "Add a simple pop-up message";
    Text = @"
    `$msg = New-Object -ComObject WScript.Shell
    `$msg.Popup("Hi Chris", 5, "DeadMau5", 48)

"@
}
New-IseSnippet @snippet

Migrating and transforming contact lists from OCS to Lync 2013 with PowerShell

Posted on

Update: You can get the functions referenced in this post here.


Recently, I was given a very favorable task, which boiled down to something like this  “We need to migrate buddy lists from OCS 2007 to Lync 2013 and handle thousands of changes to e-mail addresses/sip address and domain names in the process”.

I turned to the only resource about this on the internet, the following 200-word blog post, in Spanish.

http://uclavecchia.wordpress.com/2013/03/06/exportar-contatos-ocslync2010-e-importar-no-lync-2013/

I feel that I should probably stop you here and say Warning!  This is not at all supported!

In my situation, here were the requirements: Read the rest of this entry »