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{} }}