Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

TL;DR Version

I need to run a 32-bit PowerShell script that takes three arguments as a scheduled task in Windows Server 2008 R2. The script starts a FileSystemWatcher, so I need to keep it alive so that it can keep picking up new files.

You should probably look at the stuff under "Arguments", below if you're skimming.

Background

I've created a Powershell script that runs fine from the x86 Powershell console on my Server 2008 R2 box. It creates a FileSystemWatcher, and registers an action to the file creation event. Because I need the FileSystemWatcher to keep watching, I need powershell to keep running, not exit immediately after the script is fired:

The script takes three arguments:

  1. The path to the folder to watch
  2. The path to a folder where it will move any new .xlsx files and write an output text file based on the contents of the .xlsx file.
  3. The path to the log file.

I can't get the script to run correctly as a scheduled task, though. I think the issue is the arguments I'm using, but I don't know what arguments would be correct.

Setup

Since I need the script to run in the 32-bit Powershell, I set the program of the scheduled task to %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe.

I've made sure to set the Execution Policy to RemoteSigned for the local machine in the 32-bit Powershell.

Arguments

I've tried several different variants for my arguments. The following run in Console, but fail in some way for Task Scheduler:

Task runs, but doesn't write to log file, which indicates it probably hasn't even started the FileSystemWatcher. In any case, it doesn't pick up files when they're created:

powershell -Noexit -File "D:\Scripts\myScript.ps1" "\\otherServer\share\folder\subfolder\" "\\someserver.domain.edu\D$\working_directory\" "\\otherServer\share\folder\my_log.txt"

Task exits with code 0xfffd0000 (See this: http://www.briantist.com/errors/scheduled-task-powershell-0xfffd0000/). I am sure the task is running as the appropriate service account, which has access to the network shares used in the arguments.

powershell -Noexit -File 'D:\Scripts\myScript.ps1' '\\otherServer\share\folder\subfolder\' '\\someserver.domain.edu\D$\working_directory\' '\\otherServer\share\folder\my_log.txt'

These variants don't even work in the console (don't so much as make the first write to the log):

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'"}

Based on the Scripting Guys blog, I thought I ought to try using the -Command argument instead of -File. These two variants start the File System Watcher in console and write the corresponding message to the log file, but don't pick up files on creation:

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"

Code

Because the script works in the console, I'm pretty sure that the issue doesn't lie in the code itself. Still, here's some relevant portions from the script itself for those who won't be satisfied unless they see the code. :)

Create the FileSystemWatcher

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 M.d.yyyy.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")}
}

Register creation event

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") }
}
share|improve this question

1 Answer 1

By George, I think I've got it!

Ultimately, I reverted to the way I was originally running it in Console (that is, with dot-sourcing), but added the -Noexit flag in front:

Program: %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe

Arguments: -Noexit . D:\Scripts\myScript.ps1 \\otherserver\share\folder\subfolder\ \\server.domain.edu\D$\working_dir\ \\otherserver\share\folder\my_log.txt

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.