Dwight Baer
asked on
I need a good basic sample Powershell loop script
I haven't written any Powershell scripts for a few years, and even then my scripting was basic.
I need an example of a Powershell script that walks through about 20 hosts (either by hostname or IP address) and just pings them - waiting for a couple of seconds between each one.
A simple loop - to get me started doing this again.
Thanks
I need an example of a Powershell script that walks through about 20 hosts (either by hostname or IP address) and just pings them - waiting for a couple of seconds between each one.
A simple loop - to get me started doing this again.
Thanks
ASKER
I'll try it out later today! Thanks
ASKER
Error: “Test-Connection: A parameter cannot be found that matches parameter name ‘TargetName’
Please see attached, thanks.
error.pdf
Please see attached, thanks.
error.pdf
Drop -Targetname from the command and rerun
I prefer the foreach construct in the scripting language:
$computers = Get-Content c:\sernames.txt # or get the list from wherever you can access it ideally using Get-ADComputer as the source
foreach ($computer in $computers)
{
if (Test-Connection $computer -Quiet)
{
}
$computers = Get-Content c:\sernames.txt # or get the list from wherever you can access it ideally using Get-ADComputer as the source
foreach ($computer in $computers)
{
if (Test-Connection $computer -Quiet)
{
}
Sorry, the web form behaved oddly and subitted before I was finished
$computers = Get-Content c:\sernames.txt # or get the list from wherever you can access it ideally using Get-ADComputer as the source
foreach ($computer in $computers)
{
if (Test-Connection $computer -Quiet)
{
# Process successful response
Write-Host ("Computer {0} is alive" -f $computer)
}
else
{
# Process unsuccessful response
Write-Host ("Computer {0} is failed to respond" -f $computer)
}
}
$computers = Get-Content c:\sernames.txt # or get the list from wherever you can access it ideally using Get-ADComputer as the source
foreach ($computer in $computers)
{
if (Test-Connection $computer -Quiet)
{
# Process successful response
Write-Host ("Computer {0} is alive" -f $computer)
}
else
{
# Process unsuccessful response
Write-Host ("Computer {0} is failed to respond" -f $computer)
}
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks Fox and Brent! Brent ... it worked flawlessly. Fox ... I'm sure I can learn from your suggestion, but so far I couldn't make it work. :)
2. Open Notepad and lists all your machines with a heading of Computers and save it as Computers.csv in the C:\temp
Example:
Computers
workstation1
workstation2
Server1
Server2
Open powershell and run the following command
Import-csv c:\temp\computers.csv | %{Test-Connection -TargetName $_.Computers -Count 3 -Delay 2 -MaxHops 255 -BufferSize 256}
ref link: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-connection?view=powershell-7