Author Posts

January 1, 2012 at 12:00 am

by turbomcp at 2012-09-09 13:41:00

Hi
i am a newbie in powershell and although i think i understand the basics i am trying to do something very simple(just as a test of doing something else) and its not working:)
im sure any powersheller can see the problem in one sec
here is my code:
$items=Get-WmiObject -List |? {$_.name -like "*tcpip*"}
foreach($i in $items)
{
if ($i.name -like "*tcpv6*")
(Write-Host -ForegroundColor blue " is the item with tcpv6")
if ($i.name -like "*tcpv4*")
(Write-Host -ForegroundColor green " is the item with tcpv4")
}

basicly i just want to get some data from somewhere
and then have powershell go through it and do certain actions based on what it finds....

thanks in advance for any help

by DonJ at 2012-09-09 13:46:29

You've written your If constructs wrong.


if ($i.name -like "*tcpv6*")
{Write-Host -ForegroundColor blue " is the item with tcpv4"}
if ($i.name -like "*tcpv4*")
{Write-Host -ForegroundColor green " is the item with tcpv6"}

The conditional code goes in {curly brackets} not (parentheses). Run "help about_if*" in the shell for more details.

by turbomcp at 2012-09-09 13:50:21

Thanks man
highly appreciated
🙂
works:)
thanks for lightning fast reply

by turbomcp at 2012-09-09 14:11:22

another quick one:)
so i changed it to this:
$items=Get-WmiObject -List |? {$_.name -like "*tcpip*"}
foreach($i in $items){
if ($i.name -like "*tcpv6")
{Write-Host -ForegroundColor blue "$i is the item with tcpv6"}
if ($i.name -like "*tcpv4")
{Write-Host -ForegroundColor green "$i is the item with tcpv4"}
}

but i noticed output is all in green like this:
\\LAP1\root\cimv2:Win32_PerfFormattedData_Tcpip_TCPv4 is the item with tcpv4
\\LAP1\root\cimv2:Win32_PerfRawData_Tcpip_TCPv4 is the item with tcpv4
\\LAP1\root\cimv2:Win32_PerfFormattedData_Tcpip_TCPv6 is the item with tcpv6
\\LAP1\root\cimv2:Win32_PerfRawData_Tcpip_TCPv6 is the item with tcpv6

so even whats suposed to be blue(v6) is green

what am i missing here?

by turbomcp at 2012-09-09 14:39:42

sorry
my mistake
it was fine:)