English 中文(简体)
Powerhell - Issue with nested loops
原标题:Powershell - Issue with nested loops
  • 时间:2024-03-20 01:00:28
  •  标签:
  • powershell

我感觉到这一点可能相当简单。 基本上,我的申请是,在临时执行期间,产生临时xml文档。 这可能有助于解决问题。 然而,正如刚才所说,它们是临时性的。 它们在一条特定的道路上最多有几秒,然后删除。

我正致力于使客户使用的文字自动化(而不是向他们解释如何建立快速的批量档案)。 下面是我 st的。 应当做的是检查一个目录,等待制作Xml文档。 它将坐下来不断检查,直到档案存在。 之后,它便停止了该 lo,并用复制件将Xml文档复制到新的目录。

我正在努力整合的那部分是,我希望用户能够用钥匙来结束整个文字。 在这个例子中,它留下了 key的钥匙。 理论上说,他们在开始复印程序之后,应当能够随时终止复制(在我所展示的样本代码之前,有人会冒着眼点击科索沃开始。)

然而,现在我还没有能够准确地看到这一点。 “停靠ton子”的编码系统自行运行。 但我如何融合?

第一,工作组:

$key = [System.Windows.Input.Key]::LeftCtrl
Write-Warning "Press the $key to end the copy process."
do {
    $isCtrl = [System.Windows.Input.Keyboard]::IsKeyDown($key)
    if ($isCtrl) {
        Write-Host "`nYou pressed $key. Stopping the copy process." -ForegroundColor Green; break
    }
    Write-Host "." -NoNewline
    Start-Sleep -Milliseconds 200
} while ($true)

As stated, the above works just fine. However, below is my attempt to integrate it. It doesn t work. Once the xml file exists in the directory (this has to happen in order to break out of the first do-until loop, though that could of course be removed for testing purposes), you see the copy actions written (verbose enabled), but when you enable the debugger in Powershell, the script is either still on the copy-item, or has looped back around to it (I am not sure)...

cls
#set key variable and write message on how to end process
$key = [System.Windows.Input.Key]::LeftCtrl
Write-Warning "Press the $key key to end the copy process.`n"

#write waiting for file generation
Write-Host "Waiting for temp files to be generated..`n"

#master loop, copying files until we tell it to stop
do {
    #search for new files, but loop doing nothing until they exist
    do {
        $files = Get-ChildItem $temppath | Where-Object {( New-TimeSpan $_.LastWriteTime).Hours -le 24 }
        Start-Sleep -Milliseconds 10
    } until ( $files -ne $null )

    #copy new files to new directory
    Copy-Item $files -Destination $destination -Recurse -Verbose -ErrorAction SilentlyContinue

    #look for kill-key
    $isCtrl = [System.Windows.Input.Keyboard]::IsKeyDown($key)
    if ($isCtrl) {
        Write-Host "`nYou pressed $key. Stopping the copy process." -ForegroundColor Green; break
    }

} while ( $true )

欢迎任何建议。

问题回答




相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签