programing

Powershell 스크립트 내에서 7-Zip 실행

minimums 2023. 8. 21. 21:01
반응형

Powershell 스크립트 내에서 7-Zip 실행

7-Zip을 사용하여 Powershell(v2) 스크립트 내의 일부 파일을 백업하려고 합니다.

소유자:

$zipPath = "C:\Program Files\7-Zip\7z.exe"
[Array]$zipArgs = "-mx=9 a", "`"c:\BackupFolder\backup.zip`"", "`"c:\BackupFrom\backMeUp.txt`""

&$zipPath $zipArgs;

하지만 이 기능을 실행하면 다음과 같은 이점이 있습니다.

7-Zip [64] 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18


Error:
Incorrect command line

이 메시지를 화면에 쓰면 다음과 같이 표시됩니다.

C:\Program Files\7-Zip\7z.exe -mx=9 a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"

그래서 저는 7z.exe의 경로에 따옴표를 붙여야 한다고 생각했고, 그 결과 다음과 같은 이점이 있었습니다.

$zipPath = "C:\Program Files\7-Zip\7z.exe"
$zipPath = " `"$zipPath`" "
[Array]$zipArgs = "-mx=9 a", "`"c:\BackupFolder\backup.zip`"", "`"c:\BackupFrom\backMeUp.txt`""

&$zipPath $zipArgs;     

그러나 다음과 같은 오류가 발생합니다.

    The term '"C:\Program Files\7-Zip\7z.exe"' is not recognized as the name of a cmdlet, function, script file
, or operable program. Check the spelling of the name, or if a path was included, verify that the path is c
orrect and try again.
At C:\BackupScript\Backup.ps1:45 char:22
+                     & <<<< `"$zipPath`" $zipArgs;                    
    + CategoryInfo          : ObjectNotFound: ("C:\Program Files\7-Zip\7z.exe":String) [], CommandNotFound 
   Exception
    + FullyQualifiedErrorId : CommandNotFoundException

작성하면 다음과 같은 이점이 있습니다.

"C:\Program Files\7-Zip\7z.exe" -mx=9 a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"

명령 창에 직접 붙여넣을 때 예상대로 작동합니다.저는 한동안 이 문제를 해결하려고 노력했지만, 제가 뭔가를 놓치고 있다고 가정합니다(아마도 꽤 분명할 것입니다).이 일을 실행하기 위해 제가 무엇을 해야 하는지 누가 알 수 있습니까?

이 스크립트를 찾아서 필요에 맞게 수정했습니다.시도해 볼 수 있습니까?

$7zipPath = "$env:ProgramFiles\7-Zip\7z.exe"

if (-not (Test-Path -Path $7zipPath -PathType Leaf)) {
    throw "7 zip file '$7zipPath' not found"
}

Set-Alias Start-SevenZip $7zipPath

$Source = "c:\BackupFrom\backMeUp.txt"
$Target = "c:\BackupFolder\backup.zip"

Start-SevenZip a -mx=9 $Target $Source

7z 명령 앞에 "&" 특수 문자를 붙입니다.예: &7z...

명령 앞에 앰퍼샌드를 붙이기만 하면 됩니다.

& "C:\Program Files\7-Zip\7z.exe" -mx=9 a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"

더 간단한 해결책은 파워셸에서 7-zip을 실행하는 것입니다.cmd:

cmd /c 7za ...

올바르게 조정한 경우: "$Target"의 ""을 잊지 말고 경로에 공백이 있는 c:\programm 파일의 $7zipPath를 사용하지 마십시오.

Set-Alias 7zip $7zipPath

$Source = "c:\BackupFrom\backMeUp.txt"
$Target = "c:\BackupFolder\backup.zip"

7zip a -mx=9 "$Target" "$Source"

또는

7z a "$ArchiveName" -t7z '@listfile.txt'

매개 변수 파일을 사용하여 프로그램 또는 스크립트의 위치를 지정합니다.

-파일 "C:\Program Files\something.exe"

C:\'Program files'\7-Zip\7z.exe a '$archiveFile'  -Path '$dest'

위치:

  • archiveFile보관 파일 이름의 이름입니다.
  • dest대상 폴더.

언급URL : https://stackoverflow.com/questions/25287994/running-7-zip-from-within-a-powershell-script

반응형