Flawless Faction Puzzle 1

This topic contains 0 replies, has 1 voice, and was last updated by  Dzmitry Nikalaichyk 3 weeks, 5 days ago.

  • Author
    Posts
  • #92095

    Dzmitry Nikalaichyk
    Participant

    Hi, I've decided to make a series of posts for the Iron Scripter.
    Unfortunately, I've missed the flash, and was a little late on the Puzzle 1, but I still want to share my solution.
    I didn't looked in solution, trust me on that. =)

    So, here is my code:

    function Get-MonitorsInfo
    {
        <#
            .SYNOPSIS
            Gathering information about connected monitors to Computer.
    
            .DESCRIPTION
            Gathering information about connected monitors to Computer.
    
            .PARAMETER ComputerName
            Array of computer names to connect to.
    
            .PARAMETER Credential
            Specifies a user account that has permission to perform this action. If Credential is not specified, the current user account is used.
            Specify the value for Credential by using one of the following formats:
            A user name: "User01"
            A domain name and a user name: "Domain01\User01"
            A user principal name: "User@Domain.com"
            A PSCredential object, such as one returned by the Get-Credential cmdlet.
            When you type a user name, you are prompted for a password.
    
            .PARAMETER CimSession
            Specifies the CIM session to use for this cmdlet. Enter a variable that contains the CIM session or a command that creates or gets the CIM session,
             such as the New-CimSession or Get-CimSession cmdlets. For more information, see about_CimSessions.
    
            .EXAMPLE
            Get-MonitorsInfo
            Getting information about local computer.
            
            .EXAMPLE
            Get-MonitorsInfo -ComputerName localhost,FADC01
            Connects to a remote computers using current credentials object.
            
            .EXAMPLE
            Get-MonitorsInfo -ComputerName localhost,FADC01 -Credential (Get-Credential)
            Connects to a remote computers using custom credentials object.
    
            .EXAMPLE
            Get-MonitorsInfo -CimSession $cimSession
            Connects to a remote computer using existing CimSession
        #>
    
        [CmdletBinding(
            DefaultParameterSetName = 'CimSession'
        )]
        param (
            [Parameter(
                ValueFromPipeline,
                ValueFromPipelineByPropertyName,
                ParameterSetName = 'remote'
            )][string[]] $ComputerName,
    
            [Parameter(
                ParameterSetName = 'remote'
            )][PScredential]$Credential,
    
            [Parameter(
                ParameterSetName = 'CimSession',
                ValueFromPipelineByPropertyName
            )][CimSession]$CimSession
        )
    
        begin
        {
            enum ComputerType
            {
                StandaloneWorkstation = 0
                MemberWorkstation = 1
                StandaloneServer = 2
                MemberServer = 3
                BackupDomainController = 4
                PrimaryDomainController = 5
            }
    
            $sessions = @()
            $computer = @()
        }    
    
        process
        {
            if ($PSBoundParameters.ContainsKey('ComputerName'))
            {
                if ($PSBoundParameters.ContainsKey('Credential'))
                {
                    foreach ($cn in $ComputerName)
                    {
                        Write-Verbose -Message "Connecting to $cn"
                        $sessions += $currentSession = New-CimSession -ComputerName $cn -Credential $Credential -ErrorAction Stop
                        Write-Verbose -Message "Getting information from $cn"
                        $computer += New-Object -TypeName PSObject -Property @{
                            Monitors = Get-CimInstance -ClassName 'wmiMonitorID' -namespace 'root\wmi' -CimSession $currentSession
                            ComputerSystem = Get-CimInstance -ClassName 'Win32_OperatingSystem' -CimSession $currentSession
                            Bios = Get-CimInstance -ClassName 'Win32_Bios' -CimSession $currentSession
                        }
                        Write-Debug -Message $computer[-1]
                    }
                }
                else
                {
                    foreach ($cn in $ComputerName)
                    {
                        Write-Verbose -Message "Connecting to $cn"
                        $sessions += $currentSession = New-CimSession -ComputerName $cn -ErrorAction Stop
                        Write-Verbose -Message "Getting information from $cn"
                        $computer += New-Object -TypeName PSObject -Property @{
                            Monitors = Get-CimInstance -ClassName 'wmiMonitorID' -namespace 'root\wmi' -CimSession $currentSession
                            ComputerSystem = Get-CimInstance -ClassName 'Win32_OperatingSystem' -CimSession $currentSession
                            Bios = Get-CimInstance -ClassName 'Win32_Bios' -CimSession $currentSession
                        }
                        Write-Debug -Message $computer[-1]
                    }
                }
            }
            elseif ($PSBoundParameters.ContainsKey('CimSession'))
            {
                Write-Verbose -Message ('Getting information from {0}' -f $CimSession.ComputerName)
                $computer = New-Object -TypeName PSObject -Property @{
                    Monitors = Get-CimInstance -ClassName 'wmiMonitorID' -namespace 'root\wmi' -CimSession $CimSession
                    ComputerSystem = Get-CimInstance -ClassName 'Win32_OperatingSystem' -CimSession $CimSession
                    Bios = Get-CimInstance -ClassName 'Win32_Bios' -CimSession $CimSession
                }
                Write-Debug -Message $computer
            }
            else
            {
                Write-Verbose -Message 'Getting information from local computer'
                $computer = New-Object -TypeName PSObject -Property @{
                    Monitors = Get-CimInstance -ClassName 'wmiMonitorID' -namespace 'root\wmi'
                    ComputerSystem = Get-CimInstance -ClassName 'Win32_OperatingSystem'
                    Bios = Get-CimInstance -ClassName 'Win32_Bios'
                }
            }
    
            foreach ($c in $computer)
            {
                $c.Monitors | ForEach-Object {
                    New-Object -TypeName PSObject -Property @{
                        ComputerName = $c.ComputerSystem.CSName
                        ComputerType = $c.ComputerSystem.ProductType -as [ComputerType]
                        ComputerSerial = $c.bios.SerialNumber
                        MonitorSerial = $(
                            if ($_.SerialNumberID -ne 0)
                            {
                                -join ($_.SerialNumberID | ForEach-Object {[char]$_})
                            }
                        )
                        MonitorType = $(
                            if ($_.UserFriendlyName)
                            {
                                -join ($_.UserFriendlyName | ForEach-Object {[char]$_})
                            }
                        )
                    }
                }
            }
        }
    
        end
        {
            if ($PSCmdlet.ParameterSetName -eq 'remote')
            {
                $sessions | Remove-CimSession
            }
        }
        
    }
    

You must be logged in to reply to this topic.