72

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

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

4

1 回答 1

107

使用 $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.
于 2013-09-12T17:35:12.217 回答