0

I have an array of servers that I need to loop through, and have an attribute assigned only to specific servers (this happens later in the script).

Here's the array:

$test = @('test_dc','test_fp','test_ts','test_ap')

In the array, I have a domain controller, file/print, terminal server, and application servers (in that order).

The only servers that should get the attribute are the fp, ts, and ap servers.

Here's what I've tried thus far:

foreach ($item in $test) {
  Write-Host $item "`n"
  Write-Host "Start IF here `n"
  if ($item.Name -like '*fp*') {
    Write-Host "Found $item"
  } else {
    Write-Host "ELSE `n"
    Write-Host '-=-=-=-=-'
  }
}

Here's the output from that:

PS C:\Users\me\Desktop> .\scratch.ps1

test_dc

Start IF here

ELSE

-=-=-=-=-

test_fp

Start IF here

ELSE

-=-=-=-=-

test_ts

Start IF here

ELSE

-=-=-=-=-

test_ap

Start IF here

ELSE

-=-=-=-=- 
PS C:\Users\me\Desktop>

According to the way I think it should work, I should see this:

...
test_fp

Found test_fp
...

I've also tried this:

if ($test -contains '*fp') {
  Write-Host "Found $_"
} else {
  Write-Host 'end'
}

and I just get an empty line.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Joe-L_177
  • 25
  • 4
  • usage of the asterisk part of the string as if treating it as wildcard does not work in powershell, drop the asterisk and try it. Or use a regex pattern in that case. – t0mm13b May 09 '17 at 20:41
  • 6
    `$item` is just a normal string, it has no `name` property. Change `$item.name -like '*fp*'` to `$item -like '*fp*'` – Mathias R. Jessen May 09 '17 at 20:45
  • 1
    Is the only goal to see it on the screen? Because, that is all that Write-Host can do. It cannot be redirected. – lit May 09 '17 at 22:59
  • @t0mm13b: The entire point of PowerShell's `-like` operator is to support wildcard patterns containing metacharacters such as `*` (asterisk). The only problem here is that the input is comprised of _strings_, and strings have no `.Name` property. – mklement0 May 09 '17 at 23:56
  • @lit it can be redirected, it was changed in PSv5 - http://stackoverflow.com/questions/38523369/write-host-vs-write-information-in-powershell-5#comment64549666_38526931 – TessellatingHeckler May 10 '17 at 00:25
  • Thank you all for the help. – Joe-L_177 May 10 '17 at 13:38

1 Answers1

3

You're seeing extra info being written to host as you have indefinite writes for each item, regardless if it matches. Since you're including the else statement as well, you're going to see extra stuff written for the non-matched items. Your foreach loop also specifies the name attribute for the object, while your $test array only contains strings.

Here's what I updated it to to limit to only write the host name in your loop if it matches *fp*, otherwise writing your divider:

$test = @('test_dc','test_fp','test_ts','test_ap')
foreach ($item in $test) {
  if ($item -like '*fp*') {
    Write-Host "Found $item"
  } else {
    Write-Host '-=-=-=-=-'
  }
}

Running that will output this:

-=-=-=-=-
Found test_fp
-=-=-=-=-
-=-=-=-=-
scrthq
  • 1,039
  • 11
  • 17
  • Thank you! I figured it was something like that, but I wasn't sure. Just out of curiosity, I changed `-contains` to `-like` in the second example and the script returned 'Found' as I expected, but why didn't it return the found text, or anything, represented by `$_`? – Joe-L_177 May 10 '17 at 13:33