做一个PowerShell的函数/任务在后台运行 [英] Make a Powershell function/task run in the background

查看:1072
本文介绍了做一个PowerShell的函数/任务在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,可以让我写文件的文件路径到一个文本文件,这取决于您的输入。这听起来令人困惑,但我不知道一个更好的方式把它,所以这里的功能:

I have a function that lets me write the file-path of files to a text file, depending on your input. That sounds confusing, but I don't know of a better way to put it, so here's the function:

Function writeAllPaths([string]$fromFolder,[string]$filter,[string]$printfile) {
    Get-ChildItem -Path $fromFolder -Recurse $filter | Select-Object -Property FullName > $printfile
}

第一个参数是文件夹,你开始从搜索。结果
第二个参数,过滤器。 * .ZIP例如,将列出所有zip文件。
第三个参数,你必须提供该文本文件将结束。

First argument being the folder you start your search from.
Second argument, the filter. *.zip for instance, will list all zip files. Third argument, you have to provide where the text file will end up.

使用范例: writeAllPaths C:\\ * .zip文件C:\\ allZips.txt

问题是,当我这样做,直到把它完成PowerShell不会接受命令。这是不是很高效。有没有办法启动的时候有这个在后台运行。而当它完成preferably,给一点消息。我可以开口,在该过程的中间被创建的任何文件...

The thing is, when I do this, Powershell won't accept commands until it's done. Which isn't very productive. Is there a way to have this run in the background when started. And preferably, give a little message when it's done. I could be opening any file that's being created in the middle of the process...

此外,我在Windows 7上,所以我猜,我有PowerShell 2.0中

Also, I'm on Windows 7, so I'm guessing that I have Powershell 2.0

是啊,我不知道这件事:P

Yeah, I'm not sure about it :p

编辑:

我用的启动工作,所建议的,如下:

I used Start-Job, as suggested, as follows:

Function writeAllPaths([string]$fromFolder,[string]$filter,[string]$printfile) {
  Start-Job -ScriptBlock {Get-ChildItem -Path $fromFolder -Recurse $filter | Select-Object -Property FullName > $printfile}
}

但是,不创建的文件。旧的功能确实创建了一个文件。结果

However, the file isn't created. The old function does create a file.

EDIT2:这将是preferable有这项功能在我的PowerShell配置文件中。这样一来,我可以执行它,每当我想要的,而不必在具体的PS1文件每次我开机Powershell的时间来加载。

it would be preferable to have this function in my Powershell profile. This way, I can execute it whenever I want, instead of having to load in the specific ps1 file every time I boot Powershell.

在PowerShell配置更多信息可以在这里找到
您可以通过键入召唤自己的个人资料:记事本$简介

More info on the Powershell profile can be found here You can summon your own profile by typing: notepad $profile

推荐答案

在新的范围内创建的后台作业,为你WriteAllPaths函数中定义您的参数没有定义。试试这个,你会看到他们是不是:

In the new scope you create for the background job, your parameters defined for you WriteAllPaths function are not defined. Try this and you'll see they aren't:

Function writeAllPaths([string]$fromFolder,[string]$filter,[string]$printfile) 
{    
    Start-Job { "$fromFolder, $filter, $printFile" }
}

$job = WriteAllPaths .\Downloads *.zip zips.txt
Wait-Job $job
Receive-Job $job

, ,

试试这个:

Function writeAllPaths([string]$fromFolder, [string]$filter, [string]$printfile) 
{    
    Start-Job {param($fromFolder,$filter,$printfile) 
               "$fromFolder, $filter, $printfile" } `
               -ArgumentList $fromFolder,$filter,$printfile
}

$job = WriteAllPaths .\Downloads *.zip z.txt
Wait-Job $job
Receive-Job $job

.\Downloads, *.zip, z.txt

现在你看到你的输出,因为我们通过通过-ArgumentList的脚本块传递的参数。我会建议是,可以选择使用后台作业功能。只要坚持这个函数定义您的个人资料和你设置:

Now you see your output because we passed the parameters through to the scriptblock via -ArgumentList. What I would recommend is a function that can optionally use a background job. Just stick this function definition in your profile and you're set:

function WriteAllPaths([string]$FromFolder, [string]$Filter, 
                       [string]$Printfile, [switch]$AsJob) 
{
    if (![IO.Path]::IsPathRooted($FromFolder)) {
        $FromFolder = Join-Path $Pwd $FromFolder
    }
    if (![IO.Path]::IsPathRooted($PrintFile)) {
        $PrintFile = Join-Path $Pwd $PrintFile
    }

    $sb = {
        param($FromFolder, $Filter, $Printfile)
        Get-ChildItem $FromFolder -r $filter | Select FullName > $PrintFile
    }

    if ($AsJob) {
        Start-Job $sb -ArgumentList $FromFolder,$Filter,$PrintFile
    }
    else {
        & $sb $FromFolder $Filter $PrintFile        
    }
}

测试功能(同步),像​​这样:

Test the function (synchronously) like so:

$job = WriteAllPaths Downloads *.zip z.txt -AsJob
Wait-Job $job
Receive-Job $job

请注意,我测试如果路径是根,如果没有我prepending当前目录。我这样做是因为后台作业的首发DIR并不总是一样的,你从执行的启动工作在哪里。

Note that I'm testing if the path is root and if not I'm prepending the current dir. I do this because the background job's starting dir is not always the same as where you executed Start-Job from.

这篇关于做一个PowerShell的函数/任务在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆