\n Hi, and welcome to the PowerShell forum! \n\n\nDon’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask! \n\n\nUse a descriptive subject. Don’t say “Need help” or “PowerShell Help”, actually summarize what the problem is. It helps the rest of us keep track of which problem is which. \n\n\nDon’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy…\n <\/blockquote>\n<\/aside>\n\n <\/p>","upvoteCount":1,"datePublished":"2021-05-06T16:59:52.000Z","url":"https://community.spiceworks.com/t/foreach-with-if-statements/799113/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Without being able to reproduce the output, as I don’t know what it spits out, maybe something like so<\/p>\n
$data = \nGet-ControllerInformation | \nConvertTo-Html -Fragment | \nOut-String | \nAdd-HTMLTableAttribute -AttributeName 'class' -Value 'controllertable' |\nForEach-Object {\n if($_ -eq $someValue){ \n $_ -replace \"<td>active</td>\", \"<td style='background-color:#00FF00'>active</td>\"\n }\n elseif($_ -eq $someOtherValue){\n $_ -replace \"<td>inactive</td>\", \"<td style='background-color:#FF0000'>active</td>\"\n }\n else{\n $_ -replace \"<td>NotSure</td>\", \"<td style='background-color:#FFFF00'>active</td>\"\n }\n}\n\n$ControllerInformation = \"<h2>Controller Information</h2> $data\"\n$report = \nConvertTo-Html -body \"$SiteInformation $LicenseInformation $ControllerInformation\" -head $CSS -Title \"VDI Health Report\"\n$report | out-file c:\\temp\\report1.html\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2021-05-06T17:16:36.000Z","url":"https://community.spiceworks.com/t/foreach-with-if-statements/799113/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"Thank you for taking the time to respond Neally, really appreciate it. It does not seem evaluate what is in the IF statement () , no errors are thrown up. the output should basically display the replacement value in a particular colour (i will replace RGB codes accordingly once this is working). I have provided you with the values that are needed in the IF statement test<\/p>\n
$data provides the below information<\/p>\n
<table class=\"controllertable\"><colgro><col /><col /><col /><col /><col /></colgroup><tr><th>Broker Name</th><th>State</th><th>Last Start Time</th><th>Registered VDA</th><th>Last Ac\ntivity Time</th></tr><tr><td>CitrixDC.Aaminah.com</td><td>__*Active*__</td><td>06/05/2021 up15:23:17</td><td>0</td><td>07/05/2021 13:27:28</td></tr></table>\n<\/code><\/pre>\nForEach-Object {\n if($_ -eq \"<td>active</td>\"){ \n $_ -replace \"<td>active</td>\", \"<td style='background-color:#00FF00'>active</td>\"\n }\n elseif($_ -eq \"<td>fail</td>\"){\n $_ -replace \"<td>fail</td>\", \"<td style='background-color:#FF0000'>fail</td>\"\n }\n else{\n $_ -replace \"<td>off</td>\", \"<td style='background-color:#FFFF00'>off</td>\"\n }\n}\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-05-07T10:38:28.000Z","url":"https://community.spiceworks.com/t/foreach-with-if-statements/799113/4","author":{"@type":"Person","name":"ahmed69","url":"https://community.spiceworks.com/u/ahmed69"}},{"@type":"Answer","text":"When you do if($x -eq “Something “) $x must really be the same as what’s in the quotes including spaces, special characters, et al—by default only case doesn’t matter. \nIt’s possible you need -like or even -match depending upon the source data.<\/p>","upvoteCount":1,"datePublished":"2021-05-07T11:59:12.000Z","url":"https://community.spiceworks.com/t/foreach-with-if-statements/799113/5","author":{"@type":"Person","name":"gary-m-g","url":"https://community.spiceworks.com/u/gary-m-g"}},{"@type":"Answer","text":"
I have played around with -match and -like parameters, stll the result is the same. As i am only looking at some partial output from the $data out could that be the issue, Syntax wise everything looks fine. Interestingly without the IF statement a static entry works ForEach-object<\/p>\n
ForEach-Object {$_ -replace “active”, “active”} so not sure what is going on here<\/p>","upvoteCount":0,"datePublished":"2021-05-07T12:18:58.000Z","url":"https://community.spiceworks.com/t/foreach-with-if-statements/799113/6","author":{"@type":"Person","name":"ahmed69","url":"https://community.spiceworks.com/u/ahmed69"}},{"@type":"Answer","text":"
if you don’t mind, can you post what output<\/p>\n
Get-ControllerInformation | \nConvertTo-Html -Fragment | \nOut-String | \nAdd-HTMLTableAttribute -AttributeName 'class' -Value 'controllertable'\n<\/code><\/pre>\n^ this produces?<\/p>\n
Maybe it would be easier to match / if filter just<\/p>\n
Get-ControllerInformation\n<\/code><\/pre>\nthe output of this FIRST and then convert it to html?<\/p>","upvoteCount":0,"datePublished":"2021-05-07T12:47:40.000Z","url":"https://community.spiceworks.com/t/foreach-with-if-statements/799113/7","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
This is the output from get-controller…<\/p>\n
Broker Name<\/th> State<\/th> Last Start Time<\/th> Registered VDA<\/th> Last Activity Time<\/th><\/tr> CitrixDC.Aaminah.com<\/td> Active<\/td> 06/05/2021 15:23:17<\/td> 0<\/td> 07/05/2021 15:50:39<\/td><\/tr><\/tbody><\/table>","upvoteCount":0,"datePublished":"2021-05-07T12:51:57.000Z","url":"https://community.spiceworks.com/t/foreach-with-if-statements/799113/8","author":{"@type":"Person","name":"ahmed69","url":"https://community.spiceworks.com/u/ahmed69"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Ahmed69:<\/div>\n
\nThis is the output from get-controller…<\/p>\n
Broker Name<\/th> State<\/th> Last Start Time<\/th> Registered VDA<\/th> Last Activity Time<\/th><\/tr> CitrixDC.Aaminah.com<\/td> Active<\/td> 06/05/2021 15:23:17<\/td> 0<\/td> 07/05/2021 15:50:39<\/td><\/tr><\/tbody><\/table>\n<\/blockquote>\n<\/aside>\nthis seems to work fine<\/p>\n
$input = \n@\"\n<table class=\"controllertable\"><colgroup><col /><col /><col /><col /><col /></colgroup><tr><th>Broker Name</th><th>State</th><th>Last Start Time</th><th>Registered VDA</th><th>Last Activity Time</th>\n</tr><tr><td>CitrixDC.Aaminah.com</td><td>InActive</td><td>06/05/2021 15:23:17</td><td>0</td><td>07/05/2021 15:50:39</td></tr></table>\n\"@\n\n$data = \n$input |\nForEach-Object {\n if($_ -like '*<td>Active</td>*'){\n $_ -replace \"<td>Active</td>\",\"<td style='background-color:#00FF00'>Active</td>\"\n }\n if($_ -like '*<td>InActive</td>*'){\n $_ -replace \"<td>InActive</td>\",\"<td style='background-color:#FF0000'>InActive</td>\"\n }\n}\n\n$data\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-05-07T13:46:26.000Z","url":"https://community.spiceworks.com/t/foreach-with-if-statements/799113/9","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"Thank you very much Neally, that works now. Just need to use the -like parameter with the wildcard *, for the IF test. that works with you original code you posted. Many thanks<\/p>","upvoteCount":0,"datePublished":"2021-05-07T14:12:03.000Z","url":"https://community.spiceworks.com/t/foreach-with-if-statements/799113/10","author":{"@type":"Person","name":"ahmed69","url":"https://community.spiceworks.com/u/ahmed69"}}]}}
Neally
(Neally)
May 6, 2021, 4:59pm
2
If you post code, please use the ‘Insert Code’ button. Please and thank you!
Hi, and welcome to the PowerShell forum!
Don’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!
Use a descriptive subject. Don’t say “Need help” or “PowerShell Help”, actually summarize what the problem is. It helps the rest of us keep track of which problem is which.
Don’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy…
1 Spice up