Battle Faction Puzzle 2

This topic contains 6 replies, has 4 voices, and was last updated by  Darwin Reiswig 3 weeks, 4 days ago.

  • Author
    Posts
  • #91874

    Brandon Lundt
    Participant

    Not sure if it is against the rules to post for all three factions during the prequel, but I'm going to until someone tells me to stop.

    Get-WmiObject -Query "SELECT * FROM Win32_OperatingSystem" | %{
     [pscustomobject]@{
     "OS Name" = $_.Name
     "Version" =  $_.Version
     "Service Pack" =  ($_.ServicePackMajorVersion.ToString() + "." + $_.ServicePackMinorVersion.ToString())
     "OS Manufacturer" =  $_.Manufacturer
     "Windows Directory" =  $_.WindowsDirectory
     "Locale" = $_.Locale
     "Available Physical Memory" =  $_.FreePhysicalMemory
     "Total Virtual Memory" = $_.TotalVirtualMemorySize
     "Available Virtual Memory" =  $_.FreeVirtualMemory
     }
    }
    Get-WmiObject -Query "SELECT * FROM Win32_LogicalDisk" | %{
     [pscustomobject]@{
      PSTypeName = 'Nailed.It'
      "Drive" =  $_.DeviceID
      "DriveType" =  $_.Description
      "Size" = $_.Size
      "FreeSpace" = $_.FreeSpace
      "Compressed" = $_.Compressed
      "Used" = "{0:P2}" -f ($_.FreeSpace / $_.Size)
    }
    }
    
  • #91877

    Brandon Lundt
    Participant

    I may have been a little too quick to post and should have finished reading the pdf first. That said, I believe this is a valid Battle post.

  • #91879

    Axel Bøg Andersen
    Participant

    Here is my take on a Battle Faction solution:

    Thanks to Thomas Rayner for inspiration on how to load this with a quick copy/paste

    Works in Powershell 5.1 and 6.0 both local and remote

  • #91882

    Axel Bøg Andersen
    Participant

    I think "{0:P2}" -f ($_.FreeSpace / $_.Size) is giving you the free percentage and not the used percentage, as requested in the puzzle.

  • #91912

    Yubu
    Participant
    Update-FormatData -PrependPath $myDir\lesson2.format.ps1xml 
    
    $OSObj=gcim Win32_OperatingSystem
    $DisksObj=gcim Win32_LogicalDisk | select *,@{n='DiskSpaceUsed';e={"{0:P}" -f (($_.size-$_.Freespace)/$_.size)}}
    # https://stackoverflow.com/questions/3740128/pscustomobject-to-hashtable (Shay Levy)
    $DisksHash=$disksObj.psobject.properties | foreach -begin {$h=@{}} -process {$h."$($_.Name)" = $_.Value} -end {$h}
    $OShash=$OSobj.psobject.properties | foreach -begin {$h=@{}} -process {$h."$($_.Name)" = $_.Value} -end {$h}
    $allHash=$OShash+$disksHash
    $AllObj=[pscustomobject]$allHash
    $AllObj.psobject.TypeNames.Insert(0, "TTType")
    $AllObj
    
    $script=gc c:\lesson2.ps1; iex ($aa | select -first 1); ((gc c:\servers.txt) -match "needed" ) | %{icm $_ -usessl {$using:script | select -skip 1 | %{iex "$_"}}}
    
  • #91913

    Yubu
    Participant
    https://gist.github.com/yubu/6eba212bf1fb1be96cd6be07c1f063bb#file-lesson2-format-ps1xml
    
    Update-FormatData -PrependPath $myDir\lesson2.format.ps1xml 
    
    $OSObj=gcim Win32_OperatingSystem
    $DisksObj=gcim Win32_LogicalDisk | select *,@{n='DiskSpaceUsed';e={"{0:P}" -f (($_.size-$_.Freespace)/$_.size)}}
    # https://stackoverflow.com/questions/3740128/pscustomobject-to-hashtable (Shay Levy)
    $DisksHash=$disksObj.psobject.properties | foreach -begin {$h=@{}} -process {$h."$($_.Name)" = $_.Value} -end {$h}
    $OShash=$OSobj.psobject.properties | foreach -begin {$h=@{}} -process {$h."$($_.Name)" = $_.Value} -end {$h}
    $allHash=$OShash+$disksHash
    $AllObj=[pscustomobject]$allHash
    $AllObj.psobject.TypeNames.Insert(0, "TTType")
    $AllObj
    
    $script=gc c:\lesson2.ps1; iex ($aa | select -first 1); ((gc c:\servers.txt) -match "needed" ) |
    %{icm $_ -usessl {$using:script | select -skip 1 | %{iex "$_"}}}
    

    Powershell 6.0 OK.

  • #92140

    Darwin Reiswig
    Participant

    I added a selection for disk type, which seemed like an obvious feature to me.

    function Get-ISComputerDiskInfo
    {
        [CmdletBinding()]
        Param(
            [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
                [string[]]$ComputerName=$env:COMPUTERNAME,
            [Parameter()]
                [validateset(   "Local Disk",
                                "Network Drive",
                                "Compact Disc",
                                "Removable Disk",
                                "RAM Disk",
                                "No Root Directory"
                    )]
                [string[]]$DriveType
        )
    
        Begin {
            Write-Verbose "Begin Get-ISComputerDiskInfo"
            Update-FormatData -PrependPath ($PSScriptRoot + ".\2.format.ps1xml")
    
            Write-Verbose "Initialize drive type selection"
            $DriveList = @()
            If ($DriveType) {
                Foreach ($Drive in $DriveType) {
                    switch ($Drive) {
                        "Unknown" {$DriveList += '0'}
                        "No Root Directory" {$DriveList += '1'}
                        "Removable Disk" {$DriveList += '2'}
                        "Local Disk" {$DriveList += '3'}
                        "Network Drive" {$DriveList += '4'}
                        "Compact Disc" {$DriveList += '5'}
                        "RAM Disk" {$DriveList += '6'}
                    }    
                }
            }Else {
                $DriveList = '1','2','3','4','5','6'
            }
                
        }
    
    
        Process {
            Write-Verbose "Process Get-ISMonitorInfo"
            Foreach ($ComputerItem in $ComputerName) {
    
                Write-Verbose "Opening CIM cession for $ComputerItem"
                $CimSession = New-CimSession -ComputerName $ComputerItem
                Write-Verbose "Retrieving OS information"
                $objOperatingSystem = Get-CimInstance -ClassName Win32_OperatingSystem -CimSession $CimSession 
                Write-Verbose "Retrieving Disk information"
                $objDisks = Get-CimInstance -ClassName Win32_LogicalDisk -CimSession $CimSession 
    
                $DiskInfo = @()
    
                Write-Verbose "Building Disk objects"
                foreach ($objDisk in $objDisks) {
                    If ($objDisk.Size -gt 0) {
                        $PctUsed = "{0:P2}" -f (($objDisk.Size - $objDisk.FreeSpace) / $objDisk.Size)
                    } else {
                        $PctUsed = ''
                    }
                    If ($objDisk.DriveType -in $DriveList) {
                        $DiskInfo += @([PSCustomObject]@{
                            ComputerName = $ComputerItem
                            Drive = $objDisk.DeviceID    
                            DriveType = $objDisk.Description    
                            Size = $objDisk.Size
                            FreeSpace = $objDisk.FreeSpace
                            Compressed = $objDisk.Compressed  
                            PercentUsed = $PctUsed
                        })
                    }
                }
    
                Write-Verbose "Building final output objects"
                $Result = [PSCustomObject]@{
                    PSTypeName = "ISComputerDiskInfo"
                    ComputerName = $ComputerItem
                    OS_Name = $objOperatingSystem.Name              
                    Version = $objOperatingSystem.Version              
                    Service_Pack = $($objOperatingSystem.ServicePackMajorVersion).$($objOperatingSystem.ServicePackMinorVersion)
                    OS_Manufacturer = $objOperatingSystem.Manufacturer              
                    Windows_Directory = $objOperatingSystem.WindowsDirectory              
                    Locale = $objOperatingSystem.Locale              
                    Available_Physical_Memory = $objOperatingSystem.FreePhysicalMemory              
                    Total_Virtual_Memory = $objOperatingSystem.TotalVirtualMemorySize              
                    Available_Virtual_Memory = $objOperatingSystem.FreeVirtualMemory              
                    Disk = $DiskInfo
                }
            Write-Verbose "Output for $ComputerItem"
            Write-Output $Result
            }
        }  
    
        End {
            Write-Verbose "End Get-ISComputerDiskInfo"
        }
    }
    

    OK, I figured out that I can't figure out how to post XML in here.

You must be logged in to reply to this topic.