Script grabbing all the ips instead of just one and placing all of them in to variable

StackOverflow https://stackoverflow.com/questions/23546753

  •  18-07-2023
  •  | 
  •  

문제

Here is what I am trying to accomplish.

  • Read all computers from a list
  • go through one by one and determine if the invoke-request produces an "OK"
  • if it does , place the computer name and the word yes in an excel spread sheet.
  • otherwise put the computer name and the word "NO" in the second column.

Instead of the script grabbing each ip in the servers.txt file .. it is putting them all together and failing. What am I missing here? As you can see below it is just putting all the ips in the $server variable.

Here is the error.

Invoke-WebRequest : Cannot bind parameter 'Uri'. Cannot convert value "http:// Computer1 Computer2 Computer3:1055" to type "System.Uri". Error: "Invalid URI: The
ERROR: hostname could not be parsed."

And the code is below

$Computers = Get-Content -Path C:\temp\servers.txt

foreach ($Server in $computers) {

$reply = Invoke-WebRequest http://$computers":1055" | Select-Object statusdescription

    if ($reply.statusdescription = "OK")
{

        $Sheet1.Cells.Item($intRowSoft, 1) = $server

        $Sheet1.Cells.Item($intRowSoft, 2) = "YES"
    $intRow = $intRow + 1


}

    else {
    $Sheet1.Cells.Item($intRowSoft, 1) = $server

    $Sheet1.Cells.Item($intRowSoft, 2) = "NO"

    $intRow = $intRow + 1
}

}
도움이 되었습니까?

해결책

The problem is that you are referencing $computers and not $server.

$reply = Invoke-WebRequest http://$server":1055" | Select-Object statusdescription
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top