调用Python脚本的Powershell脚本在ISE中正常工作,但在Task Scheduler中无法正常工作

时间:2020-08-15 20:11:09

标签: python powershell scheduled-tasks

我遇到了一个之前曾问过的问题,但是我的问题似乎有点不同,因为我的Powershell脚本调用了Python脚本,并且在从Task Scheduler进行调用时,它好像正常运行,但似乎没有放置正确目录中的CSV文件。实际上,我不知道它在哪里/甚至正在创建CSV文件。我不确定是否是调用Python脚本的Powershell脚本可能是问题所在。任务计划程序可能不支持此功能。

Python脚本将从网站上抓取数据并将文件放置在Python脚本(uplink.py)所在的目录中。

请参见以下脚本:

# 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

当我从ISE以admin或console身份以SYSTEM身份运行此脚本时,脚本可以100%运行。当它使用Python脚本参数启动pythonw进程时,它将在正确的文件夹中创建CSV文件,以使我的脚本能够正确进行。但是,当我从Task Scheduler运行它时,它似乎正在运行pythonw进程,并且在完成后由于找不到CSV文件而失败。

任务计划程序详细信息 作为系统运行 程式:powershell.exe 参数:-NoProfile -NoLogo -NonInteractive -ExecutionPolicy绕过-文件“ C:\ Stuff \ Scripts \ Meraki Health Checks \ Automated_Health_Checks.ps1”

不确定将这些CSV文件放置在何处。

我一直在绞尽脑汁想办法解决这个问题,但现在我全神贯注。如果有人可以建议我如何解决这个问题,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

任务计划程序在什么条件下运行有问题的脚本?与之类似,事件触发器是什么?另外,我可以看到如何安排任务以系统身份运行,但是在测试脚本时,如何从以系统身份运行的Powershell控制台中手动测试脚本?该控制台窗口是否也在系统提升时运行?难道就是您需要以最高权限运行命令?