Please Explain this Logic in the Script

This topic contains 2 replies, has 2 voices, and was last updated by  Sikander Mirza 3 years, 2 months ago.

  • Author
  • #21407

    Sikander Mirza

    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 {
        Param (
                       HelpMessage='Enter ComputerName')]
                       HelpMessage="Enter a Drive eg:C:,D:,E:")]
            [ValidateSet('C:', 'D:', 'E:')]
            foreach ($computer in $ComputerName){ 
                Write-Verbose "Quering WMI for $computer"
                try {
                $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer `
                      -ErrorVariable MyError -ErrorAction Stop
                } catch {
                    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   
  • #21408

    Dave Wyatt

    $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

      Sikander Mirza

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

You must be logged in to reply to this topic.