A question of scope.

Not really a question, just a piece of code I found interesting, so I’m putting it here in Discussions if anybody else finds it interesting enough to discuss.

Jeffrey Snover blogged about Private scope recently:
http://www.jsnover.com/blog/2013/03/17/variable-scopes-and-private/
and one of the Scripting Games produced some discussion about selecting the protocol for CIM Sessions. They met in my ISE this morning and produced this:

function Get-_CIMSession
{
 [CmdletBinding()]
 Param
 ([Parameter(Mandatory=$true,ValueFromPipeline=$true)]
 [string[]]$Computername)

 Begin
 {
  $_UseDCOM = New-CimSessionOption -Protocol DCOM
  $Private:PSDefaultParameterValues = $PSDefaultParameterValues.Clone()
  $PSDefaultParameterValues['New-CIMSession:SessionOption'] = $_UseDCOM
  $PSDefaultParameterValues['Test-WSMan:ErrorAction'] = 'Stop'
 }
 Process
 {
  foreach ($Computer in $ComputerName)
   { Try {Test-WSMan $Computer | out-null
   $_CIM = &{New-CimSession -ComputerName $Computer}}
   Catch { $_CIM = New-CimSession -Computername $Computer }

   $_CIM
  }
 }
}

 

 

Curious: What browser are you using? The editor borked your code a bit, but I edited it in FF and it took it all just fine. Did you use the “Visual” editor tab, and not the “Text” tab when launching the code-inserter-thing?

I did that in Chrome. Pretty sure I was on the Visual tab when I did it, but I can’t swear to it now. I’ll test that the next time if I post any more code.
Thanks for fixing it. :slight_smile: