相关疑难解决方法(0)

PowerShell:DownloadFileAsync的Runspace问题

我需要WebClient在PowerShell 2.0中下载文件,我想显示下载进度,所以我这样做:

$activity = "Downloading"

$client = New-Object System.Net.WebClient
$urlAsUri = New-Object System.Uri($url)

$event = New-Object System.Threading.ManualResetEvent($false)

$downloadProgress = [System.Net.DownloadProgressChangedEventHandler] {
    $progress = [int]((100.0 * $_.BytesReceived) / $_.TotalBytesToReceive)
    Write-Progress -Activity $activity -Status "${progress}% done" -PercentComplete $progress
}

$downloadComplete = [System.ComponentModel.AsyncCompletedEventHandler] {
    Write-Progress -Activity $activity -Completed
    $event.Set()
}

$client.add_DownloadFileCompleted($downloadComplete) 
$client.add_DownloadProgressChanged($downloadProgress)

Write-Progress -Activity $activity -Status "0% done" -PercentComplete 0
$client.DownloadFileAsync($urlAsUri, $file)    

$event.WaitOne()
Run Code Online (Sandbox Code Playgroud)

There is no Runspace available to run scripts in this thread.$downloadProgress处理程序中遇到错误,这是合乎逻辑的.但是,我如何Runspace为(可能)属于的线程提供ThreadPool …

powershell multithreading

11
推荐指数
2
解决办法
7251
查看次数

Powershell调试事件-Action代码块

我有脚本监视特定目录中的文件创建。\n我在创建 System.IO.FileSystemWatcher 后使用 Register-ObjectEvent,

\n\n

它工作得很好,但如果我在 -Action 代码块中设置断点,IDE 会生成:

\n\n

警告:不会命中 \'D:\\My Stuff\\Desktop\\scripts\\fileWatcher.ps1:15\' 上的断点行断点

\n\n

在我将文件放入我正在监视的目录中后,此消息立即发生,并且我可以看到我的写入主机在上述“警告”之后立即打印出我的消息。

\n\n

这是正常的吗?
\n我需要添加更多代码并且可以真正使用调试器..\n我应该做什么,以便我可以调试此代码块?

\n\n
$fsw = [System.IO.FileSystemWatcher] $path\n\nRegister-ObjectEvent -InputObject $fsw \xe2\x80\x93EventName Created -SourceIdentifier DDFileCreated -Action { \n    $name = $Event.SourceEventArgs.Name \n    $changeType = $Event.SourceEventArgs.ChangeType \n    $timeStamp = $Event.TimeGenerated \n    Write-Host "The file \'$name\' was $changeType at $timeStamp" -fore green \n    Out-File -FilePath $logFile -Append -InputObject "The file \'$name\' was $changeType at $timeStamp"\n} \n
Run Code Online (Sandbox Code Playgroud)\n

powershell-ise

5
推荐指数
1
解决办法
1210
查看次数