我需要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 …
我有脚本监视特定目录中的文件创建。\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我应该做什么,以便我可以调试此代码块?
$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} \nRun Code Online (Sandbox Code Playgroud)\n