Flawless Faction Puzzle 1

This topic contains 4 replies, has 2 voices, and was last updated by  Jeremy Murrah 1 month ago.

  • Author
    Posts
  • #91598

    Brandon Lundt
    Participant

    Several updates from what I posted for Daybreak. I included Format data as well as Pester testing. My Context "No Mocked Functions" needs some help.

  • #91713

    Jeremy Murrah
    Participant

    Here's my version. My thinking on 'flawless' code, beside the obvious 'having no errors' is that it should work in as many situations as possible. So I tried to add options for handling different input types and different connection methods. Expanded upon Brandon's excellent test code above. Could probably add more stuff but the day job calls.

    • #91728

      Brandon Lundt
      Participant

      So when I run this on my computer I get:

      ComputerSerial ComputerName MonitorInfo ComputerType
      -------------- ------------ ----------- ------------
                     Computer01                               
      

      Question, lines 18 to 26 why start-job for the if statement but not for the else statement?

      Your lines 34 to 46 gives me an idea about just using invoke-command to fan out for all N computernames that are passed in. I really dig that chuck of code. I also really like your additions to the pester tests!

    • #91837

      Jeremy Murrah
      Participant

      for lines 18-26 I'm using start-job as a way to run Get-CimInstance with alternate credentials. It makes some assumptions about where the credentials can logon, but I figure better than nothing, and if it doesn't work there's always the invoke-command section.

      As for your update, invoke-command doesn't work when trying to run against the local box, so you'll need something to account for that.

    • #91731

      Brandon Lundt
      Participant

      Regarding my idea, what do you think of this:

      Function Get-MonitorStatistics {
          
          [outputtype('PScustomObject')]
          [cmdletbinding(SupportsShouldProcess = $False)]
          Param(
              [Parameter(Mandatory = $False,
                          ValueFromPipeline = $True)]
              [String[]]$ComputerName = $env:COMPUTERNAME
              
          )#Param
          Begin{
              
              New-Variable -Name Cim_Parameters
              New-Variable -Name Computer
              New-Variable -Name ComputerSerial
              New-Variable -Name MonitorSerial
              New-Variable -Name MonitorType
              Update-FormatData -PrependPath ($PSScriptRoot + ".\MonitorStatistics.Format.ps1xml")
          }#Begin
          Process{
              $ScriptBlock = {
                  $ComputerType = Get-CimInstance -ClassName "Win32_ComputerSystem"  @Cim_Parameters | Select-Object -ExpandProperty Model
                  $ComputerSerial = Get-CimInstance -ClassName "Win32_Bios" @Cim_Parameters | Select-Object -ExpandProperty SerialNumber
                  
                  Get-CimInstance -ClassName "wmiMonitorID" -Namespace "root\wmi" @Cim_Parameters | ForEach-Object {
                      
                      $psObject = [PSCustomObject]@{
                                  PSTypeName     = 'System.Object.MonitorStatistics'
                                  ComputerName   = $ComputerName
                                  ComputerType   = $ComputerType
                                  ComputerSerial = $ComputerSerial
                                  MonitorSerial  = ($_.SerialNumberID   -ne 0 | ForEach-Object{[char]$_}) -join ""
                                  MonitorType    = ($_.UserFriendlyName   -ne 0 | ForEach-Object{[char]$_}) -join ""
                              }
                      Write-Output -InputObject $psObject
                  }#ForEach-Object Get-CimInstance
              }#ScriptBlock
              Invoke-Command -ScriptBlock $ScriptBlock -ComputerName $ComputerName | Write-Output
          }#Process
          End{
              
              Remove-Variable -Name psObject
              Remove-Variable -Name Computer
              Remove-Variable -Name ComputerSerial
              Remove-Variable -Name MonitorSerial
              Remove-Variable -Name MonitorType
          }
      }#Function
      

You must be logged in to reply to this topic.