Powershell 2.0 脚本在 cmd.exe 中运行,但不在任务计划程序中运行

发布于 2024-12-12 07:54:52 字数 2670 浏览 5 评论 0原文

我最近在我的 SBS 2008 SP2 服务器上安装了 Powershell 2.0,并且相信它运行良好。登录到服务器时,Windows Powershell (x86)Windows Powershell 程序都将 Get-ExecutionPolicy 设置为 RemoteSigned >。两个Poweshell的Get-Host显示版本为2.0

该脚本保存在服务器的 C:\Script\Powershell\ 上,名称为 TaskScheduler_Get_SFTP_Files.ps1

如果我从位于 C:\ 的 cmd.exe 运行命令 Powershell C:\Script \Powershell\TaskScheduler_Get_SFTP_Files.ps1,然后 Powershell 使用从 PuTTY 下载的各种命令连接到 FTP 帐户,重命名下载的文件文件,然后将它们保存到其位置。在我的脚本中,我有各种 Write-Output 命令,这样,当我手动运行脚本时,我可以在 cmd shell 中看到发生了什么。这可以很好地从 cmd.exe 进行调用,我可以看到包含下载文件的最终目录。

另一方面,任务计划程序似乎需要鼓励性的甜言蜜语来完成工作(目前超出了我的词汇量)。在任务计划程序中,我设置了一个具有以下规范的作业(从 XML 导出复制),并请您注意我如何使用参数调用 Powershell:

    <Principal id="Author">
       <UserId>WOODBRIDGE\ADMIN</UserId>
       <LogonType>Password</LogonType>
       <RunLevel>HighestAvailable</RunLevel>
     </Principal>
   <Settings>
     <IdleSettings>
       <Duration>PT10M</Duration>
       <WaitTimeout>PT1H</WaitTimeout>
       <StopOnIdleEnd>true</StopOnIdleEnd>
       <RestartOnIdle>false</RestartOnIdle>
     </IdleSettings>
     <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
     <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
     <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
     <AllowHardTerminate>true</AllowHardTerminate>
     <StartWhenAvailable>false</StartWhenAvailable>
     <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
     <AllowStartOnDemand>true</AllowStartOnDemand>
     <Enabled>true</Enabled>
     <Hidden>false</Hidden>
     <RunOnlyIfIdle>false</RunOnlyIfIdle>
     <WakeToRun>true</WakeToRun>
     <ExecutionTimeLimit>PT4H</ExecutionTimeLimit>
     <Priority>7</Priority>
   </Settings>
   <Actions Context="Author">
     <Exec>
       <Command>Powershell</Command>
       <Arguments>-NoProfile -File C:\Script\Powershell\TaskScheduler_Get_SFTP_Files.ps1</Arguments>
     </Exec>
   </Actions>

当查看任务的历史记录时,其中一项是EventID 201,一般注释为“任务计划程序已成功完成任务“\XXX\YYYYYYYYYYYYYYYYYYYYYY”,实例“{23dee164-ea4b-4ed5-ba48-19f07bb83f3e}”,操作“C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.EXE”,返回代码 0。”

有任何帮助吗?

I have recently installed Powershell 2.0 onto my SBS 2008 SP2 server and belive it is working well. When logged onto the server, both Windows Powershell (x86) and Windows Powershell programmes have Get-ExecutionPolicy set to RemoteSigned. The Get-Host of both Poweshell shows the version as 2.0.

The script is saved on the C:\Script\Powershell\ of the Server and is called TaskScheduler_Get_SFTP_Files.ps1

If I run from cmd.exe at C:\ the command Powershell C:\Script\Powershell\TaskScheduler_Get_SFTP_Files.ps1, Powershell then connects to the FTP account using various commands downloaded from PuTTY, renames the downloaded files and then saves them to their location. Throughout my scipt, I have various Write-Output commands as that way, when I run the script manually I can see at the cmd shell whats happening. This works fine calling from cmd.exe and I can see the final directory with the downloaded files.

Task Scheduler on the other hand appears to need sweet words of encouragement to do the work (currently beyond my vocabulary). Within Task Scheduler, I have set up a job that has the following specification (copied from the XML export) and draw your attention to how I am calling Powershell with the argument:

    <Principal id="Author">
       <UserId>WOODBRIDGE\ADMIN</UserId>
       <LogonType>Password</LogonType>
       <RunLevel>HighestAvailable</RunLevel>
     </Principal>
   <Settings>
     <IdleSettings>
       <Duration>PT10M</Duration>
       <WaitTimeout>PT1H</WaitTimeout>
       <StopOnIdleEnd>true</StopOnIdleEnd>
       <RestartOnIdle>false</RestartOnIdle>
     </IdleSettings>
     <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
     <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
     <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
     <AllowHardTerminate>true</AllowHardTerminate>
     <StartWhenAvailable>false</StartWhenAvailable>
     <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
     <AllowStartOnDemand>true</AllowStartOnDemand>
     <Enabled>true</Enabled>
     <Hidden>false</Hidden>
     <RunOnlyIfIdle>false</RunOnlyIfIdle>
     <WakeToRun>true</WakeToRun>
     <ExecutionTimeLimit>PT4H</ExecutionTimeLimit>
     <Priority>7</Priority>
   </Settings>
   <Actions Context="Author">
     <Exec>
       <Command>Powershell</Command>
       <Arguments>-NoProfile -File C:\Script\Powershell\TaskScheduler_Get_SFTP_Files.ps1</Arguments>
     </Exec>
   </Actions>

When looking at the history for the Task, one of the items is an EventID 201 with the General comment being "Task Scheduler successfully completed task "\XXX\YYYYYYYYYYYYYYYYYYYYY" , instance "{23dee164-ea4b-4ed5-ba48-19f07bb83f3e}" , action "C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.EXE" with return code 0."

Any help would be appreciated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

脱离于你 2024-12-19 07:54:52

@nimizen 解决方案将起作用,但在 PowerShell 2.0 中,有一种稍微更简单的方法来启动脚本文件:

-NoLogo -File C:\Script\Powershell\TaskScheduler_Get_SFTP_Files.ps1

您是否也使用 -NoProfile 选项取决于您的配置文件是否加载脚本所需的模块或管理单元。

@nimizen solution will work but in PowerShell 2.0 there is a slightly easier way to launch script files:

-NoLogo -File C:\Script\Powershell\TaskScheduler_Get_SFTP_Files.ps1

Whether or not you also use the -NoProfile option depends on whether your profile loads modules or snapins that the script requires.

牵你手 2024-12-19 07:54:52

这是一件奇怪的事,我还没有完全弄清楚。感谢所有受访者的贡献。我已修改任务计划程序中的参数以显示 -NoLogo -NoProfile -File ...

唯一的问题是我的 Powershell 脚本不会执行创建新目录、下载 FTP 文件并重命名它们的有用段。在脚本的开头,我有一个 dir >将执行的 C:\Temp\Dir.txt 命令(这只是为了查看任务计划程序是否确实启动了任务),但是我感兴趣的实际部分(从 FTP 下载和处理信息的部分)不起作用。

在任务计划程序中,我必须将安全选项更改为“仅在用户登录时运行”,并且用户是 SBS 2008 的管理员。如果我尝试将任务保留为“无论用户是否运行”已登录或不”并勾选了“以最高权限运行”复选框,仅运行获取目录副本的部分。

仅供参考,我只在 Powershell 中编程一周,对正则表达式不太熟悉,所以我的代码效率相当低(有人可能能够发现为什么代码的 FTP 部分不运行)? 目前,我将把任务计划程序保留为“仅在用户登录时运行”,至少这样文件已下载并 谢谢大家。(很抱歉发布

了我糟糕的代码,但对于某些人来说,为什么 dir > 之外的部分可能很明显?当使用任务计划程序“无论用户是否登录都运行”时,C:\Scripts\Powershell\dir.txt 不会运行,它可能会帮助使用非常基本的 inelegenat 和不安全的 SFTP 脚本来下载文件的人?)

# -----------------------------------------------------------------------------
clear
dir > C:\Scripts\Powershell\dir.txt
$ErrorActionPreference = 'SilentlyContinue'
# Do not change the MM to mm as it will NOT return the month!
[string]$TodayDate = Get-Date -Format "yyyyMMdd"
[string]$DayName = Get-Date -Format "dddd"


# -----------------------------------------------------------------------------
# Define the environment variables
$ScriptPath = 'W:\IT\Utilities\PuTTY\'
$DestFolder = 'W:\BBBB\Statements\'
$BBBB_acc = '[email protected]:outgoing/*.*'
$BBBB_pwd = 'myPassword'
$DoDelete = $false
$Ext = @(".csv", ".pdf")
$ExpectedFileNames = @("marginreport", "XXX14444", "XXX1cash", "XXX1money", "XXX1opnpos", "XXX1trades", "XXX1_an", "XXX1_ds", "XXX1_ep", "XXX1_ms")
$ReplacedFileNames = @("Margin_MAC", "Call_Interest", "XXX_Cash", "XXX_Money", "XXX_Open", "XXX_Trades", "Margin_Analysis", "FFO", "XXX_EP", "Margin_Summary")


$DoDownload = $true
IF ($DayName -eq "Saturday") {$DoDownload = $false}
IF ($DayName -eq "Sunday") {$DoDownload = $false}


 # -----------------------------------------------------------------------------
if ($DoDownload) {
    # Make sure the destination directories exist
    IF (!(Test-Path $DestFolder)){
        New-Item -type directory -path $DestFolder
        # Write-Output ('Created target directory: ' + $DestFolder)
        }

    $TodaysDestFolder = $DestFolder + $TodayDate    
    IF (!(Test-Path $TodaysDestFolder)){
        New-Item -type directory -path $TodaysDestFolder
        # Write-Output ('Created todays target directory: ' + $TodaysDestFolder)
        }


    # -----------------------------------------------------------------------------
    # SFTP todays Files
    # Old method of calling calling a batch file .\Download_BBBB_Outgoing.bat
    & ($ScriptPath + '\pscp.exe') -sftp -P 22 -pw $BBBB_pwd $BBBB_acc $DestFolder
    # Write-Output ("Finished Downloading Files")


    # -----------------------------------------------------------------------------
    # Create the FTP Delete Script, Rename and Move the Files
    # The PuTTY batch files need to be ASCII, Powershell by default may write in UNICODE
    # Write-Output ('Creating Script File for FTP')
    $BBBB_Pattern = '\.(csv|pdf)'
    $BBBB_Script_FileName = "SFTP_BBBB_Delete_Files.txt"
    $BBBB_Script_FullFileName = $DestFolder + "\" + $BBBB_Script_FileName

    # Get-ChildItem $DestFolder -Recurse seems to traverse all subdirectories
    $Count = 0
    "cd outgoing" | Out-File $BBBB_Script_FullFileName -encoding ASCII -force
    Get-ChildItem $DestFolder | Foreach-Object {
        if ($_.Name -match $BBBB_Pattern) {
            # Append the instruction to delete the file to the FTP script
            "del " + $_ | Out-File $BBBB_Script_FullFileName -encoding ASCII -append

            # Find the extension of the file
            $i = 0
            while ((($_.name).ToLower()).IndexOf($Ext[$i]) -eq -1){
                $i++}

            # See if there is a replacement name for the file
            $j = 0
            while ((($_.name).ToLower()).IndexOf($ExpectedFileNames[$j]) -eq -1){            
                $j++}

            # Construct FileName
            $FTPDateStamp = ($_.name).substring(($_.name).length - 14, 14)
            $FTPDateStamp = $FTPDateStamp -replace("\.","")
            $IdxExt = (($_.Name).tolower()).IndexOf($Ext[$i])
            if ($j -eq -1){
                $NewName = ($_.name).substring(0,$IdxExt) + '_20' + $FTPDateStamp + $Ext[$i]
                }
            else {
                $NewName = $ReplacedFileNames[$j] + '_20' + $FTPDateStamp + $Ext[$i]
                }

            Rename-Item ($DestFolder + "\" + $_) -NewName $NewName
            Move-Item ($DestFolder + $NewName) $TodaysDestFolder
            $Count = $Count + 1
            }
        }

    # -----------------------------------------------------------------------------
    # Delete the downloaded files from the SFTP
    # PSFTP will terminate the batch if an error occurs. This can be changed with the -be switch
    # See 6.1.1 of the PuTTY release notes
    if ($DoDelete) {
        if ($Count -gt 0) {
            # Write-Output ('Deleting the downloaded files from SFTP account')
            & ($ScriptPath + '\psftp.exe') -batch [email protected] -pw $BBBB_pwd -P 22 -b $BBBB_Script_FullFileName
            }
        }

    Remove-Item $BBBB_Script_FullFileName
    $ErrorActionPreference = 'Continue'
    # Write-Output ('Script finished to download from BBBB SFTP account')
    }

This was a strange one which I have not quite got to the bottom of. Thanks to all the respondents for the contributions. I have ammended the arguments in the Task Scheduler to show -NoLogo -NoProfile -File ...

The only problem is that my Powershell sricpt would not execute the useful segment that created the new directory, download the FTP files and rename them. At the begining of the script, I had a dir > C:\Temp\Dir.txt command which would execute (this was simply to see whether the Task Scheduler was indeed kicking off the task), however the actual part of intetest to me (the bit that downloaded and processes the information from the FTP did not work.

From within Task Scheduler, I have had to change the security options to "Run only when the user is logged on" with the user being the Administrator for SBS 2008. If I try to keep the task as "Run whether user is logged on or not" and have the checkbox ticked "Run with the heighest privelages", only the part that takes a copy of the directory runs.

FYI, I have only been programming in Powershell for a week and am not that comfortable with regular expressions so my code is quite inefficient (someone may be able to spot why the FTP part of the code does not run)? For the moment, I will leave the Task Scheduler to "Run only when the user is logged on" as at least this way the files are downloaded and processed.

Thanks all. (Sorry for posting my horrible code, but it may be obvious to someone why the part beyond dir > C:\Scripts\Powershell\dir.txt does not run when Task Scheduler "Run whether user is logged on or not" is used and it may help people with a very basic inelegenat and insecure SFTP script to download files?)

# -----------------------------------------------------------------------------
clear
dir > C:\Scripts\Powershell\dir.txt
$ErrorActionPreference = 'SilentlyContinue'
# Do not change the MM to mm as it will NOT return the month!
[string]$TodayDate = Get-Date -Format "yyyyMMdd"
[string]$DayName = Get-Date -Format "dddd"


# -----------------------------------------------------------------------------
# Define the environment variables
$ScriptPath = 'W:\IT\Utilities\PuTTY\'
$DestFolder = 'W:\BBBB\Statements\'
$BBBB_acc = '[email protected]:outgoing/*.*'
$BBBB_pwd = 'myPassword'
$DoDelete = $false
$Ext = @(".csv", ".pdf")
$ExpectedFileNames = @("marginreport", "XXX14444", "XXX1cash", "XXX1money", "XXX1opnpos", "XXX1trades", "XXX1_an", "XXX1_ds", "XXX1_ep", "XXX1_ms")
$ReplacedFileNames = @("Margin_MAC", "Call_Interest", "XXX_Cash", "XXX_Money", "XXX_Open", "XXX_Trades", "Margin_Analysis", "FFO", "XXX_EP", "Margin_Summary")


$DoDownload = $true
IF ($DayName -eq "Saturday") {$DoDownload = $false}
IF ($DayName -eq "Sunday") {$DoDownload = $false}


 # -----------------------------------------------------------------------------
if ($DoDownload) {
    # Make sure the destination directories exist
    IF (!(Test-Path $DestFolder)){
        New-Item -type directory -path $DestFolder
        # Write-Output ('Created target directory: ' + $DestFolder)
        }

    $TodaysDestFolder = $DestFolder + $TodayDate    
    IF (!(Test-Path $TodaysDestFolder)){
        New-Item -type directory -path $TodaysDestFolder
        # Write-Output ('Created todays target directory: ' + $TodaysDestFolder)
        }


    # -----------------------------------------------------------------------------
    # SFTP todays Files
    # Old method of calling calling a batch file .\Download_BBBB_Outgoing.bat
    & ($ScriptPath + '\pscp.exe') -sftp -P 22 -pw $BBBB_pwd $BBBB_acc $DestFolder
    # Write-Output ("Finished Downloading Files")


    # -----------------------------------------------------------------------------
    # Create the FTP Delete Script, Rename and Move the Files
    # The PuTTY batch files need to be ASCII, Powershell by default may write in UNICODE
    # Write-Output ('Creating Script File for FTP')
    $BBBB_Pattern = '\.(csv|pdf)'
    $BBBB_Script_FileName = "SFTP_BBBB_Delete_Files.txt"
    $BBBB_Script_FullFileName = $DestFolder + "\" + $BBBB_Script_FileName

    # Get-ChildItem $DestFolder -Recurse seems to traverse all subdirectories
    $Count = 0
    "cd outgoing" | Out-File $BBBB_Script_FullFileName -encoding ASCII -force
    Get-ChildItem $DestFolder | Foreach-Object {
        if ($_.Name -match $BBBB_Pattern) {
            # Append the instruction to delete the file to the FTP script
            "del " + $_ | Out-File $BBBB_Script_FullFileName -encoding ASCII -append

            # Find the extension of the file
            $i = 0
            while ((($_.name).ToLower()).IndexOf($Ext[$i]) -eq -1){
                $i++}

            # See if there is a replacement name for the file
            $j = 0
            while ((($_.name).ToLower()).IndexOf($ExpectedFileNames[$j]) -eq -1){            
                $j++}

            # Construct FileName
            $FTPDateStamp = ($_.name).substring(($_.name).length - 14, 14)
            $FTPDateStamp = $FTPDateStamp -replace("\.","")
            $IdxExt = (($_.Name).tolower()).IndexOf($Ext[$i])
            if ($j -eq -1){
                $NewName = ($_.name).substring(0,$IdxExt) + '_20' + $FTPDateStamp + $Ext[$i]
                }
            else {
                $NewName = $ReplacedFileNames[$j] + '_20' + $FTPDateStamp + $Ext[$i]
                }

            Rename-Item ($DestFolder + "\" + $_) -NewName $NewName
            Move-Item ($DestFolder + $NewName) $TodaysDestFolder
            $Count = $Count + 1
            }
        }

    # -----------------------------------------------------------------------------
    # Delete the downloaded files from the SFTP
    # PSFTP will terminate the batch if an error occurs. This can be changed with the -be switch
    # See 6.1.1 of the PuTTY release notes
    if ($DoDelete) {
        if ($Count -gt 0) {
            # Write-Output ('Deleting the downloaded files from SFTP account')
            & ($ScriptPath + '\psftp.exe') -batch [email protected] -pw $BBBB_pwd -P 22 -b $BBBB_Script_FullFileName
            }
        }

    Remove-Item $BBBB_Script_FullFileName
    $ErrorActionPreference = 'Continue'
    # Write-Output ('Script finished to download from BBBB SFTP account')
    }
最美的太阳 2024-12-19 07:54:52

请尝试按如下方式更改命令参数:

-noprofile -nologo -command "&{C:\Script\Powershell\TaskScheduler_Get_SFTP_Files.ps1}"

Please try changing your command arguments as follows:

-noprofile -nologo -command "&{C:\Script\Powershell\TaskScheduler_Get_SFTP_Files.ps1}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文