隐藏Invoke-WebRequest的进度

时间:2013-09-12 17:30:35

标签: powershell progress powershell-v3.0

如何隐藏Invoke-WebRequest的进度显示?我做了很多连续的请求并且使用了我自己的Write-Progress显示器,所以我不需要每次都在它下面弹出一个内置的显示器。

我使用根据Invoke-WebRequest的结果自动创建的mshtml结果(IE COM对象),因此我无法切换到WebClient或类似的东西,除非有人提供说明关于如何从WebClient请求中获取mshtml对象。

1 个答案:

答案 0 :(得分:60)

使用$ progressPreference变量。默认情况下,它应该具有“继续”值,除非您在别处编辑它,这告诉Powershell显示进度条。由于您提到您有自己的自定义进度显示,因此我会在执行cmdlet后立即重置它。例如:

$progressPreference = 'silentlyContinue'    # Subsequent calls do not display UI.
Invoke-WebRequest ...
$progressPreference = 'Continue'            # Subsequent calls do display UI.
Write-Progress ...

有关about_preference_variables的偏好变量的更多信息。这是$ ProgressPreference的条目:

$ProgressPreference
-------------------
Determines how Windows PowerShell responds to progress updates 
        generated by a script, cmdlet or provider, such as the progress bars
        generated by the Write-Progress cmdlet. The Write-Progress cmdlet 
        creates progress bars that depict the status of a command.

        Valid values:
          Stop:               Does not display the progress bar. Instead,
                                it displays an error message and stops executing.

          Inquire:            Does not display the progress bar. Prompts
                                for permission to continue. If you reply
                                with Y or A, it displays the progress bar.

          Continue:           Displays the progress bar and continues with
          (Default)             execution.

          SilentlyContinue:   Executes the command, but does not display
                                the progress bar.