A question of scope.

This topic contains 2 replies, has 2 voices, and was last updated by  Rob Campbell 4 years, 4 months ago.

  • Author
    Posts
  • #4392

    Rob Campbell
    Participant

    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:

    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
      }
     }
    }

     

     

  • #4448

    Don Jones
    Keymaster

    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?

  • #4451

    Rob Campbell
    Participant

    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. 🙂

You must be logged in to reply to this topic.