Function Powershell脚本在ISE中工作,但在控制台中不工作

Function Powershell脚本在ISE中工作,但在控制台中不工作,function,powershell,Function,Powershell,我正在编写一个powershell脚本,它将启动一个进程,监视文件夹中的文件更改,然后在添加/更改文件时备份该文件夹 如果我从Powershell ISE中运行脚本,它工作正常,我可以监视文件夹,它将按预期正确保存备份 问题是我想运行一个批处理文件,该文件将运行powershell脚本。但是每当我从powershell控制台运行脚本,或者运行运行脚本的批处理文件时,它就不再工作了。脚本将运行并注册事件。但是,当我将一个文件复制到关注的文件夹中时,我只得到更改的事件,而不是创建的事件,并且不再调用

我正在编写一个powershell脚本,它将启动一个进程,监视文件夹中的文件更改,然后在添加/更改文件时备份该文件夹

如果我从Powershell ISE中运行脚本,它工作正常,我可以监视文件夹,它将按预期正确保存备份

问题是我想运行一个批处理文件,该文件将运行powershell脚本。但是每当我从powershell控制台运行脚本,或者运行运行脚本的批处理文件时,它就不再工作了。脚本将运行并注册事件。但是,当我将一个文件复制到关注的文件夹中时,我只得到更改的事件,而不是创建的事件,并且不再调用doStuff函数。我不确定如何进行调试:/

下面是我的脚本。我已经删除了与当前实际错误无关的部分,所以我在这里使用的一些变量你不会看到声明,但它们存在。当更改的事件发生时,我将主机写入控制台,但不是创建的事件,尽管如ISE中的早期说明,我同时获得了两个事件,并且一切正常

#unregister events, in case they weren't unregistered properly before. Just error     siliently if they don't exist
Unregister-Event ConsoleStopped -ErrorAction SilentlyContinue
Unregister-Event FileCreated -ErrorAction SilentlyContinue
Unregister-Event FileChanged -ErrorAction SilentlyContinue
Unregister-Event TimerTick -ErrorAction SilentlyContinue

#start the console process
Write-Host Starting console process...
$consoleProcess = Start-Process "$consoleExe" -PassThru

#register to listen for when the console stops
Register-ObjectEvent $consoleProcess Exited -SourceIdentifier ConsoleStopped -Action {
    Write-Host Console stopped

    #unregister events
    Unregister-Event ConsoleStopped -ErrorAction SilentlyContinue
    Unregister-Event FileCreated -ErrorAction SilentlyContinue
    Unregister-Event FileChanged -ErrorAction SilentlyContinue

    if(!$timer.Enabled) {
        Unregister-Event TimerElapsed -ErrorAction SilentlyContinue
        Remove-Item $timer
    }

    Remove-Item $fsw
    Remove-Item $consoleProcess
}

#watch all files/folders
$filter = '*.*'  # You can enter a wildcard filter here.

# In the following line, you can change 'IncludeSubdirectories to $true if required.                           
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property     @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'DirectoryName, FileName,     LastWrite'}

#register for FileCreated event
Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    write-host Created event has occurred
    doStuff($Event)
}

#register for FileChanged event
Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action {
    Write-Host Change event has occurred
    doStuff($Event)
}

function doStuff($event)
{
    write-host doStuff has been called

    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "The file '$name' in '$folder' was $changeType at $timeStamp" -fore     green

    if(!$timer.Enabled) {
        Write-Host Starting save timer
        Register-ObjectEvent $timer Elapsed -SourceIdentifier TimerElapsed -Action $TimerAction
        $timer.Start()
        Out-File "$backupDir\backup.log" -Append -Force -InputObject "A request for a backup created at $timeStamp"
    }
    else {
        Write-Host A backup has already been request
    }
}

function backupSave ()
{
    Write-Host Starting backup...
    $timestamp = Get-Date -Format o | foreach { $_ -replace ":", "." }
    Copy-Item $folder "$backupDir\backup_$timestamp" -Recurse -Force
}

尝试将函数移动到脚本的开头

i、 e.将函数doStuff$event和函数backupSave移到脚本开头


问题可能是因为当您从脚本调用函数时,它们尚未定义。它在Powershell ISE中工作,因为您多次运行了脚本,并且函数是在某个时间定义的。

谢谢您的建议!实际上我已经试过了,但不幸的是它没有起作用:/事实上,我把doStuff移到了开头,但不是backupSave,我明天会尝试将它们都移到上面,如果可以的话,我会发布back。你能不能也包括一些输出?还有错误信息吗?如果我在ISE中,我会得到所有预期的输出。当我复制一个新文件时,我会得到一个已创建的事件,然后根据文件的大小更改一些事件,然后是保存和备份文件的所有正确消息。当我在powershell控制台中运行它并在唯一的输出上复制文件时,我得到的是已发生更改的事件输出,但没有错误。我已将doStuff函数移到实际调用它的位置上方。我从触发的create和changed事件的-Action块中得到一个跟踪,$Event变量存在于该块中,但是doStuff函数仍然没有被调用。我不知道如何做任何类型的错误日志记录,因为我似乎没有收到任何错误,除非它是某个默默失败的东西。您可以通过检查$error变量的内容来查看是否有任何错误。看看这能不能给你一些启示。