Author Posts

September 2, 2015 at 7:23 am

This code is giving me a html output of the adcomputer names. Im pretty sure it has to do with my if else. Really appreciate your time!

http://pastebin.com/raw.php?i=2PMxfxK9

$Header = @"

TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
TR:Nth-Child(Even) {Background-Color: #dddddd;}
TR:Hover TD {Background-Color: #C1D5F8;}


$Title

"@

Import-Module ActiveDirectory
if ((Get-ADComputer -filter * -Property *| Select-Object ipv4addres) -eq $null) {
	$a = Get-ADComputer -filter * -Property * | Select-Object name, dnshostname, operatingsystem, operatingsystemservicepack, ipv4address, lastlogondate, logoncount, @{ label = "PingResults"; Expression = { "Computer is OFF" } } | sort lastlogondate -Descending | ConvertTo-Html -head $header  -Fragment 
} else {
	$b = get-adcomputer -filter * -Property * | Select-Object name, dnshostname, operatingsystem, operatingsystemservicepack, ipv4address, lastlogondate, logoncount, @{ label = "PingResults"; Expression = { tnc $_.ipv4address -InformationLevel Quiet } } | sort lastlogondate -Descending | ConvertTo-Html -head $header -fragment
}
ConvertTo-Html -head $header  -Body $a $b -Title "Domain Inventory" | Out-File c:\test.html

September 2, 2015 at 7:44 am

It's not so much an issue with your if statement as much as it is with the logical flow of the properties. You are running the Get-ADComputer command in your criteria session of your if statement and then when you get into the code block, you are generating a whole new list rather than using the one you have already tested. You need to collect your list outside of the if statement and then validate and run based on the current item.

Something like this
Note: I can't test the below code because I don't have 'tnc', but this should give you the general workflow.

Import-Module ActiveDirectory
$computerlist = Get-ADComputer -filter * -Property *
$results =@()
ForEach ($computer in $computerlist) {
	if ($computer.ipv4address -eq $null) {
		$results += $computer | Select-Object name, dnshostname, operatingsystem, operatingsystemservicepack, ipv4address, lastlogondate, logoncount, @{ label = "PingResults"; Expression = { "Computer is OFF" } }
	} else {
		$results += $computer | Select-Object name, dnshostname, operatingsystem, operatingsystemservicepack, ipv4address, lastlogondate, logoncount, @{ label = "PingResults"; Expression = { tnc $_.ipv4address -InformationLevel Quiet } }
	}
}
$results | sort lastlogondate -Descending | ConvertTo-Html -head $header -Title "Domain Inventory" | Out-File c:\test.html

September 2, 2015 at 7:52 am

In the ForEach command how does it build the $computer variable?

September 2, 2015 at 8:05 am

Cool Didn't see your update let me try it out, Thanks!

September 2, 2015 at 8:09 am

Well, your Get-ADComputer command gets a collection of ADComputer objects from your Active Directory and stores them in the $computerlist variable.

Simple Example
Position[0]: ComputerName: Workstation1, IP: 1.1.1.1, etc., etc.
Position[1]: ComputerName: Workstation2, IP: 2.2.2.2, etc., etc.
Position[2]: ComputerName: Workstation3, IP: 3.3.3.3, etc., etc.

The ForEach command then says for each of those object in the collection, set $computer to the current object run the code between the brackets.

So based off the example data above.

ForEach ($computer in $computerlist) {
Write-Host $computer.ComputerName
}

When the ForEach is reached, it will set $computer to the first object in the collection. $computer will equal ComputerName: Workstation1, IP: 1.1.1.1, etc., etc.
Then it runs the code inside the block, so $computer.ComputerName which is currently "Workstation1" is written to the console.

Once the end of the code block is reached, it loops back up to the ForEach statement and sets $computer to the next object in the collection.
$computer will now equal Position[1]: ComputerName: Workstation2, IP: 2.2.2.2, etc., etc. Then it runs the code inside the block again, but this time $computer.ComputerName equals "Workstation2".

It continues to do this until it has looped through every object in the collection. Then it moves on past the ForEach statement and runs the rest of your code.

September 2, 2015 at 8:19 am

Reading your response

September 2, 2015 at 8:20 am

I've only been using powershell.org for about a month. I see Don is active on another thread, he may be able to point to some good resources on here.

September 2, 2015 at 8:32 am

With that said += is a short hand mechanism.

$results += $computer | Select-Object name, dnshostname, operatingsystem, operatingsystemservicepack, ipv4address, lastlogondate, logoncount, @{ label = "PingResults"; Expression = { tnc $_.ipv4address -InformationLevel Quiet } }

is the same as 

$results = $results + $computer | Select-Object name, dnshostname, operatingsystem, operatingsystemservicepack, ipv4address, lastlogondate, logoncount, @{ label = "PingResults"; Expression = { tnc $_.ipv4address -InformationLevel Quiet } }

When the variable is an array this results in adding an object to the collection.

You can think of it like this.
$result = @()

At current the $result variable has an empty array in it.

$results += $computer | Select-Object name, dnshostname, operatingsystem, operatingsystemservicepack, ipv4address, lastlogondate, logoncount, @{ label = "PingResults"; Expression = { tnc $_.ipv4address -InformationLevel Quiet } }

After this runs, $results is no longer empty, it has 1 object in it.

When it runs a second time, the new object is added to the array along with the first object, so the $results variable then has 2 objects in it, etc, etc.

this is different that using just "=" by itself in that if you ran the below after running the above twice, rather than $results having 3 objects, it would just have 1 because = by itself does not keep, or ADD TO, the content that is already in the variable.

$results = $computer | Select-Object name, dnshostname, operatingsystem, operatingsystemservicepack, ipv4address, lastlogondate, logoncount, @{ label = "PingResults"; Expression = { tnc $_.ipv4address -InformationLevel Quiet } }

September 2, 2015 at 8:36 am

There are a lot of good free resource on this site at the top under Resource/Free eBooks. Several of which Don has co-authored. I just don't know which ones might cover the above topics. If you are just looking to learn anything about Powershell, I would highly recommend any of them.