I created a Powershell script using an account that is part of the Administrators Group. I can run it without error if I manually trigger it as a standalone script. It launches a web scraping python script and then uploads the resulting csv file to my webserver. But when I make that script a scheduled task, I get the "The system cannot find the specified file" error when I manually trigger the task and it fails to write a log file. Here is the standalone code:
\# Define the path to the Python executable, your Python script, and the CSV file
$pythonExe = "C:\\Python312\\pythonw.exe"
$scriptPath = "D:\\OneDrive\\Websites\\Howe Street Reporter\\assets\\files\\Most_Actives_TSXV-EG.py"
$csvFilePath = "D:\\OneDrive\\Websites\\Howe Street Reporter\\assets\\files\\TSXV_Most-Actives.csv"
\# FTP/SFTP details
$serverAddress = "ftp://173.231.203.237" # Change to your server's address
$remotePath = "/public_html/wp-content/themes/Howe-Street-Reporter/" # The directory on the server where the file will be uploaded
\# Retrieve credentials from Windows Credential Manager
$credential = Get-StoredCredential -Target "173.231.203.237"
\# Run the Python script
& "$pythonExe" "$scriptPath"
\# Upload the CSV file
$webClient = New-Object System.Net.WebClient
$webClient.Credentials = New-Object System.Net.NetworkCredential($credential.UserName, $credential.GetNetworkCredential().Password)
$uri = New-Object System.Uri("$serverAddress$remotePath" + \[System.IO.Path\]::GetFileName("$csvFilePath"))
$webClient.UploadFile($uri, "$csvFilePath")
Here is the scheduled task code:
\# Define the path to the Python executable, your Python script, the CSV file, and the log file
$pythonExe = "C:\Python312\python.exe"
$scriptPath = "D:\OneDrive\Websites\Howe Street Reporter\assets\files\Most_Actives_TSXV-EG.py"
$csvFilePath = "D:\OneDrive\Websites\Howe Street Reporter\assets\files\TSXV_Most-Actives.csv"
$logFilePath = "D:\OneDrive\Websites\Howe Street Reporter\assets\files\TaskLog.txt"
\# FTP/SFTP details
$serverAddress = "ftp://173.231.203.237" # Change to your server's address
$remotePath = "/public_html/wp-content/themes/Howe-Street-Reporter/" # The directory on the server where the file will be uploaded
\# Retrieve credentials from Windows Credential Manager
$credential = Get-StoredCredential -Target "173.231.203.237"
\# Create the script block to run the Python script and then upload the CSV file with error logging
$scriptBlock = {
Try {
Set-Location "D:\OneDrive\Websites\Howe Street Reporter\assets\files"
& $pythonExe $scriptPath
$webClient = New-Object System.Net.WebClient
$webClient.Credentials = New-Object System.Net.NetworkCredential($credential.UserName, $credential.GetNetworkCredential().Password)
$uri = New-Object System.Uri($serverAddress + $remotePath + [System.IO.Path]::GetFileName($csvFilePath))
$webClient.UploadFile($uri, $csvFilePath)
} Catch {
$errorMessage = $_.Exception.Message
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp - Error: $errorMessage" | Out-File -FilePath $logFilePath -Append
}
}
\# Create the scheduled task action with the script block
$action = New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-Command $scriptBlock"
\# Create the trigger to run the task every 15 minutes
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 15)
\# Register the task
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "RunPythonScriptAndUploadCSV" -Description "Runs a Python script every 15 minutes and uploads a CSV file" -RunLevel Highest
I made sure the permissions were full control. I ran the powershell ISE as administrator. The task scheduler was using an account that is part of the Administrators Group. I ran a stripped down version of a task that only wrote a log file to the same file location and it worked. I am at a loss. I am new to this, so may be missing something obvious. Forgive me if this is the case.