Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Scheduled task to execute powershell scripts never completes

$
0
0

Hi

Each time I  have manually execute the below script via Task Scheduler on windows SBS server 2008, the job just remains at "Running" status - never appears to complete. Last attempt has been over a day and hasn't completed.

However if i run the script manually via powershell editor , it works fine. takes about 10-15mins or so to complete.

 

Here is the powershell version i'm running on the server;

Major  Minor  Build  Revision

-----  -----  -----  --------
2      0      -1     -1      

Not sure why the Task Scheduler is having problems. I cannot find any errors in the logs...

Any ideas?

 

$Subnet = "192.168.0"

$MinAddress = 60

$MaxAddress = 254

# step through each address from MinAddress to MaxAddress

$emailtext=for($Address=$MinAddress;$Address -le $MaxAddress;$Address++){

    # make a text string for the current IP address to be tested

    $TestAddress = $Subnet+"."+[convert]::ToString($Address)

    # do the ping(s), don't display red text if the ping fails (erroraction)

    $Result = Test-Connection -ComputerName $TestAddress -Count 2 -ErrorAction SilentlyContinue

    if($Result -eq $null){

        Write-Output "No reply from $TestAddress"

        #Write-Host "No reply from $TestAddress"

        # have to use try/catch as the GetHostByAddress errors in a nasty way if it doesn't find anything

        try{

            # do the reverse DNS query

            $HostName = [System.Net.Dns]::GetHostByAddress($TestAddress).HostName

            if($HostName -ne $null){

                # found something in DNS, display it

                Write-Output " ($HostName)"

             #   Write-Host " ($HostName)"

            }

        } catch {

            # didn't find anything (GetHostByAddress generated an error), just do a CRLF

            #Write-Output ""

        }

    }   

} Send-MailMessage -SmtpServer 192.168.0.10 -Subject "ping sweep" -From srv01@domain.com -To support@domain.com -body ($emailtext | Out-String)

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles