2016-11-19 1 views
0

Windows Server 2012 R2 (64 비트)를 사용하고 있습니다. PowerShell 버전 4를 사용할 수 있습니다. 파일을 압축 및 압축 해제하려고합니다. Write-Zip 명령을 시도하면 다음 오류가 발생합니다.Powershell에서 파일 압축 및 압축 해제 4

Write-Zip : 'Write-Zip'이라는 단어가 cmdlet, 함수, 스크립트 파일 또는 작동하는 프로그램의 이름으로 인식되지 않습니다. 이름의 철자를 확인하거나 경로가 포함되어 있으면 경로가 올바른지 확인하고 다시 시도하십시오.

문제를 해결하려면 어떻게해야합니까? 서버에 zip/winrar를 설치해야합니까? 또는 zip/unzip 파일을 수행하는 다른 명령이 있습니까?

+0

폴더를 압축해야합니다. 이 폴더에는 많은 하위 폴더와 파일이 있습니다. 누군가가 윈도우즈 커맨드 라인 (cmd)뿐만 아니라 powershell로도 압축 할 수있는 방법을 말해 줄 수 있습니까? – Raji

답변

7

Write-Zip 당신이 그것을 사용하기 전에 별도의 설치가 필요 http://pscx.codeplex.com/의 일부가 될 것으로 보인다 Add-ZipNew-Zip 기능을 참조하십시오. 그냥 폴더에서 우편 아카이브를 만들려면

그러나, 당신은 단지

$source = "c:\temp\source" 
$archive = "c:\temp\archive.zip" 

Add-Type -assembly "system.io.compression.filesystem" 
[io.compression.zipfile]::CreateFromDirectory($source, $archive) 

이는 .NET Framework 클래스 ZipFile에서 CreateFromDirectory 방법을 이용을 실행할 수 있습니다. $source 폴더에있는 파일에서 zip 아카이브를 만들고 $archive 변수에 정의 된대로 아카이브를 만듭니다. 참고 ZipFile 클래스는 .NET 프레임 워크 파워 쉘 버전 4.5

+0

아주 잘 작동합니다. 고마워요. 김 – Raji

0

PowerShell V5 (https://www.microsoft.com/en-us/download/details.aspx?id=50395)로 업그레이드 할 수 있으면 기본적으로 업그레이드 할 수 있습니다. https://richardspowershellblog.wordpress.com/2014/10/25/powershell-5-zip-and-unzip/

PowerShell 버전 4의 경우이 검색 http://www.powershellgallery.com/items?q=zip&x=0&y=0을 사용할 수 있습니다. 이것은 또한 당신이 찾고있는 무엇을 보인다 : https://www.powershellgallery.com/packages/Microsoft.PowerShell.Archive/1.0.1.0

이 모듈을 설치하려면, 다음을 입력해야합니다

install-module -name <module name> 
  • powershellgallery.com 사이트를 업로드 할 수있는 무료입니다. 실행하기 전에 모듈을 확인하고 이해하십시오.

희망이 도움이됩니다. 고마워, 팀.

+0

안녕하세요, 감사합니다.하지만 "Install-Module"명령을 실행하면이 cmd가 인식되지 않는 "Write-Zip"명령과 동일한 오류가 발생합니다. – Raji

+1

안녕하세요, 라지, 죄송합니다. PowerShell V5를 설치하면 poweshellgallery.com에서 설치할 수 있습니다. 또는이 URL에 나열된 MSI 설치 프로그램을 사용하십시오. https://msdn.microsoft.com/en-us/powershell/gallery/readme?f=255&MSPPError=-2147217396 –

-1

PS4에서 작동해야합니다.

[CmdletBinding()] 
Param(
[Parameter(Mandatory=$True)] 
[ValidateScript({Test-Path -Path $_ })] 
[string]$sourceDirectory, 

[Parameter(Mandatory=$True)] 
[ValidateScript({-not(Test-Path -Path $_ -PathType Leaf)})] 
[string]$destinationFile, 

[Parameter(Mandatory=$True)] 
[int]$noOlderThanHours 
) 

function New-Zip 
{ 
    param([string]$zipfilename) 
    set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
    (dir $zipfilename).IsReadOnly = $false 
} 

function Add-Zip 
{ 
    param([string]$zipfilename) 

    if(-not (test-path($zipfilename))) 
    { 
     set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
     (dir $zipfilename).IsReadOnly = $false  

    } 

    $shellApplication = new-object -com shell.application 
    $zipPackage = $shellApplication.NameSpace($zipfilename) 


     foreach($file in $input) 
     { 
      $zipPackage.CopyHere($file.FullName) 
      Start-sleep -milliseconds 500 
     } 
} 

$oldest = (Get-Date) - (New-TimeSpan -Hours $noOlderThanHours) 

$filesToZip = dir $sourceDirectory -Recurse | Where-Object {$_.lastwritetime -gt $oldest} 

Write-Host Going to zip following files 

$filesToZip | foreach {Write-Host $_.FullName} 



$filesToZip| Add-Zip $destinationFile 
1

년에 도입되었다> 내장 함수 = 5

$source = "C:\temp\foldertozip" 

If(Test-path $source) 
{ 
    $destFolder = "$($source)_$(get-date -f yyyyMMdd_HHmm)"   
    Compress-Archive -Path $source -DestinationPath "$($destFolder).zip" 
} 
+0

폴더가 비어 있지 않은지 확인하십시오. –

0

당신은 사용자 정의 PowerShell을 대상 New-Object -ComObject Shell.Application을 사용할 수 있습니다 "아카이브를 압축"과에 플래그로 파일을 복사 압축을 풉니 다.

$filePath = "foo.zip" 
$shell = New-Object -ComObject Shell.Application 
$zipFile = $shell.NameSpace($filePath) 
$destinationFolder = $shell.NameSpace("C:\Program Files\WindowsPowerShell\Modules") 

$copyFlags = 0x00 
$copyFlags += 0x04 # Hide progress dialogs 
$copyFlags += 0x10 # Overwrite existing files 

$destinationFolder.CopyHere($zipFile.Items(), $copyFlags) 

https://github.com/hashicorp/best-practices/blob/master/packer/scripts/windows/install_windows_updates.ps1#L12-L22

이 창 '핵심'판 작동하지 않습니다 신용 소스. 가능한 경우 powershell 5로 업그레이드하고 Expand Archive을 사용하면 훨씬 간단합니다.