I am having an issue that has been asked before but my issue seems to be a bit different as my Powershell script calls a Python script and when doing so from Task Scheduler it runs as if it is working but It doesnt seem to place the CSV files in the correct directory. In fact I have no idea where it is placing / is even creating the CSV files. I am not sure if Powershell script calling Python script may be the problem. Perhaps Task Scheduler does not support this.
The Python script scrapes data off of the web site and places the files in the directory that the Python script (uplink.py) is in.
Please see below script:
# CSV Path Variables
$csvappliancepath = "C:\Stuff\Scripts\Meraki Health Checks\Meraki Store CSVs\Appliances"
$csvotherpath = "C:\Stuff\Scripts\Meraki Health Checks\Meraki Store CSVs\Other"
$csvinroot = "C:\Stuff\Scripts\Meraki Health Checks\*.csv"
$csvnewpath = "C:\Stuff\Scripts\Meraki Health Checks\Meraki Store CSVs"
# CSV Variables for renaming
$csvappoldname = "C:\Stuff\Scripts\Meraki Health Checks\Check appliances -$((Get-Date).ToString('yyyy-MM-dd')).csv"
$csvotholdname = "C:\Stuff\Scripts\Meraki Health Checks\Check other devices -$((Get-Date).ToString('yyyy-MM-dd')).csv"
$csvothnewname = "C:\Stuff\Scripts\Meraki Health Checks\Check other devices -$((Get-Date).ToString('yyyy-MM-dd_HHmmss')).csv"
$csvappnewname = "C:\Stuff\Scripts\Meraki Health Checks\Check appliances -$((Get-Date).ToString('yyyy-MM-dd_HHmmss')).csv"
# CSV Item Variables for Import
$csvappnewitem = "C:\Stuff\Scripts\Meraki Health Checks\Meraki Store CSVs\Appliances\Cash Converters Southern Africa appliances -$((Get-Date).ToString('yyyy-MM-dd_HHmmss')).csv"
# CSV Exclusion file
$csvexclitem = "C:\Stuff\Scripts\Meraki Health Checks\Excluded Stores CSV\Meraki CSV Email Exclusions.csv"
# LOG Variables
$CurrentPath = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
$LogPath = Join-Path -Path $CurrentPath -ChildPath 'Logs'
$LogRootName = (Split-Path -Path $MyInvocation.MyCommand.Path -Leaf) -replace '\.ps1'
$TimeStamp = Get-Date -Format yyyyMMdd_HHmmss
$LogFileName = '{0}_{1}.log' -f $LogRootName, $TimeStamp
$LogFile = Join-Path -Path $LogPath -ChildPath $LogFileName
$NumberOfLogsToKeep = 10
If(Test-Path -Path $LogPath)
{
# Perform some cleanup and keep only the most recent ones
$Filter = '{0}_????????_??????.log' -f (Join-Path -Path $LogPath -ChildPath $LogRootName)
Get-ChildItem -Path $Filter |
Sort-Object -Property LastWriteTime -Descending |
Select-Object -Skip $NumberOfLogsToKeep |
Remove-Item -Verbose
}
Else
{
# No logs to clean but create the Logs folder
New-Item -Path $LogPath -ItemType Directory -Verbose
}
Start-Transcript -Path $Logfile
# Run Python Script and wait for process to end before proceeding
if (!(Get-Process "pythonw" -ErrorAction SilentlyContinue))
{
Start-Process "C:\Python\pythonw.exe" -ArgumentList '-u "C:\Stuff\Scripts\Meraki Health Checks\uplink.py"' -Wait
}
else
{
Get-Process "pythonw" | Stop-Process -Force
Start-Process "C:\Python\pythonw.exe" -ArgumentList '-u "C:\Stuff\Scripts\Meraki Health Checks\uplink.py"' -Wait
}
# Tests to see if the CSV items exist then renames, imports and moves the files
if ((Get-Item -Path $csvappoldname -ErrorAction Continue) -and
(Get-Item -Path $csvotholdname -ErrorAction Continue))
{
Rename-Item -Path $csvappoldname -NewName $csvappnewname -ErrorAction Continue
Rename-Item -Path $csvotholdname -NewName $csvothnewname -ErrorAction Continue
$merakicsv = Import-Csv $csvappnewname -ErrorAction Continue
$merakicsvexcl = Import-Csv $csvexclitem -ErrorAction Continue
Get-Item -Path "C:\Stuff\Scripts\Meraki Health Checks\*Appliances*" |
Move-Item -Destination $csvappliancepath -ErrorAction Continue
Get-Item -Path "C:\Stuff\Scripts\Meraki Health Checks\*other*" |
Move-Item -Destination $csvotherpath -ErrorAction Continue
}
else
{
Write-Host "API scrape CSV and/or Exclusion CSV is possibly missing so renaming, CSV import and moving of item could not execute. Investigation is required."
Break
}
# Foreach to iterate through CSV selected rows and send email alerts based on results
foreach ($row in $merakicsv) {
if($($row.Network) -notin $merakicsvexcl.Network)
{
if ($row.'WAN1 Status' -eq 'Ready')
{
Send-MailMessage -To $recipient -From $sender -Subject "$($row.Network) WAN1 is Ready!" -Body "$($row.Network) $wan1ready" -SmtpServer $smtpserver
}
if ($row.'WAN1 Status' -eq 'Failed')
{
Send-MailMessage -To $recipient -From $sender -Subject "$($row.Network) WAN1 is Failed!" -Body "$($row.Network) $wan1failed" -SmtpServer $smtpserver
}
if ($row.'WAN2 Status' -eq 'Failed')
{
Send-MailMessage -To $recipient -From $sender -Subject "$($row.Network) WAN2 is Failed!" -Body "$($row.Network) $wan2failed" -SmtpServer $smtpserver
}
if ($row.'WAN1 Status' -eq 'Not connected')
{
Send-MailMessage -To $recipient -From $sender -Subject "$($row.Network) WAN1 is Not connected!" -Body "$($row.Network) $wan1notconnected" -SmtpServer $smtpserver
}
if ($row.'WAN2 Status' -eq 'Not connected')
{
Send-MailMessage -To $recipient -From $sender -Subject "$($row.Network) WAN2 is Not connected!" -Body "$($row.Network) $wan2notconnected" -SmtpServer $smtpserver
}
}
}
Stop-Transcript
When I run this from ISE as admin or console as SYSTEM the script works 100%. When it starts the pythonw process with the Python script arguments it creates the CSV files in the correct folder for my script to proceed correctly. However when I run it from Task Scheduler it appears to run pythonw process and after it is done it fails as it cannot find the CSV files.
Task Scheduler Details Running as SYSTEM Program: powershell.exe Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "C:\Stuff\Scripts\Meraki Health Checks\Automated_Health_Checks.ps1"
Not sure where it would be placing these CSV files.
I have been racking my brain trying to figure this out but I am at a total loss now. If someone could please advise me on how to tackle this issue I will greatly appreciate it.