虽然在 onCreated 内部卡在 PowerShell 中的数组上但在 ISE 中工作正常

While inside of onCreated gets stuck on a array in PowerShell but works fine in ISE

编写了一个脚本来监视文件夹中的新文件。当调用 onCreated 时,它递增到我列表中的下一个驱动器盘符,然后免费检查该驱动器 space。如果免费 space 在我的要求之下,它会再次增加。我的完整脚本在 ISE 中运行良好。

这是我仍然无法开始工作的简化版本。 当在 PowerShell 中 运行 并且它卡在 $DriveVar 并且只输出 "marker1"。如果您在 ISE 中 运行,对于您复制到文件夹中的每个文件,marker2 行正确地轮换驱动器号。

$filter = '*.*'
$Watchfolder = "C:\Script\watch"
$LocationArray = "L","J","K","Q","S"
$LocationCount = [int]$LocationArray.Count
$LastUsed = [int]0

$Watcher = New-Object IO.FileSystemWatcher $Watchfolder, $filter -Property @{
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite' }

$onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier FileCreated -Action {
    $Freespace = [long]-1
    while ($Freespace -le 0) {
        $LastUsed++
        if ($LastUsed -ge $LocationCount) {
            $LastUsed = 0 }
        Write-Host "marker1"

        $DriveVar = $LocationArray[$LastUsed]
        Write-Host "marker2 $DriveVar"
        $Freespace = 5
    }
}

PowerShell 中的输出:

marker1

ISE 中的输出:

marker1
marker2 J
marker1
marker2 K
marker1
marker2 Q

我尝试为 $DriveVar 和其他一些东西调用一个函数(所有这些都在 ISE 中工作),但我似乎无法让它工作。如果我单独使用 while 循环(没有 onCreated),它在 Powershell 中工作正常。

看起来这是一个范围问题。我用你的代码做了一些测试,得到了相同的结果。我能够通过将变量范围限定为 $global: 来解决问题,然后得到了想要的结果。试试这个:

$filter = '*.*'
$Watchfolder = "C:\Script\watch"
$global:LocationArray = "L","J","K","Q","S"
$global:LocationCount = [int]$LocationArray.Count
$global:LastUsed = [int]0

$Watcher = New-Object IO.FileSystemWatcher $Watchfolder, $filter -Property @{
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite' }

$onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier FileCreated -Action {
    $Freespace = [long]-1
    while ($Freespace -le 0) {
        $global:LastUsed++
        if ($global:LastUsed -ge $global:LocationCount) {
            $global:LastUsed = 0 }
        Write-Host "marker1"

        $DriveVar = $global:LocationArray[$global:LastUsed]
        Write-Host "marker2 $DriveVar"
        $Freespace = 5
    }
}

我只是在你的 Write-Host "marker1" 行之后尝试 Write-Host $LocationArray 发现了这个,它只是吐出一个空行。

我们知道全局变量不受欢迎,但对于您的用例,将您的驱动器、计数、上次使用设置为全局变量,其工作方式如下所示。

$filter = '*.*'
$Watchfolder = "D:\Scripts\watch"

$Global:LocationArray = "L","J","K","Q","S"
$Global:LocationCount = [int]$LocationArray.Count
$Global:LastUsed = [int]0

$Watcher = New-Object IO.FileSystemWatcher $Watchfolder, $filter -Property @{
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite' }

# Clear previous event
Unregister-Event -SourceIdentifier FileCreated -ErrorAction SilentlyContinue -Force

$onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier FileCreated -Action {
    $Freespace = [long]-1
    while ($Freespace -le 0) {
        $LastUsed++
        if ($LastUsed -ge $LocationCount) 
        { $LastUsed = 0 }
        Write-Host "marker1"

        $DriveVar = $LocationArray[$LastUsed]
        Write-Host "marker2 $DriveVar"
        $Freespace = 5
    }
}

...在 ISE

 marker1
marker2 J
marker1
marker2 K
marker1
marker2 Q

...在控制台主机

 marker1
marker2 J
marker1
marker2 K
marker1
marker2 Q