Title + “, as per usual.”

$comps = "NonExistentPC-01"

$Output = Foreach($C in $comps){

	$Flag = 0
	
	Trap {

		$Flag = 1
		Continue
		
	}
	
	$System = Get-WmiObject Win32_ComputerSystem -ComputerName $C | Select-Object -Property Name,Model
	$BIOS = Get-WmiObject Win32_BIOS -ComputerName $C | Select-Object -Property Manufacturer,SerialNumber
	
	If ($Flag -eq 0) {
	
		[PSCustomObject]@{
	  
			Name = $System.Name
			Make = $BIOS.Manufacturer
			Model = $System.Model
			SerialNum = $BIOS.SerialNumber
		
		}
		
		$Out = $C + ' Processed - machine found'
		Write-Host $Out

	} Else {
	
		[PSCustomObject]@{
	  
			Name = "$C"
			Make = " "
			Model = "Machine not found"
			SerialNum = " "
		
		}
		
		$Out = $C + ' Processed - no connection'
		Write-Host $Out
	
	}
	
}

$Output | Export-Csv -Path "c:\Temp\CorpWorkstationInfo.csv" -NoTypeInformation

# CLS
Write-Host
Write-Host
Write-Host
Write-Host "All done! See file \C:\Temp\SerialNumbers.txt for results"
Write-Host
Write-Host
Write-Host

I’m trying to create a CSV with basic information drawn remotely from my network workstations. The magic happens in these two lines:

$System = Get-WmiObject Win32_ComputerSystem -ComputerName $C | Select-Object -Property Name,Model
$BIOS = Get-WmiObject Win32_BIOS -ComputerName $C | Select-Object -Property Manufacturer,SerialNumber

However, some of my workstations are online, sometimes not. When a workstation is online, I want to create an object that has 4 attributes, drawn from the information gathered and stored in the $Output array. When it’s not, I want a different object created that has the name of the PC and a message saying that it’s not there.

Later, I want to feed it every computer in my AD, but at the moment I’m still just testing, so I’m only giving it a PC name I know doesn’t exist. Now, I have tried to encapsulated my differing states with all the permutations of If-Else, try-catch, trap, etc. statements that I could think of, but nothing seems to work quite the way I want it to. When it does work, I get the correct info for the awake PCs in the CSV & console output, and blank lines, without names or anything else, for those that aren’t answering hails at that moment.

The way it’s written now, I get the headers and the blank line in the CSV, but the output to the console still says that the machine was found. Obviously, I’m misunderstanding something. Anyone want to point towards that misunderstanding?

3 Spice ups

without reviewing the code,

I guess you have a loop in it and a variable does not empty out as you run it.

let’s say you have 2 computers, 1 is online 2 is offline, you set the variable in computer1 and it’s all good, however computer 2 fails, but it does not reset the variable to it has the same value.

foreach($computer in $computers){
$var1 = $null
$var2 = $null

# other stuff
}

that will ensure the variable is empty at each run and no previous assigned info stays in it.

@alexw ​, good to see you again.

As per usual, you are correct. That does happen. Unfortunately that is not the issue, here.

Since I am only running one instance (one computer result) and closing the PS window after each run, each variable should be cleared between runs. However, I nulled the $Out variable at the beginning of the Foreach in order to test it, just in case. It made no difference in the console output or the output in the CSV. The object, as far as I can tell, should be recreated and overwritten on each pass, regardless.

It’s as if the error is never being caught by the Trap statement even though it’s thrown, as it’s written now, so the flag never gets changed and the If statement acts as if everything is hunky-dory, even when it’s not, and what goes to the CSV is blank, regardless.

then I don’t understand the issue, maybe like so?

$comps =  "NonExistentPC-01"

$Output = 
Foreach ($C in $comps) {
    $system = $null
    $bios = $null

    if (test-connection $c -Quiet -Count 1) {
        $System = Get-WmiObject Win32_ComputerSystem -ComputerName $C
        $BIOS = Get-WmiObject Win32_BIOS -ComputerName $C

        [PSCustomObject]@{
            Name      = if($system){$System.Name}else{' '}
            Make      = if($bios){$BIOS.Manufacturer}else{' '}
            Model     = if($system){$System.Model}else{' '}
            SerialNum = if($bios){$BIOS.SerialNumber}else{' '}
        }
    }
    Else {
        [PSCustomObject]@{
            Name      = "$C"
            Make      = " "
            Model     = "Machine not found"
            SerialNum = " "
        }
    }
}

$Output | Export-Csv -Path "c:\Temp\CorpWorkstationInfo.csv" -NoTypeInformation

Smacks self upside the head … hard

TEST THE FRAKKIN CONNECTION, ITSELF, FIRST AND MAKE THAT YOUR BASIS FOR THE REST, IDIOT!!!

Ok, now I feel stupid. In earlier iterations of my code I couldn’t figure out what to test against. That was what I was missing.

Thank you, @alexw . You can tell I don’t do enough programming, anymore.