我需要运行一个32位PowerShell脚本,该脚本将三个参数作为Windows Server 2008 R2中的计划任务。 该脚本启动一个FileSystemWatcher,所以我需要保持它活着,以便它可以继续拿起新的文件。
如果你正在浏览,你可能应该在下面的“参数”下看看。
我已经创build了一个PowerShell脚本,可以在Windows Server 2008 R2框上的x86 PowerShell控制台上正常运行。 它创build一个FileSystemWatcher,并为文件创build事件注册一个动作。 因为我需要FileSystemWatcher继续观看,所以我需要PowerShell继续运行,脚本启动后不立即退出:
该脚本有三个参数:
尽pipe如此,我无法让脚本作为计划任务正确运行。 我认为这个问题是我正在使用的论点,但是我不知道什么论点是正确的。
由于我需要脚本在32位PowerShell中运行,因此我将计划任务的程序设置为%SystemRoot%\ syswow64 \ WindowsPowerShell \ v1.0 \ powershell.exe。
我已经确保将32位PowerShell中的本地机器的执行策略设置为RemoteSigned。
我已经尝试了几个不同的变体,我的论点。 以下在控制台中运行,但以某种方式失败的任务计划程序 :
该任务运行,但它不写入日志文件,这表明它可能还没有启动FileSystemWatcher。 在任何情况下,它们在创build时都不会拾取文件:
powershell -Noexit -File "D:\Scripts\myScript.ps1" "\\otherServer\share\folder\subfolder\" "\\someserver.domain.edu\D$\working_directory\" "\\otherServer\share\folder\my_log.txt"
该任务以代码0xFFFD0000退出(请参阅Powershell计划任务的最后结果0xFFFD0000 )。 我确定任务正在运行,可以访问参数中使用的networking共享。
powershell -Noexit -File 'D:\Scripts\myScript.ps1' '\\otherServer\share\folder\subfolder\' '\\someserver.domain.edu\D$\working_directory\' '\\otherServer\share\folder\my_log.txt'
这些变体甚至不能在控制台中工作(不要在第一次写入日志中):
powershell -Noexit -File "D:\Scripts\myScript.ps1" "\\otherServer\share\folder\subfolder\ \\server.domain.edu\D$\working_directory\ \\otherServer\share\folder\my_log.txt" powershell -Noexit -command {"& 'D:\Scripts\myScript.ps1' '\\otherServer\share\folder\subfolder\' '\\server.domain.edu\D$\working_directory\' '\\otherServer\share\folder\my_log.txt'"}
基于脚本专家的博客 ,我想我应该尝试使用-Command参数而不是-Command 。 这两个变体在控制台中启动文件系统监视器,并将相应的消息写入日志文件,但不会在创build时拾取文件:
powershell -Noexit -command "& 'D:\Scripts\myScript.ps1' '\\otherServer\share\folder\subfolder\' '\\server.domain.edu\D$\working_directory\' '\\otherServer\share\folder\my_log.txt'" powershell -Noexit -command "& D:\Scripts\myScript.ps1 \\otherServer\share\folder\subfolder\ \\server.domain.edu\D$\working_directory\ \\otherServer\share\folder\my_log.txt"
因为脚本在控制台中工作,我敢肯定,问题不在于代码本身。 尽pipe如此,对于那些除非看到代码才会满意的人,脚本本身还有一些相关部分。 🙂
Function startWatcher ($onFolder) { try { #Create a file watcher $filter = "*.xlsx" $fsWatcher = New-Object IO.FileSystemWatcher $onFolder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} $fileWatcherStartDate = (Get-Date -format Mdyyyy.HH.mm.ss).Tostring() log($fileWatcherStartDate) log "Started file system watcher on $onFolder for $filter files.`r`n" return $fsWatcher } catch { handleError("Error starting file system watcher") } }
Function registerCreationEvent ($watcher) { log "Attempting to register creation event..." try { Register-ObjectEvent $watcher Created -SourceIdentifier FileCreated -Action { try { #Code to read from Excel file, create an output text file from the #content, and move both to the specified working directory. } catch { handleError("Performing main action on individual file.") } } log "Event creation registered." } catch { handleError("Error registering creation event") } }
乔治,我想我已经知道了!
最终,我回到了原来在控制台上运行它的方式(也就是使用dot-sourcing ),但在前面添加了-Noexit标志:
程序: %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe
参数: -Noexit . D:\Scripts\myScript.ps1 \\otherserver\share\folder\subfolder\ \\server.domain.edu\D$\working_dir\ \\otherserver\share\folder\my_log.txt -Noexit . D:\Scripts\myScript.ps1 \\otherserver\share\folder\subfolder\ \\server.domain.edu\D$\working_dir\ \\otherserver\share\folder\my_log.txt