0

as i was wondering why my script takes so long i was seachring on google and also here in stackoverflow.

But all that i could find any close to helpful was this one here, Powershell Script Running Slowly

As I'm still pretty new to Powershell this is a little complicated to get through and take over to my script as i dont know how to handle those mentiond things anyway as i never heard of it before.

My Script is pretty easy and just gives me some Informations if there is something that returns an echo or not.

I wanted to "scan" our entire Network so I made an csv with out local Networks IP's and pass it to Powershell to "Ping" those.

But I realised that the "was not responing" part takes a long time to execute.

$list = Import-Csv -Path D:\ipcheck3.csv -UseCulture
$x=$list.IP
$ErrorActionPreference = "SilentlyContinue"
foreach ($y in $x)
{

    try
    {
        if(Test-Connection $y -Count 1 -quiet)
        {
            write-host "$y responded" 
            $y | Export-Csv -Path D:\PingSucceded.csv -Append
        }
        else
        {
            Write-Host "$y was not responding"
            $y | Export-Csv -Path D:\Pingfailed.csv -Append
        }
    }
    catch
    {
        Write-Warning "Other Error occured" 
    }

}

There are not only Windows Clients out there so WMI is not an option and I don't know how to achvie this otherwise

EDIT:

After the Workflow input this is my "Try Version"

workflow Test-IPrange
{
    Param
    (       
        $IPs
    )

    $tocheck= $IPs.IP

    foreach -parallel ($IP in $tocheck)
    {
        $pingsucceed = Test-Connection $IP -Count 1 -quiet

        if($pingsucceed -eq "True")
        {
            $IP | Export-Csv -Path D:\testj.csv -Append
        }
        else
        {
            $IP | Export-Csv -Path D:\testn.csv -Append
        }
    }
}

Test-IPrange -IPs $(Import-Csv -Path D:\ipcheck3.csv -UseCulture)

My Output of Workflow Try

#TYPE System.String
PSComputerName,"PSShowComputerName","PSSourceJobInstanceId","Length"
localhost,"True","4e208e38-f7c2-492f-9d81-6583a103c3ac","12"
localhost,"True","4e208e38-f7c2-492f-9d81-6583a103c3ac","12"

With the Help of @Fourat

i edited my code to this form

Function Custom-Ping {
    Param(
        [string]$Address
    )
    $ping = ping $Address /w 1 /n 1
    $result = ![string]::IsNullOrEmpty($ping -Like "*(0% Verlust)*")
    return $result
} 

$list = Import-Csv -Path D:\ipcheck3.csv -UseCulture
$x=$list.IP
$ErrorActionPreference = "SilentlyContinue"
foreach ($y in $x)
{

    try
    {
        if(Custom-Ping $y)
        {           
            Write-Host "$y responded"
            $y | Export-Csv -Path D:\PingsuccededV3.csv -Append
        }
        else
        {

            Write-Host "$y was not responding"
            $y | Export-Csv -Path D:\PingfailedV3.csv -Append
        }
    }
    catch
    {
        Write-Warning "Textline from CMD Command or other Error" 
    }

}

which works properly good and is faster

7
  • Can you add some numbers ?? How many IP you have in your file and how much time your script takes to process them ? Try increase/decrease the number of IPs and maybe give us an average of time per IP. Commented Mar 18, 2019 at 13:46
  • I've been there my self, and you do use a slow method. What I did to speed it up a ton, was using Workflow in a foreatch -parallel setup. It took a little time as workflow are just Powershell, but do convert the code to something else. You can read about workflows here -> learn.microsoft.com/en-us/system-center/sma/… Commented Mar 18, 2019 at 13:47
  • What OS are you on? Test-NetConnection is a very useful cmdlet on Win8.1+ Commented Mar 18, 2019 at 13:52
  • @Fourat I have about 4500 IP's to check the avg on a suceed is 0.74 seconds the avg on a fail is: 4.611 seconds Commented Mar 18, 2019 at 13:57
  • @TheIncorrigible1 I'm on a win10 system with powershell 5 Commented Mar 18, 2019 at 13:58

2 Answers 2

2

I think that your process time is spoiled by the timeouts. If all your IPs are in the local network, try to reduce the timeout (because the default value is 5 seconds).

If you have Powershell 6 :

Test-Connection $y -Count 1 -quiet -TimeoutSeconds 1

If you don't, just use ping :

ping 58.47.45.1 /w 1 /n 1

You can also use a parallel for each loop, but it won't help much if you have multiple fails :

ForEach -Parallel ($x in $y)
{
    ...
}

UPDATE

In order to handle ping results, you can use a function like this (I used the keyword 'perte' because my computer is in French) :

Function Custom-Ping {
    Param(
        [string]$Address
    )
    $ping = ping $Address /w 1 /n 1
    $result = ![string]::IsNullOrEmpty($ping -Like "*(perte 0%)*")
    return $result
} 
Sign up to request clarification or add additional context in comments.

6 Comments

i'm poorly on PW 5 and when i Try ping it will responde everytime with succeded as there was an ping but it failed and with the workflow i'm trying to figure it out how it works
@Kevin, I added a function to handle ping results.
Thank you for your help, but i now get all pings as false (i changed the word perte with our language)
I got one more question, as the output is now no psobject i get something like #TYPE System.String Length 10 how can i convert this ?
@Kevin the output of Custom-Ping ?
|
1

I've used Workflow to solve this issue my self. It's a few years ago I did it, so something better and newer is out there. But this works great for me... I've ping over 2000 computers within a few Min...

workflow Test-ComputersConnection
{
Param
(
    # Param1 help description
    $Computernames#,

    # Param2 help description
    #        [int]
    #        $Param2
)

foreach -parallel ($ComputerName in $Computernames)
{
    $ConnectionTest = Test-Connection -ComputerName $ComputerName -ErrorAction SilentlyContinue -Count 1

    if ($ConnectionTest.Address -eq $ComputerName) {
        Write-Output $(Add-Member -MemberType NoteProperty -Name "Computername" -Value $ComputerName -InputObject $ConnectionTest -PassThru )
        #Write-Verbose -Verbose -Message "[$($ComputerName)]: Replays on Ping."
    }
    Else {
        #Write-Verbose -Verbose -Message "[$($ComputerName)]: Do not replays on Ping."
    }
}
}

$OnlineNow0 = Test-ComputersConnection -Computernames $( Import-Csv -Path D:\ipcheck3.csv -UseCulture |
Select-Object -ExpandProperty name)

The code above is a quick edit of what I use... You will need to edit the $(Import ...) statement first, to make sure the PC name is being deliveret to the workflow.

I've just testet on my own computer and it gave me a reply...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.