Please Explain this Logic in the Script

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Sikander Mirza Sikander Mirza 1 year, 11 months ago.

  • Author
    Posts
  • #21407
    Profile photo of Sikander Mirza
    Sikander Mirza
    Participant

    Hi,
    Can someone please explain me the logic of $everythingok in the script.
    When we declare this variable under the 'try {' block as $true,The understanding is if the WMI query failed for the first cmdlet then the computername should be logged and the Script should not proceed to the other queries, but how PowerShell or say script knows about what is $everythingok=$true, $everythingok=$false and in if($everythingok) is PowerShell assuming the variable to be true and proceed with the other queries
    Please explain me how this logic is working here???

    {Function Get-Info {
        [CmdletBinding()]
        Param (
            [Parameter(Mandatory=$true,
                       Valuefrompipeline=$true,
                       ValuefrompipelineBypropertyName=$true,
                       HelpMessage='Enter ComputerName')]
            [ValidateCount(1,2)]
            [Alias('HostName')]
            [String[]]$ComputerName,
            [String]$errorlogpath='e:\infoerrorlog.txt', 
    
            [Parameter(Mandatory=$true,
                       HelpMessage="Enter a Drive eg:C:,D:,E:")]
            [ValidateSet('C:', 'D:', 'E:')]
            [String]$Drive
    
            
        )
        Begin{}
        Process{
            foreach ($computer in $ComputerName){ 
                Write-Verbose "Quering WMI for $computer"
                try {
                    $everythingok=$true
                $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer `
                      -ErrorVariable MyError -ErrorAction Stop
                } catch {
                    $everythingok=$false
                    Write-Verbose "$computer Unreachable, Logging Failed computer name to $errorlogpath"
                    Write-Verbose "Error was $MyError"
                    $computer | Out-File $errorlogpath -Append
                }
                if ($everythingok) {
                    $cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer
                    $bios = Get-WmiObject -Class Win32_Bios -ComputerName $computer
                    $processor = Get-WmiObject -Class Win32_Processor -ComputerName $computer
                    $network = Get-WmiObject -Class Win32_NetworkAdapterConfiguration `
                               -Filter "IPenabled=$true" -ComputerName $computer
                    $disk = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceId='$Drive'" -ComputerName $computer
    
                        [PSCustomObject] @{
                            'ComputerName'                = $computer
                            'Domain'                      = $cs.domain
                            'OS Name'                     = $os.caption
                            'OS Architecture'             = $os.OSArchitecture 
                            'MB Model'                    = $cs.Model
                            'MB Make'                     = $cs.Manufacturer
                            'Memory[GB]'                  = $cs.TotalPhysicalMemory /1GB -as [Int]
                            'Processor'                   = $processor.MaxClockSpeed
                            'Processor Cores'             = $processor.NumberOfLogicalProcessors
                            'Processor Architecture[Bit]' = $processor.AddressWidth
                            'LastBootupTime'              = $os.ConvertToDateTime($os.LastBootUpTime)
                            'IPAddress'                   = $network.ipaddress
                            'Drive'                       = $disk.DeviceId 
                            'DiskSize'                    = $disk.size
                            'FreeSpace'                   = $disk.freespace
                            'FileSystem'                  = $disk.filesystem   
                        }
                    }
        
            }
        }
        End{}
    }}
  • #21408
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    $everythingok will only be set to $false if an exception occurs in the first call to Get-WmiObject. If there was no error, then it'll still be set to $true and the rest of the loop body will execute.

    • #21409
      Profile photo of Sikander Mirza
      Sikander Mirza
      Participant

      Thanks Dave, for the prompt reply.
      I understood this now.
      Thanks.

You must be logged in to reply to this topic.