我最近在我的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:\命令Powershell C:\ Script \ Powershell \ TaskScheduler_Get_SFTP_Files.ps1 中从 cmd.exe 运行,则Powershell将使用以下命令连接到FTP帐户从 PuTTY 下载的各种命令,重命名下载的文件,然后将其保存到其位置。在我的scipt中,我有各种 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 \ YYYYYYYYYYYYYYYYYYYYY“,实例”{23dee164- ea4b-4ed5-ba48-19f07bb83f3e}“,action”C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ Powershell.EXE“,返回码0 。”
任何帮助都会受到赞赏吗?
答案 0 :(得分:1)
@nimizen解决方案可以使用,但在PowerShell 2.0中,有一种更简单的方法来启动脚本文件:
-NoLogo -File C:\Script\Powershell\TaskScheduler_Get_SFTP_Files.ps1
您是否也使用-NoProfile
选项取决于您的配置文件是否加载脚本所需的模块或管理单元。
答案 1 :(得分:1)
这是一个奇怪的,我还没有到底。感谢所有受访者的贡献。我已经修改了任务计划程序中的参数来显示-NoLogo -NoProfile -File ...
唯一的问题是我的Powershell sricpt不会执行创建新目录的有用段,下载FTP文件并重命名它们。在脚本的开头,我有一个目录&gt; C:\ Temp \ Dir.txt命令将执行(这只是为了查看任务调度程序是否确实启动了任务),但是对我来说是intetest的实际部分(从FTP下载并处理信息的位)没用。
在任务计划程序中,我必须将安全选项更改为“仅在用户登录时运行”,并且用户是SBS 2008的管理员。如果我尝试将任务保持为“运行是否用户是登录或不登记“并勾选复选框”以最高的privelages运行“,只运行目录副本的部分。
仅供参考,我只是在Powershell中编程了一个星期而且对正则表达式不太熟悉所以我的代码效率很低(有人可能能够发现为什么代码的FTP部分没有运行)?目前,我将任务计划程序保留为“仅在用户登录时运行”,至少以这种方式下载和处理文件。
谢谢大家。 (很抱歉发布我可怕的代码,但是对于某些人来说,为什么除了dir&gt; 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 = 'myAccount@BBBB.com: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 myAccount@BBBB.com -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')
}
答案 2 :(得分:0)
请尝试按如下方式更改命令参数:
-noprofile -nologo -command "&{C:\Script\Powershell\TaskScheduler_Get_SFTP_Files.ps1}"