简体   繁体   English

如何使用puppet脚本在Windows中解压缩zip文件

[英]How can I unzip a zip file in windows using puppet script

I am new to puppet I want to unzip a zip file kept in puppet windows agent. 我是木偶的新手我想解压缩一个保存在木偶窗口代理中的zip文件。

class ziptest {
exec {"test" :
command =>'unzip Test.zip',
cwd =>  'D:\',
path => 'D:\',
    }
}

While execution I am getting an error : 执行时我收到错误:

COULD NOT FIND COMMAND UNZIP

这是一个用于Windows配置的木偶模块 ,包括解压缩类型。

I wasn't able to unzip an archive with the module counsyl/windows on Win7. 我无法使用Win7上的模块counsyl / windows解压缩存档。 Instead, I have another working solution. 相反,我有另一个有效的解决方案。 Problem ist, windows does not have a built-in command line tool to unzip an archive. 问题是,Windows没有内置的命令行工具来解压缩档案。

Solution: 1. Copy 7za.exe on agent (Download here: www.7-zip.org ) 2. Run exec to unzip file 解决方案:1。在代理上复制7za.exe( 在此处下载:www.7-zip.org )2。运行exec以解压缩文件

How to copy 7za: 如何复制7za:

class sevenzip {

$location = '\\SERVER\path\7za920\7za.exe'
$local_file = 'C:\Windows\System32\7za.exe'

file { $local_file:
    ensure => file,
    source => $location,
    source_permissions => ignore
    }
}

How to run 7za: 如何运行7za:

class install_updates_win {

$location = '\\SERVER\path\PSWindowsUpdate.zip'
$local_file = 'C:\PSWindowsUpdate.zip'
$destination = 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSWindowsUpdate'

file { $local_file:
    ensure => file,
    source => $location,
    source_permissions => ignore,
}

exec { 'extract-pswindowsupdate':
    command   => "C:\Windows\System32\cmd.exe /c C:/Windows/System32/7za.exe e $local_file -o$destination -y",
    cwd       => 'C:/',
    logoutput => true, 
    }
}

I am new to puppet as well and had the same issue. 我也是傀儡的新手,也有同样的问题。 :) It is possible to call powershell from puppet using the ps module: https://forge.puppet.com/puppetlabs/powershell :)可以使用ps模块从puppet调用powershell: https//forge.puppet.com/puppetlabs/powershell

exec { "unzip" :
  command => "Expand-Archive -Path $source -DestinationPath $destination -Force", 
  provider  => powershell,
  logoutput => true,
}    

or without the module just calling the ps.exe 或者没有模块只调用ps.exe

exec { "unzip":
  command   => "C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -command Expand-Archive -Path $source -DestinationPath $destination -Force",
  logoutput => true,
}

I am using expand archive which requires PS 4 so I will have to do some version check as well to be on the safe side. 我正在使用需要PS 4的扩展存档,所以我必须做一些版本检查以确保安全。

I think you can write a class which is using unzip on linux machines and PS on Windows machines. 我想你可以编写一个在Linux机器上使用unzip和在Windows机器上使用PS的类。

define zip::expandarchive($source, $destination) {

  if $::kernel == 'windows' {
    exec { "unzip with ps ($source, $destination)":
        command   => "C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -command Expand-Archive -Path $source -DestinationPath $destination -Force",
        logoutput => true,
    }
  }
  else {
    exec { "unzip ($source, $destination)":
      command   => "unzip -o -d $destination $source",
      logoutput => true,
    }
  }
}

Usage: 用法:

zip::expandarchive { 'ziptest' :
  source => 'D:/Test.zip', 
  destination => 'D:',
}

Using cygwin seems to work as well but you need to select the zip and unzip tools when you install it. 使用cygwin似乎也可以正常工作,但是在安装时需要选择zip和解压缩工具。 Also adding bin folder to the PATH is not working so you need the full path (let me know if you know how to fix this): 另外将bin文件夹添加到PATH不起作用,因此您需要完整路径(如果您知道如何解决此问题,请告诉我):

exec { 'unzip package':
  command   => "c:/cygwin64/bin/unzip.exe -o -d $destination $source",
  logoutput => true,
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何仅使用 Windows 的内置功能 zip 或从脚本中解压缩? - How can you zip or unzip from the script using ONLY Windows' built-in capabilities? 木偶清单将Windows主机上的压缩文件解压缩 - Puppet manifest to unzip a zipped file on a windows host 是否可以在Windows XP上使用.BAT命令解压缩.ZIP文件? - Is it possible to unzip .ZIP file using .BAT command on Windows XP? 在Windows上以编程方式解压缩AES加密的zip文件 - Programatically unzip an AES encrypted zip file on Windows 如何使用 git bash (Windows) 解压缩文件? - How to unzip a file using git bash (Windows)? 如何使用 Windows 批处理脚本迭代和解压缩输入 txt 文件中存在的文件列表 - How to iterate and unzip list of files present in a input txt file using windows batch script 如何在Windows上使用Python提取包含禁止文件名的zip存档? - How can I extract a zip archive that contains a forbidden file name on Windows using Python? 使用.Net 4.5 ZipFile,无法解压缩使用Windows zip压缩的文件(右键单击发送到Compressed) - Using .Net 4.5 ZipFile, cannot unzip a file zipped with Windows zip (right click send to Compressed) 创建批处理脚本以解压缩文件而无需其他zip工具 - creating batch script to unzip a file without additional zip tools Powershell脚本:解压缩文件并在zip文件中执行bat文件 - Powershell Script : Unzip files and execute bat file within the zip files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM