Flawless Faction Puzzle 2

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

  • Author
    Posts
  • #92242

    Dzmitry Nikalaichyk
    Participant

    Hi,
    Here is my solution on Puzzle 2 for Flawless faction. I've decided to use the same supporting code for connection to remotes as in Puzzle 1.

    So, here is the code:

    function Get-SystemInformation
    {
        <#
            .SYNOPSIS
            Gathering information about Computers system.
    
            .DESCRIPTION
            Gathering information about Computers system.
    
            .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-SystemInformation
            Getting information about local computer.
            
            .EXAMPLE
            Get-SystemInformation -ComputerName localhost,FADC01
            Connects to a remote computers using current credentials object.
            
            .EXAMPLE
            Get-SystemInformation -ComputerName localhost,FADC01 -Credential (Get-Credential)
            Connects to a remote computers using custom credentials object.
    
            .EXAMPLE
            Get-SystemInformation -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
        {
            $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 @{
                            OperatingSystem = Get-CimInstance -ClassName 'Win32_OperatingSystem' -CimSession $currentSession
                            Disks = Get-CimInstance -ClassName 'Win32_LogicalDisk' -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 @{
                            OperatingSystem = Get-CimInstance -ClassName 'Win32_OperatingSystem' -CimSession $currentSession
                            Disks = Get-CimInstance -ClassName 'Win32_LogicalDisk' -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 @{
                    OperatingSystem = Get-CimInstance -ClassName 'Win32_OperatingSystem' -CimSession $CimSession
                    Disks = Get-CimInstance -ClassName 'Win32_LogicalDisk' -CimSession $CimSession
                }
                Write-Debug -Message $computer
            }
            else
            {
                Write-Verbose -Message 'Getting information from local computer'
                $computer = New-Object -TypeName PSObject -Property @{
                    OperatingSystem = Get-CimInstance -ClassName 'Win32_OperatingSystem'
                    Disks = Get-CimInstance -ClassName 'Win32_LogicalDisk'
                }
            }
    
            foreach ($c in $computer)
            {
                New-Object -TypeName PSObject -Property @{
                    ComputerName = $c.OperatingSystem.CSName
                    OSName = $c.OperatingSystem.Caption
                    Version = $c.OperatingSystem.Version
                    ServicePack = '{0}.{1}' -f $c.OperatingSystem.ServicePackMajorVersion,$c.OperatingSystem.ServicePackMinorVersion
                    OSManufacturer = $c.OperatingSystem.Manufacturer
                    WindowsDirectory = $c.OperatingSystem.WindowsDirectory
                    Locale = $c.OperatingSystem.Locale
                    AvailablePhysicalMemory = $c.OperatingSystem.FreePhysicalMemory/1MB
                    TotalVirtualMemory = $c.OperatingSystem.TotalVirtualMemorySize/1MB
                    AvailableVirtualMemory = $c.OperatingSystem.FreeVirtualMemory/1MB
                    Disks = $(
                        foreach ($disk in $c.disks)
                        {
                            New-Object -TypeName PSObject -Property @{
                                Drive = $disk.DeviceID
                                DriveType = $disk.Description
                                Size = $disk.Size/1GB
                                FreeSpace = $disk.FreeSpace/1GB
                                Compressed = $disk.Compressed
                                UsedSpace = '{0:P}' -f (1-($disk.FreeSpace/$disk.Size))
                                PSTypeName = 'SystemInfomation.Disk'
                            }
                        }
                    )
                    PSTypeName = 'SystemInfomation'
                }
            }
        }
    
        end
        {
            if ($PSCmdlet.ParameterSetName -eq 'remote')
            {
                $sessions | Remove-CimSession
            }
        }
    }
    

    Here is the formatting xml:

    <?xml version="1.0" encoding="utf-8" ?>
    <Configuration>
        <ViewDefinitions>
            <View>
                <Name>SystemInfomation</Name>
                <ViewSelectedBy>
                    <TypeName>SystemInfomation</TypeName>
                </ViewSelectedBy>
                <TableControl>
                    <TableHeaders>
                        <TableColumnHeader>
                            <Label>Computer Name</Label>
                            <Widthᡇ</Width>
                        </TableColumnHeader>
                        <TableColumnHeader>
                            <Label>Operating System</Label>
                            <Widthᡛ</Width>
                        </TableColumnHeader>
                        <TableColumnHeader>
                            <Label>Version</Label>
                            <Widthᡄ</Width>
                        </TableColumnHeader>
                        <TableColumnHeader>
                            <Label>Disks</Label>
                            <Widthᡂ</Width>
                        </TableColumnHeader>
                    </TableHeaders>
                    <TableRowEntries>
                        <TableRowEntry>
                            <TableColumnItems>
                                <TableColumnItem>
                                    <PropertyName>ComputerName</PropertyName>
                                </TableColumnItem>
                                <TableColumnItem>
                                    <PropertyName>OSName</PropertyName>
                                </TableColumnItem>
                                <TableColumnItem>
                                    <PropertyName>Version</PropertyName>
                                </TableColumnItem>
                                <TableColumnItem>
                                    <ScriptBlock>$_.Disks.Drive</ScriptBlock>
                                </TableColumnItem>
                            </TableColumnItems>
                        </TableRowEntry>
                    </TableRowEntries>
                </TableControl>
            </View>
            <View>
                <Name>DiskInfo</Name>
                <ViewSelectedBy>
                    <TypeName>SystemInfomation.Disk</TypeName>
                </ViewSelectedBy>
                <TableControl>
                    <TableHeaders>
                        <TableColumnHeader>
                            <Label>Drive</Label>
                        </TableColumnHeader>
                        <TableColumnHeader>
                            <Label>Type</Label>
                        </TableColumnHeader>
                        <TableColumnHeader>
                            <Label>Size(GB)</Label>
                        </TableColumnHeader>
                        <TableColumnHeader>
                            <Label>Free(GB)</Label>
                        </TableColumnHeader>
                        <TableColumnHeader>
                            <Label>Used Space(%)</Label>
                        </TableColumnHeader>
                    </TableHeaders>
                    <TableRowEntries>
                        <TableRowEntry>
                            <TableColumnItems>
                                <TableColumnItem>
                                    <PropertyName>Drive</PropertyName>
                                </TableColumnItem>
                                <TableColumnItem>
                                    <PropertyName>DriveType</PropertyName>
                                </TableColumnItem>
                                <TableColumnItem>
                                    <ScriptBlock>"{0:n2}" -f $_.Size</ScriptBlock>
                                </TableColumnItem>
                                <TableColumnItem>
                                    <ScriptBlock>"{0:n2}" -f $_.FreeSpace</ScriptBlock>
                                </TableColumnItem>
                                <TableColumnItem>
                                    <PropertyName>UsedSpace</PropertyName>
                                </TableColumnItem>
                            </TableColumnItems>
                        </TableRowEntry>
                    </TableRowEntries>
                </TableControl>
            </View>
        </ViewDefinitions>
    </Configuration>
    

You must be logged in to reply to this topic.