{"id":894,"date":"2012-02-09T17:43:45","date_gmt":"2012-02-09T22:43:45","guid":{"rendered":"http:\/\/enterpriseadmins.org\/blog\/?p=894"},"modified":"2012-02-09T17:43:45","modified_gmt":"2012-02-09T22:43:45","slug":"get-beer-powershell-function","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/get-beer-powershell-function\/","title":{"rendered":"Get-Beer Powershell function"},"content":{"rendered":"<p>Earlier in the week I had the privilege of presenting an introduction to Powershell\/PowerCLI webex to a group of VMware customers.  To help explain the Where-Object, Select-Object, and Sort-Object cmdlets, I thought it would be helpful to create a Get-Beer function.  This function was a success, so I thought I would share it here.  The function is very basic &#8212; it uses Import-CSV to read information from a file and returns the results ordered by name.  The results can then be piped to other functions for demo purposes.<\/p>\n<p>You can <a href=\"http:\/\/enterpriseadmins.org\/blog\/wp-content\/uploads\/2012\/02\/beer.csv\">download a sample beer.csv file here<\/a>.<\/p>\n<p>Here is the function:<\/p>\n<pre><code class=\"language-powershell\">\r\n<#\r\n.SYNOPSIS\r\nList Miller Coors beer\r\n.DESCRIPTION\r\nThis function returns a list of beer bottled by MillerCoors.\r\nThe object also includes nutrition and alcohol information.\r\n.EXAMPLE\r\nPS C:\\> Get-Beer\r\n.NOTES\r\nThis function was created for demo purposes only.  \r\nVMware PowerCLI rocks!\r\n#>\r\nFunction Get-Beer {\r\n\t$myResults = @()\r\n\tImport-Csv C:\\tmp\\beer.csv | Sort-Object Name | %{\r\n\t\t[decimal]$thisAlcoholPercent = $_.\"Alcohol%\"\r\n\t\t[decimal]$thisCaloriesPer12oz = $_.CaloriesPer12oz\r\n\t\t$myResults += new-object -type PSObject -Property @{\r\n\t\t\tName = $_.Name\r\n\t\t\tAlcoholPercent = $thisAlcoholPercent\r\n\t\t\tCaloriesPer12oz = $thisCaloriesPer12oz\r\n\t\t\tCategory = $_.Category\r\n\t\t} #End New-Object\r\n\t} #End for each object\r\n\t\r\n\t# Return a custom PS object containing each beer\r\n\treturn ($myResults | Select-Object Name, AlcoholPercent, CaloriesPer12oz, Category)\r\n} #End Function\r\n<\/code><\/pre>\n<p>The function type casts the numeric values as decimal so that they properly sort.  To run this, place a beer.csv file in the C:\\tmp path (or adjust the filename\/path in the function), paste the function into a powershell window and you are ready to go.<\/p>\n<p>Here are a few examples of what you can do with this function:<\/p>\n<pre>\r\nGet-Beer\r\n\r\n(Get-Beer).count\r\n\r\nGet-Beer | Sort-Object AlcoholPercent -Descending | Select-Object Name, AlcoholPercent -First 5\r\n\r\nGet-Beer | Where-Object {$_.CaloriesPer12oz -lt 200 -AND $_.Category -eq \"Craft\"} | Sort-Object AlcoholPercent -Descending | Select-Object -First 5\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Earlier in the week I had the privilege of presenting an introduction to Powershell\/PowerCLI webex to a group of VMware customers. To help explain the Where-Object, Select-Object, and Sort-Object cmdlets, I thought it would be helpful to create a Get-Beer &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/get-beer-powershell-function\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-894","post","type-post","status-publish","format-standard","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=894"}],"version-history":[{"count":4,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/894\/revisions"}],"predecessor-version":[{"id":899,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/894\/revisions\/899"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}