{"id":1653,"date":"2011-09-22T08:53:40","date_gmt":"2011-09-22T12:53:40","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/?p=1653"},"modified":"2011-09-22T08:53:40","modified_gmt":"2011-09-22T12:53:40","slug":"the-powershell-day-care-building-scriptblocks","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/","title":{"rendered":"The PowerShell Day Care: Building ScriptBlocks"},"content":{"rendered":"<p>Good morning kids and welcome to the PowerShell Day Care center. We offer a creative and nurturing environment for PowerShell professionals of all ages. Later there might even be juice and cookies. But first let's get out our blocks, our scriptblocks, and start building. I've written a number of posts on script blocks and today have a script to offer, that even if you don't need it as is, it might offer a few insights into PowerShell. <!--more--><\/p>\n<p>A scriptblock is a small (usually) chunk of PowerShell code enclosed in curly braces. You see them all the time in cmdlets like Where-Object and Start-Job. Think of scriptblocks as modular and re-usable commands that write objects to the pipeline. Often I think you can use a scriptblock for simple tasks instead of creating a function. For example, suppose I want an easy way to get the current day of the year.<\/p>\n<p>[cc lang=\"DOS\"]<br \/>\nPS C:\\> (Get-Date).DayOfyear<br \/>\n265<br \/>\n[\/cc]<\/p>\n<p>Short and simple. But I'm lazy. I don't want to have to type that all the time. And while I could save it as a variable, if my PowerShell session is open for a few days, the variable will become stale. This is a simple example anyway. I could create a function but perhaps wrapping this in a scriptblock would be a better approach.<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n$doy={(Get-Date).DayOfyear}<br \/>\n[\/cc]<\/p>\n<p>The variable, $doy, is the command.<\/p>\n<p>[cc lang=\"DOS\"]<br \/>\nPS C:\\> $doy<br \/>\n(Get-Date).DayOfyear<br \/>\nPS C:\\><br \/>\n[\/cc]<\/p>\n<p>To run the scriptblock I can use the invoke operator &.<\/p>\n<p>[cc lang=\"DOS\"]<br \/>\nPS C:\\> &$doy<br \/>\n265<br \/>\n[\/cc]<\/p>\n<p>Or perhaps we want to quickly get running services. We might create a scriptblock like this:<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n$run={get-service | where {$_.status -eq 'running'}}<br \/>\n[\/cc]<\/p>\n<p>If I invoke $run, the command will execute, just as if I had typed it. Where things get tricky is when you want to build a scriptblock from other variables. Remember, scriptblocks run in their own scope. I run into this more often in scripting where I'm building a scriptblock from variables that will execute later. Here's an example of the problem.<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n$log=Read-Host \"Enter a log name, ie system\"<br \/>\n[int]$count=Read-Host \"How many entries do you want?\"<br \/>\n$sb={get-eventlog -log $log -newest $count}<br \/>\nstart-job $sb<br \/>\n[\/cc]<\/p>\n<p>The job gets created but fails because it can't find values for $log and $count. The solution I came up with is to create a string first, taking advantage of variable expansion. Then explicitly define a scriptblock.<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n$log=Read-Host \"Enter a log name, ie system\"<br \/>\n[int]$count=Read-Host \"How many entries do you want?\"<\/p>\n<p>$cmd=\"get-eventlog -log $log -newest $count\"<\/p>\n<p>$sb=$executioncontext.InvokeCommand.NewScriptBlock($cmd)<br \/>\nstart-job $sb<br \/>\n[\/cc]<\/p>\n<p>This is slightly more complicated, but $cmd is defined with the expanded variables and then I create the scriptblock. For you overachievers, you can combine steps and do this:<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n$sb=$executioncontext.InvokeCommand.NewScriptBlock(\"get-eventlog -log $log -newest $count\")<br \/>\n[\/cc]<\/p>\n<p>Now, let's take this to the next level. I have a number of scriptblocks that I want to define in my PowerShell profile. I could simply define them in the profile or a dot sourced script but that's too easy. So I wrote a script that parses a text file of scriptblock commands and creates them. First, here's the text file that gets parsed.<\/p>\n<p>[cc lang=\"DOS\"]<br \/>\n#script block commands<br \/>\n#name delimiter command<br \/>\n#default delimiter is the ~<br \/>\n#each command must be on one line<\/p>\n<p>dc01 ~ mstsc -v jdhit-dc01 \/console<br \/>\nrun ~ gsv | where {$_.status -eq \"running\"}<br \/>\nbigp ~ ps | where {$_.ws -gt 100mb}<br \/>\ndisk ~ Param ([string]$computername=$env:computername) get-wmiobject win32_logicaldisk -filter \"drivetype=3\" -computername $computername | Select \"DeviceID\",\"VolumeName\",@{Name=\"SizeGB\";Expression={\"{0:N4}\" -f ($_.size\/1GB)}},@{Name=\"Freespace\";Expression={\"{0:N4}\" -f ($_.FreeSpace\/1GB)}},@{Name=\"Utilization\";Expression={ \"{0:P2}\" -f (1-($_.freespace -as [double])\/($_.size -as [double]))}}<br \/>\ndoy ~ (get-date).DayOfYear<br \/>\nup ~ Param([string]$computername=$env:computername) Get-WmiObject win32_operatingsystem -computername $computername |Select  @{name=\"Computer\";Expression={$_.CSName}},@{Name=\"LastBoot\";Expression={$_.ConvertToDateTime($_.LastBootUpTime) }},@{Name=\"Uptime\";Expression={(Get-Date)-($_.ConvertToDateTime($_.LastBootUpTime))}}<br \/>\ndirt ~ Param([string]$Path=$env:temp) Get-ChildItem $path -Recurse -force -ea \"silentlycontinue\" | measure-object -Property Length -sum | Select @{Name=\"Path\";Expression={$path}},Count,@{Name=\"SizeMB\";Expression={\"{0:N4}\" -f ($_.sum\/1mb)}}<br \/>\n[\/cc]<\/p>\n<p>The structure of the file is scriptblock name, a delimiter (I\"m using ~) and the scriptblock command. The command must be one line. Notice scriptblocks can also take parameters. This file is parsed by my LoadCmds.ps1 script.<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n[cmdletbinding()]<\/p>\n<p>param (<br \/>\n[Parameter(Position=0)]<br \/>\n[ValidateNotNullorEmpty()]<br \/>\n[string]$Path=\"cmds.txt\",<br \/>\n[string]$Delimiter=\"~\",<br \/>\n[switch]$Passthru<br \/>\n)<\/p>\n<p>Write-Verbose \"Starting $($myinvocation.mycommand)\"<\/p>\n<p>if (Test-Path $path) {<br \/>\n  Write-Verbose \"Processing $path\"<\/p>\n<p> #filter out blanks and lines that start with a comment<br \/>\n Get-Content $path | Where { ($_ -match $delimiter) -AND (!($_.Trim().StartsWith(\"#\"))) } |<br \/>\n foreach -begin { $script:newvariables=@() } -process {<br \/>\n     #split the line at the ~<br \/>\n     $data=$_.Split(\"~\")<br \/>\n     $sb=$data[0].Trim()<br \/>\n     $cmd=$data[1].Trim()<br \/>\n     Write-Verbose \"Creating $sb\"<br \/>\n     #add the scriptblock name to the array<br \/>\n     $script:newvariables+=$sb<br \/>\n     #create a script block object and assign it to the value variable<br \/>\n     $value=$executioncontext.InvokeCommand.NewScriptBlock($cmd)<br \/>\n     #define the new variable<br \/>\n     Set-Variable -name $sb -Value $value -Scope Global<br \/>\n     }  -end {<br \/>\n  if ($Passthru) {<br \/>\n   #write the new variables to the pipeline if -Passthru<br \/>\n   get-variable -Name $script:newvariables<br \/>\n  }<br \/>\n } #close end<br \/>\n} #close if<\/p>\n<p>else {<br \/>\n    Write-Warning \"Failed to find $path\"<br \/>\n }<\/p>\n<p>Write-Verbose \"Ending $($myinvocation.mycommand)\"<br \/>\n#end<br \/>\n[\/cc]<\/p>\n<p>The script takes parameters for the filepath and delimiter. It also uses -Passthru if you want to see the variables that get created. Assuming the file is found blanks and commented lines are filtered out.<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n Get-Content $path | Where { ($_ -match $delimiter) -AND (!($_.Trim().StartsWith(\"#\"))) } |<br \/>\n[\/cc]<\/p>\n<p>Each line is piped to ForEach-Object where I take advantage of the Begin, Process and End parameters. In the Begin scriptblock I define a script variable for an array to hold each command I'm going to create. In the Process scriptblock each line is split into an array on the delimiter.<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n foreach -begin { $script:newvariables=@() } -process {<br \/>\n     #split the line at the ~<br \/>\n     $data=$_.Split(\"~\")<br \/>\n     $sb=$data[0].Trim()<br \/>\n     $cmd=$data[1].Trim()<br \/>\n[\/cc]<\/p>\n<p>The variable name is added to the array and the scriptblock defined.<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n$script:newvariables+=$sb<br \/>\n#create a script block object and assign it to the value variable<br \/>\n$value=$executioncontext.InvokeCommand.NewScriptBlock($cmd)<br \/>\n[\/cc]<\/p>\n<p>All that is left is to define the scriptblock variable and set it to the global scope.<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n#define the new variable<br \/>\nSet-Variable -name $sb -Value $value -Scope Global<br \/>\n[\/cc]<\/p>\n<p>After each line in the text file has been processed the End script block runs and if -Passthru was used, the array with new scriptblock names is written to the pipeline.<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n-end {<br \/>\n  if ($Passthru) {<br \/>\n   #write the new variables to the pipeline if -Passthru<br \/>\n   get-variable -Name $script:newvariables<br \/>\n  }<br \/>\n } #close end<br \/>\n[\/cc]<\/p>\n<p>Whenever I want to add something, I update the text file. One advantage to the text file is that it is not a PowerShell executable so my execution policy doesn't apply.<\/p>\n<p>If you want to try this out you can download a copy of <a href='http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/09\/LoadCmds.txt' Target='_blank'>LoadCmds<\/a>. Otherwise, see what you can build with scriptblocks and be sure to share.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Good morning kids and welcome to the PowerShell Day Care center. We offer a creative and nurturing environment for PowerShell professionals of all ages. Later there might even be juice and cookies. But first let&#8217;s get out our blocks, our scriptblocks, and start building. I&#8217;ve written a number of posts on script blocks and today&#8230;<\/p>\n","protected":false},"author":1,"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,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8],"tags":[321,534,82,540],"class_list":["post-1653","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-foreach-object","tag-powershell","tag-scriptblock","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>The PowerShell Day Care: Building ScriptBlocks &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The PowerShell Day Care: Building ScriptBlocks &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"Good morning kids and welcome to the PowerShell Day Care center. We offer a creative and nurturing environment for PowerShell professionals of all ages. Later there might even be juice and cookies. But first let&#039;s get out our blocks, our scriptblocks, and start building. I&#039;ve written a number of posts on script blocks and today...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2011-09-22T12:53:40+00:00\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1653\\\/the-powershell-day-care-building-scriptblocks\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1653\\\/the-powershell-day-care-building-scriptblocks\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"The PowerShell Day Care: Building ScriptBlocks\",\"datePublished\":\"2011-09-22T12:53:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1653\\\/the-powershell-day-care-building-scriptblocks\\\/\"},\"wordCount\":1187,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"keywords\":[\"ForEach-object\",\"PowerShell\",\"ScriptBlock\",\"Scripting\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1653\\\/the-powershell-day-care-building-scriptblocks\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1653\\\/the-powershell-day-care-building-scriptblocks\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1653\\\/the-powershell-day-care-building-scriptblocks\\\/\",\"name\":\"The PowerShell Day Care: Building ScriptBlocks &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"datePublished\":\"2011-09-22T12:53:40+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1653\\\/the-powershell-day-care-building-scriptblocks\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1653\\\/the-powershell-day-care-building-scriptblocks\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1653\\\/the-powershell-day-care-building-scriptblocks\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The PowerShell Day Care: Building ScriptBlocks\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The PowerShell Day Care: Building ScriptBlocks &#8226; The Lonely Administrator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/","og_locale":"en_US","og_type":"article","og_title":"The PowerShell Day Care: Building ScriptBlocks &#8226; The Lonely Administrator","og_description":"Good morning kids and welcome to the PowerShell Day Care center. We offer a creative and nurturing environment for PowerShell professionals of all ages. Later there might even be juice and cookies. But first let's get out our blocks, our scriptblocks, and start building. I've written a number of posts on script blocks and today...","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/","og_site_name":"The Lonely Administrator","article_published_time":"2011-09-22T12:53:40+00:00","author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"The PowerShell Day Care: Building ScriptBlocks","datePublished":"2011-09-22T12:53:40+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/"},"wordCount":1187,"commentCount":5,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"keywords":["ForEach-object","PowerShell","ScriptBlock","Scripting"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/","name":"The PowerShell Day Care: Building ScriptBlocks &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"datePublished":"2011-09-22T12:53:40+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"The PowerShell Day Care: Building ScriptBlocks"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":826,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/826\/friday-the-13-script-blocks\/","url_meta":{"origin":1653,"position":0},"title":"Friday the 13 Script Blocks","author":"Jeffery Hicks","date":"August 13, 2010","format":false,"excerpt":"In celebration of Friday the 13th and to help ward off any triskaidekaphobia I thought I'd offer up 13 PowerShell scriptblocks. These are scriptblocks that might solve a legitimate business need like finding how long a server has been running to the more mercurial such as how many hours before\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1814,"url":"https:\/\/jdhitsolutions.com\/blog\/google-plus\/1814\/re-use-powershell-scriptblocks-i-commented\/","url_meta":{"origin":1653,"position":1},"title":"Re-Use PowerShell Scriptblocks","author":"Jeffery Hicks","date":"October 24, 2011","format":false,"excerpt":"\/\/Re-Use PowerShell Scriptblocks\/\/I commented on a blog post today that showed how to use a hash table with Select-Object to format file sizes say as KB.dir $env:temp -rec | select fullname,@{Name=\"KB\";Expression={$_.length\/1kb}}Since you most likely will want to use something similar for other directories, don't feel you have to re-invent the\u2026","rel":"","context":"In &quot;Google Plus&quot;","block_context":{"text":"Google Plus","link":"https:\/\/jdhitsolutions.com\/blog\/category\/google-plus\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1517,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1517\/scriptblocks-on-the-fly\/","url_meta":{"origin":1653,"position":2},"title":"ScriptBlocks On the Fly","author":"Jeffery Hicks","date":"June 20, 2011","format":false,"excerpt":"I'm always preaching about writing PowerShell scripts and functions with reuse and modularization in mind. You should never have to write the same block of code twice. But what about in the shell during your daily grind? Perhaps today you're dealing with some issue and periodically need to run a\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":693,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/693\/out-clip\/","url_meta":{"origin":1653,"position":3},"title":"Out-Clip","author":"Jeffery Hicks","date":"July 6, 2010","format":false,"excerpt":"I\u2019ve started working on the 2nd edition of Managing Active Directory with Windows PowerShell: TFM. As with almost all of my writing projects it will be full of PowerShell code examples. In the past I\u2019ve always relied on a manual copy and paste to add content to the manuscript. The\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1962,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1962\/friday-fun-whats-my-variable\/","url_meta":{"origin":1653,"position":4},"title":"Friday Fun What&#8217;s My Variable","author":"Jeffery Hicks","date":"January 6, 2012","format":false,"excerpt":"I use scriptblocks quite a bit in my PowerShell work, often saved as variables. These are handy for commands you want to run again, but don't necessarily need to turn into permanent functions. $freec={(get-wmiobject win32_logicaldisk -filter \"deviceid='c:'\" -property Freespace).FreeSpace\/1mb} Now in PowerShell I can invoke the scriptblock. PS S:\\> &$freec\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/get-variabletype-1-300x197.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2198,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2198\/friday-fun-13-more-scriptblocks\/","url_meta":{"origin":1653,"position":5},"title":"Friday Fun: 13 More Scriptblocks","author":"Jeffery Hicks","date":"April 13, 2012","format":false,"excerpt":"In celebration of Friday the 13th I thought I would offer up a menu of 13 more script blocks. If you missed the first course, you can find the original 13 scrptblocks here. I'm not going to spend a lot of time going over these. Many of them are simple\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/13ball-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=1653"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1653\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}