【问题标题】:How I can hide the progress bar (compression) in powershell?如何在 powershell 中隐藏进度条(压缩)?
【发布时间】:2014-06-19 08:12:26
【问题描述】:

我正在测试一个小脚本来压缩文件,我使用“PowerGUI 脚本编辑器”将脚本编译为可执行文件,一切正常,但是当你去运行时。 exe并压缩文件,显示进度条,我想知道如何隐藏指示压缩的进度条,我的代码是:

$srcdir = mysourcedir
$zipFilename = \zipfilename
$zipFilepath = $env:temp
$zipFile = "$zipFilepath$zipFilename"

#Prepare zip file
if(-not (test-path($zipFile))) {
set-content $zipFile ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipFile).IsReadOnly = $false  
} 

$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipFile)
$files = Get-ChildItem -Path $srcdir

foreach($file in $files) { 
$ProgressPreference = ’SilentlyContinue’
$zipPackage.CopyHere($file.FullName)
#using this method, sometimes files can be 'skipped'
#this 'while' loop checks each file is added before moving to the next
while($zipPackage.Items().Item($file.name) -eq $null){
$ProgressPreference = ’SilentlyContinue’
    Start-sleep -seconds 1
}
}

我要隐藏的进度条是这样的:

http://i.stack.imgur.com/LlUVQ.png

我真的需要帮助,希望有人能帮我解决我的问题,谢谢

【问题讨论】:

    标签: powershell compression zipfile


    【解决方案1】:

    进度条来自CopyHere 方法,而不是来自PowerShell,因此设置$ProgressPreference = 'SilentlyContinue' 无济于事。从理论上讲,您可以将值为 4 的第二个参数传递给函数以抑制该对话框:

    $zipPackage.CopyHere($file.FullName, 4)
    

    但如文档所述,该参数对于 zip 文件会被忽略:

    注意在某些情况下,例如压缩 (.zip) 文件,某些选项标志可能会被设计忽略。


    如果您有 PowerShell 3 和 .Net framework 4.5,您可以使用 ZipFile 类而不是 Shell.Application 对象:

    Add-Type -Assembly System.IO.Compression.FileSystem
    $cl = [IO.Compression.CompressionLevel]::Optimal
    [IO.Compression.ZipFile]::CreateFromDirectory($srcdir, $zipFile, $cl, $false)
    

    否则,请尝试this question 的答案中列出的其他方法之一。

    【讨论】:

      【解决方案2】:

      Ansgar 是对的,但仍有办法隐藏它。

      您可以通过搜索所有标题为“正在压缩...”的窗口(@98​​7654321@ 循环)并隐藏它们来隐藏它。
      但这还不够,因为如果文件太小,您将没有窗口,while 循环将永远运行。
      这个问题是因为CopyHere是异步函数。
      为了克服这个问题,我还添加了一个检查以查看文件何时未锁定,然后删除所有作业。

      Function Test-FileLock {
            param (
              [parameter(Mandatory=$true)][string]$Path
            )
      
            $oFile = New-Object System.IO.FileInfo $Path
      
            if ((Test-Path -Path $Path) -eq $false) {
              return $false
            }
      
            try {
              $oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
      
              if ($oStream) {
                $oStream.Close()
              }
              $false
            } catch {
              # file is locked by a process.
              return $true
            }
      }
      
      
      $scriptBlock = {
          $FORCEMINIMIZE_STATE   = 11
          $HIDE_STATE            = 0
          $MAXIMIZE_STATE        = 3
          $MINIMIZE_STATE        = 6
          $RESTORE_STATE         = 9
          $SHOW_STATE            = 5
          $SHOWDEFAULT_STATE     = 10
          $SHOWMAXIMIZED_STATE   = 3
          $SHOWMINIMIZED_STATE   = 2
          $SHOWMINNOACTIVE_STATE = 7
          $SHOWNA_STATE          = 8
          $SHOWNOACTIVATE_STATE  = 4
          $SHOWNORMAL_STATE      = 1
      
      $member = @"
      [DllImport("user32.dll")]
      public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
      "@
      
          $script:showWindowAsync = Add-Type -memberDefinition $member -name "Win32ShowWindowAsync" -namespace Win32Functions -passThru
      
          $continue = $true
          do{
              $processes = Get-Process | where {$_.MainWindowTitle -eq "Compressing..." }
              foreach($proc in $processes)
              {
                  if(![string]::IsNullOrEmpty($proc))
                  {
                      $null = $showWindowAsync::ShowWindowAsync($proc.MainWindowHandle, $HIDE_STATE)
                      $continue = $false
                  }
              }
          }while($continue)
      }
      
      $job = Start-Job -ScriptBlock $scriptBlock
      
      #############################################
      # Your CopyHere function                    #
       $zipPackage.CopyHere($file.FullName)
      ###########################################
      
      # Checking when the compression will finish
      do{
          Start-Sleep 2
      }while((Test-FileLock $zipPackage))
      
      Stop-Job $job
      Get-Job | Remove-Job
      

      【讨论】:

        猜你喜欢
        • 2011-06-10
        • 1970-01-01
        • 2011-10-02
        • 2020-09-04
        • 1970-01-01
        • 2016-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多