Author Posts

February 20, 2018 at 3:35 pm

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
}

February 20, 2018 at 3:37 pm

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

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

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

Is an array.

February 20, 2018 at 3:42 pm

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

February 20, 2018 at 4:25 pm

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.

February 20, 2018 at 4:52 pm

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

February 20, 2018 at 5:14 pm

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.

February 20, 2018 at 5:22 pm

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.

February 22, 2018 at 2:59 pm

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