Trouble with ForEach and defined list

Welcome Forums General PowerShell Q&A Trouble with ForEach and defined list

This topic contains 7 replies, has 4 voices, and was last updated by

js
 
Participant
8 months, 4 weeks ago.

  • Author
    Posts
  • #94207

    Participant
    Points: 0
    Rank: Member

    Still new to PowerShell Scripting and having trouble with getting the results from my ForEach $psitem in $locationN to work through the list of servers. I believe I have it improperly called out but, I'm a little stuck.

    Not sure what I am or am not doing correctly. Any help would be appreciated.

    $DeployServer='DeployServer'
    $DeployPort='8080'
    $IndexPort='9994'
    $LogResults='D:\Temp\DeployCheckResults.txt'

    $location1="(server1,server2,server3,server4)"
    $location2="(server5,server6,server7,server8)"

    $env:COMPUTERNAME | Out-File $LogResults
    Test-NetConnection $DeployServer -Port $DeployPort -InformationLevel Detailed | Out-File -FilePath $LogResults -Append

    ForEach ($psItem in $location1)

    {
    Test-NetConnection -Port $IndexPort -InformationLevel Detailed | Out-File -FilePath $LogResults -Append
    }

    ForEach ($psItem in $location2)
    {
    Test-NetConnection -Port $IndexPort -InformationLevel Detailed | Out-File -FilePath $LogResults -Append
    }

  • #94209

    Keymaster
    Points: 1,638
    Helping HandTeam Member
    Rank: Community Hero

    $location1="(server1,server2,server3,server4)"

    Doesn't create an array. It creates a single string.

    $location1=@('server1','server2','server3','server4')

    Is an array.

    • #94213

      Participant
      Points: 0
      Rank: Member

      Thank you, I'll give that a try. I wasn't thinking an array more of a comma delimited list. Thank you again.

    • #94221

      Participant
      Points: 0
      Rank: Member

      Question as even after creating the $location1 and $location2 as an array my ForEach is failing to resolve the Servers in the list and actually labeling all the servers in the array and not resolving even after fully quallifing the server names.
      $location1="@(server1,server2,server3)"
      $location2="@(server4.domain.com,server5.domain.com,server6.domain.com)

      ForEach ($psitem in $location1)
      {
      Test-NetworkConnection -Port $IndexPort -InformaitonLevel Detailed | Out-File $LogResults -Append
      }

      ForEach ($psitem in $location2)
      {
      Test-NetworkConnection -Port $IndexPort -InformaitonLevel Detailed | Out-File $LogResults -Append
      }

      I have tried to add the parameter -Computername $psitem.ComputerName
      I have tried to add the parameter -ComputerName $psitem

      In the Test-NetworkConnection -Computername $psitem.Computername -Port $indexPort -InformationLevel Detailed | Out-file $LogResults -Append

      But, I appear to still be failing to resolve-Dnsname : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty and then try the command again.

      Am I not defining the parameter correctly still?
      Thanks in advance.

  • #94224

    Participant
    Points: -12
    Rank: Member

    Hi

    Don't know about Test-NetworkConnection and how it works but assuming you want test your computernames from location variables? You don't need to do it like that.

    $location1 = 'server1','server2','server3'
    
    ForEach ($server in $location1) {
    Test-NetworkConnection -computerName $server -Port $IndexPort -InformaitonLevel Detailed | Out-File $LogResults -Append
    }
    

    In this case you have all your server names in $location1 and you'll go one by one through those and assigning server name in variable $server.

    or just

    $location1 = 'server1','server2','server3'
    
    $location1 | Test-NetworkConnection -computerName $_ -Port $IndexPort -InformaitonLevel Detailed | Out-File $LogResults -Append
    

    Where you use pipelane and $_ as server name.

    The $PSItem is same as $_ and these are used in pipeline. Example Get-service | where {$_.something -eq 'something'} or Get-Service | where {$PSItem.something -eq 'something'} gives you the same result.

    Regards

    Jake

    • #94231

      Participant
      Points: 0
      Rank: Member

      Thanks I'll give that a run. I had a list before and I see where I was going wrong in my list, and then I changed to an array and I think I see the error in the way the servers are defined in the both the list and the array.

    • #94233

      Participant
      Points: 0
      Rank: Member

      Again, thank you very much. I see that I was indeed not defining the servers in the list/array correctly and that caused all of the servers to actually be a long servername which was too long. Thank you again.

    • #94420
      js

      Participant
      Points: 218
      Helping Hand
      Rank: Participant

      I think you mean 'Test-NetConnection' not 'Test-NetworkConnection'.

The topic ‘Trouble with ForEach and defined list’ is closed to new replies.