Use PowerShell to quickly start a new Google Search

November 16, 2013 FoxDeploy

Hi guys,

In this two-part series, I’ll demonstrate how I created a quick little function to launch a new Google search from the command line using Powershell.  It started with me being annoyed that looking up the maximum RAM for my motherboard was a multistep process, first using **Get-WmiObject Win32_BaseBoard Select Product**  and then Googling it to see the max RAM.  I wanted to start a search right from the Powershell window!

To start off, I wanted to make a function accept infinite number of characters with spaces as an argument.  This became a problem as PowerShell uses spaces to delimit parameters, so I had to get a bit clever.  I didn’t want to have to create thirty or more parameters in my function header to accept all of the possible names.  I got around this by using the $args variable, which alongside the $input variable can be used to handle either Pipeline Input or the function being called itself with parameters.  Using $input leads to another problem–For here there be dragons!–the $input automatic variable is NOT an array, but rather a construct known as an enumerator.  That means two things:

  1. Unless you call its .Reset() Method, you can only access its value a single time.  One time use!
  2. You cannot use it in the Begin phase of your functions, only in the Process.

If you want to really learn about how weird these things are, check out these blogs posts here about them.

With all of that in mind, I instead created a structure in the script to detect if there are arguments present, and if not, set $args equal to the values in $input by means of classing $input as an array (this means throwing an @ sign at the front, and robing it in a nice warm pair of parentheses), and then running the .Split() Method on $args, to give us separate objects (necessary to build a URL).

The code reads the content of $args (which will be similar whether the function receives a parameter or pipeline input) and then for each object in $args, throws it onto a $query variable with a plus sign at the end of it.  In the last step of the code, $query is substringed to remove a dangling plus sign, and then committed to $url, which is then invoked using the Start command, to open in your browser of choice.

Here is the code.

`<# .Synopsis Searches the Googes .DESCRIPTION Lets you quickly start a search from within Powershell .EXAMPLE Search-Google Error code 5 --New google search results will open listing top entries for 'error code 5'` `.EXAMPLE search-google (gwmi win32_baseboard).Product maximum ram`

``If you need to get the maximum ram for your motherboard, you can even use this type of syntax #> function Search-Google { 
  Begin { 
      $query='https://www.google.com/search?q=' 
      } 
  Process { 
    if ($args.Count -eq 0) {
      "Args were empty, commiting `$input to `$args" 
      Set-Variable -Name args -Value (@($input) | % {$_}) 
      "Args now equals $args" $args = $args.Split() 
    }
    ELSE { 
      "Args had value, using them instead" 
    }

    Write-Host $args.Count, "Arguments detected" "Parsing out Arguments: $args" for ($i=0;$i -le $args.Count;$i++){ $args | % {"Arg $i `t $_ `t Length `t" + $_.Length, " characters"} }``

    $args | % {$query = $query + "$_+"}`

  } 
  End { 
    $url = $query.Substring(0,$query.Length-1) 
    "Final Search will be $url `nInvoking..." 
    start "$url" 
  } 
}

   And here is the result.

And a new browser tab was born…

 

In the end, you can use this to get the maximum RAM, just as I wanted to!

search-google (gwmi win32_baseboard select -expand Product) maximum ram

If you want to add this to your default Powershell window (and why wouldn’t you?), simply add it to your PowerShell Profile, which is launched every time you open the program.

Continue Reading...

Automating Google Maps & Traffic to determine when to drive to work using PowerShell

November 08, 2013 FoxDeploy

When I moved a while back, I wondered what the best route to take to work would be, and also when I should leave for work, but I didn’t know the area well enough to be certain.  I could go to Google Maps and put in my home address and work address and see what the current traffic time was, but this wouldn’t help me determine if I needed to leave at 7:45 or if I could eat another bagel and walk out the door at 8:00 and still make it to work on time (bagel carbohydrates notwithstanding).

So I did what I always do and turned to PowerShell to build a tool to do this.  I’d already used some PowerShell driven Internet Explorer automation to save me loading 100 URLs to see what the title was of each (to track down what the patch was doing for a particular KB Update Number), so I knew I could start there and work out a solution.

[IO.Directory]::CreateDirectory($Path) Out-Null
Continue Reading...

Continued: CREATING A GUI NATIVELY FOR YOUR POWERSHELL TOOLS USING .NET METHODS

October 30, 2013 FoxDeploy

When we left off, we had a nice fancy UI that didn't do anything! This session, we'll link our pinging code from before into our GUI, and then update the contents on the screen as part of the process. Once we're done with this, we'll experiment with some of the other controls available, and then customize things a bit more. Continue Reading...

Creating a GUI Natively for your PowerShell tools using System.Windows.Forms

October 23, 2013 FoxDeploy

The first post in the FoxDeploy GUI Creating series, this post is focused on making a GUI using Windows Forms! Continue Reading...

Solving 5447, MP has rejected a policy request because it was not approved.

October 21, 2013 FoxDeploy

You may see the error message like this from time to time:

 Component: SMS_MP_CONTROL_MANAGER

MP has rejected a policy request from GUID:XXXXX-XXXX-XXX-XXX-XXXXXXXXXXXXXX because it was not approved.

This really means that the client…is not approved.  This can happen for a few reasons, but namely one.  If you set SCCM to Manual Approval mode at some point, be it for testing or troubleshooting, any clients that attempted to be approved at that time are marked as Unapproved, pretty much until the end of time.  You have to find these systems and mark them as approved.

Continue Reading...

Moving your VM Storage the Smart way

October 17, 2013 FoxDeploy

Hi all,

Don’t be a dummy, use the fantastic tools built-in to Hyper-V 2012 to make it easy to migrate your VMs onto new storage or over to a new host.  Never mess with copying .vhdx or configuration files again.

I rebuilt my storage in my test lab, making use of Storage Tiering in Storage Spaces to pool two 64 GB SSDs and one 1.5 TB HDD to get the best of both high-speed read and writes and bulk storage without needing to move my files around manually.  The tiering aspect of Storage Spaces will detect which files are most used and then automatically merge them into the faster speed SSD from bulk, how cool is that!

Continue Reading...

Microsoft MVP

Five time Microsoft MVP, and now I work for the mothership


Need Help?

Get help much faster on our new dedicated Subreddit!

depicts a crowd of people in a night club with colored lights and says 'join the foxdeploy subrreddit today'


Blog Series
series_sml_IntroToDsc
series_sml_PowerShellGUI series_sml_IntroToRaspberryPi Programming series_sml_IntroToWindows Remote Management Series The Logo for System Center Configuration Manager is displayed here Depicts a road sign saying 'Learning PowerShell Autocomplete'




Blog Stats