Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell脚本未压缩正确的文件_Powershell_Scripting_Zip_Target_7zip - Fatal编程技术网

PowerShell脚本未压缩正确的文件

PowerShell脚本未压缩正确的文件,powershell,scripting,zip,target,7zip,Powershell,Scripting,Zip,Target,7zip,我正在测试我在网上找到的这个脚本。我的问题是,它不是压缩目标文件夹中的文件,而是复制和压缩7-zip程序文件文件夹的内容。这是什么原因造成的?提前感谢使用文件的.FullName属性(PSv3+语法),将文件作为完整路径传递给Zip函数: 问题是,通过情景[1]返回的[System.IO.FileInfo]实例只将其文件名字符串化,这就是您的情况,因此您的Zip函数随后将$toBeZipped值解释为相对于当前位置的值,这是C:\ProgramFiles\7-Zip 也就是说,最好不要在函数中

我正在测试我在网上找到的这个脚本。我的问题是,它不是压缩目标文件夹中的文件,而是复制和压缩7-zip程序文件文件夹的内容。这是什么原因造成的?提前感谢

使用文件的
.FullName
属性(PSv3+语法),将文件作为完整路径
传递给
Zip
函数:


问题是,通过情景[1]返回的
[System.IO.FileInfo]
实例只将其文件名字符串化
,这就是您的情况,因此您的
Zip
函数随后将
$toBeZipped
值解释为相对于当前位置的值,这是
C:\ProgramFiles\7-Zip

也就是说,最好不要在函数中使用
设置位置
,这样在您确实希望传递实际相对路径的情况下,它们会被正确地解释为相对于当前位置:

Zip C:\Users\Admin\Desktop\TEST.zip $Files.FullName

[1]
Get ChildItem
输出字符串化为仅文件名时:

Function Zip {
    Param
    (
        [Parameter(Mandatory)] # make sure a value is passed          
        [string]$zipFile
        ,
        [Parameter(Mandatory)] # make sure a value is passed
        [string[]]$toBeZipped
    )
    # Don't change the location, use & to invoke 7z by its full path.
    $null = & "C:\Program Files\7-Zip\7z.exe" A -tzip $zipFile $toBeZipped
    # You may want to add error handling here.
}
注:

  • 相关cmdlet输出始终字符串化为完整路径,
  • 幸运的是,在PowerShell[Core]v6.1+中,
    Get ChildItem
    总是字符串化到完整路径
因此,以下仅适用于Windows PowerShell中的
Get ChildItem

问题有两方面:

  • 即使是PowerShell的内置cmdlet也不会将文件/目录参数(参数值-而不是通过管道输入)绑定为对象,而是绑定为字符串(更改此行为将在中讨论)

  • 因此,对于健壮的参数传递,您需要确保您的
    Get ChildItem
    输出一致地字符串化到完整路径,而
    Get ChildItem
    并不能保证这一点,而且当只使用名称字符串化时,即使您需要注意它,也很容易忘记

始终传递
.FullName
属性值是最简单的解决方法
或者,对于任何PowerShell提供程序(而不仅仅是文件系统)的可靠操作,
.PSPath

[System.IO.FileInfo]
[System.IO.DirectoryInfo]
通过
Get ChildItem
命令输出的实例仅字符串化为其文件名,当且仅当

  • 如果一个或多个文本目录路径被传递到
    -LiteralPath
    -Path
    (可能作为第一个位置参数)或根本没有路径(以当前位置为目标);也就是说,如果目录的内容被枚举

  • 并且不使用
    -Include
    /
    -Exclude
    参数(是否使用
    -Filter
    ,没有区别)

  • 相比之下,以下各项是否也存在并无区别:

    • -Filter
      (可选作为第二个位置参数,但请注意,将通配符表达式(如
      *.txt
      指定为第一个(可能是唯一的)位置参数将绑定到
      -Path
      参数)
    • -Recurse
      (它本身,但请注意,它通常与
      -Include
      /
      -Exclude
      结合使用)
命令示例:

Function Zip {
    Param
    (
        [Parameter(Mandatory)] # make sure a value is passed          
        [string]$zipFile
        ,
        [Parameter(Mandatory)] # make sure a value is passed
        [string[]]$toBeZipped
    )
    # Don't change the location, use & to invoke 7z by its full path.
    $null = & "C:\Program Files\7-Zip\7z.exe" A -tzip $zipFile $toBeZipped
    # You may want to add error handling here.
}
使用文件的
.FullName
属性(PSv3+语法),将文件作为完整路径传递给
Zip
函数:


问题是,通过情景[1]返回的
[System.IO.FileInfo]
实例只将其文件名字符串化
,这就是您的情况,因此您的
Zip
函数随后将
$toBeZipped
值解释为相对于当前位置的值,这是
C:\ProgramFiles\7-Zip

也就是说,最好不要在函数中使用
设置位置
,这样在您确实希望传递实际相对路径的情况下,它们会被正确地解释为相对于当前位置:

Zip C:\Users\Admin\Desktop\TEST.zip $Files.FullName

[1]
Get ChildItem
输出字符串化为仅文件名时:

Function Zip {
    Param
    (
        [Parameter(Mandatory)] # make sure a value is passed          
        [string]$zipFile
        ,
        [Parameter(Mandatory)] # make sure a value is passed
        [string[]]$toBeZipped
    )
    # Don't change the location, use & to invoke 7z by its full path.
    $null = & "C:\Program Files\7-Zip\7z.exe" A -tzip $zipFile $toBeZipped
    # You may want to add error handling here.
}
注:

  • 相关cmdlet输出始终字符串化为完整路径,
  • 幸运的是,在PowerShell[Core]v6.1+中,
    Get ChildItem
    总是字符串化到完整路径
因此,以下仅适用于Windows PowerShell中的
Get ChildItem

问题有两方面:

  • 即使是PowerShell的内置cmdlet也不会将文件/目录参数(参数值-而不是通过管道输入)绑定为对象,而是绑定为字符串(更改此行为将在中讨论)

  • 因此,对于健壮的参数传递,您需要确保您的
    Get ChildItem
    输出一致地字符串化到完整路径,而
    Get ChildItem
    并不能保证这一点,而且当只使用名称字符串化时,即使您需要注意它,也很容易忘记

始终传递
.FullName
属性值是最简单的解决方法
或者,对于任何PowerShell提供程序(而不仅仅是文件系统)的可靠操作,
.PSPath

[System.IO.FileInfo]
[System.IO.DirectoryInfo]
通过
Get ChildItem
命令输出的实例仅字符串化为其文件名,当且仅当

  • 如果一个或多个文本目录路径被传递到
    
    
    | Select-Object -ExpandProperty FullName
    
    Function Zip{
        Param (
            [string]$zipFile,
            [string[]]$toBeZipped
        )
        & "C:\Program Files\7-Zip\7z.exe" A -tzip $zipFile $toBeZipped | Out-Null
    }
    $Days = "60"
    $LastWrite = (Get-Date).Date.AddDays(-$Days)
    
    $TargetFolder = "$($ENV:USERPROFILE)\Downloads\*"
    
    $Files = Get-Childitem $TargetFolder -Recurse | 
       Where {$_.LastWriteTime -le $LastWrite} | 
         Select-Object -ExpandProperty FullName
    $Files
    Zip "$($ENV:USERPROFILE)\Desktop\TEST.zip" $Files