Hello,
I have script that runs fine in ISE but when I try to run it as a scheduled task nothing happens. I am trying to output the results of test-connection to Excel. The point of the script is to ping a few Cisco ASA/vpn connections once an hour, the tunnels drop periodically (The real point of the script is to help me get a better job, that's why the Excel automation).
When I run it in ISE there is output in the lower window. For each IP address I ping, "True" is displayed in the window. One of the addresses I ping is known to be invalid so I don't think its test-connection's True/False output.
Script is below. Any help is appreciated.
#$ErrorActionPreference = "SilentlyContinue"
$dte = get-date
$dte = $dte.tostring() -replace "[:\s/]", "."
$FileName = "ASAs_" + "$dte"
$strPath = "D:\Scripts\PingASAs\$FileName.xlsx"
$a = New-Object -comobject Excel.Application
$a.Visible = $False
$b = $a. Workbooks.Add()
$c = $b. Worksheets.Item( 1)
$c.Cells.Item(1 ,1) = "ASA"
$c.Cells.Item(1 ,2) = "Status"
$c.Cells.Item(1 ,3) = "Date and Time"
$d = $c.UsedRange
$d.EntireColumn.autofit($True)
$range = $a.Range("A1","C1")
$range.Font.Bold = $True
$range.Font.Size = 12
$intRow = 2
$ASAs = (get-content D:\Scripts\PingASAs\ASAs.txt)
foreach ($ASA in $ASAs)
{
IF (Test-Connection -ComputerName $ASA -TTL 255 -Count 2 -Quiet)
{
$c.Cells.Item($intRow , 2 ) = "Online"
$c.Cells.Item($intRow , 2).interior.colorindex = 4
}
Else
{
$c.Cells.Item($intRow , 2 ) = "Offline"
$c.Cells.Item($intRow , 2).Font.Bold = $True
$c.Cells.Item($intRow , 2).Font.Italic = $True
$c.Cells.Item($intRow , 2).interior.colorindex = 46
}
$c.Cells.Item($intRow , 1 ) = $ASA
$c.Cells.Item($intRow , 3 ) = $dte
$d.EntireColumn.AutoFit()
$intRow = $intRow + 1
}
#IF(Test-Path $strPath)
#{
#Remove-Item $strPath
#$a.ActiveWorkbook.SaveAs($strPath)
#}
#ELSE
#{
$a.ActiveWorkbook.SaveAs($strPath)
#}