Flawless Faction Puzzle 1

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

  • Author
  • #91598

    Brandon Lundt

    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

    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

      So when I run this on my computer I get:

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

      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

      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

      Regarding my idea, what do you think of this:

      Function Get-MonitorStatistics {
          [cmdletbinding(SupportsShouldProcess = $False)]
              [Parameter(Mandatory = $False,
                          ValueFromPipeline = $True)]
              [String[]]$ComputerName = $env:COMPUTERNAME
              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")
              $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
              Invoke-Command -ScriptBlock $ScriptBlock -ComputerName $ComputerName | Write-Output
              Remove-Variable -Name psObject
              Remove-Variable -Name Computer
              Remove-Variable -Name ComputerSerial
              Remove-Variable -Name MonitorSerial
              Remove-Variable -Name MonitorType

You must be logged in to reply to this topic.