我有PowerShell 4.0和Windows Server 2012 R2。我想将ZIP文件(84个文件)提取到一个单独的文件夹中。我创建了一个工作正常的脚本,但该脚本是硬编码的,代码太长,无法解决。我想通过参数化脚本以减少代码行数来减少代码。谁能帮我这个?这是我的脚本:
Function Get-ShortName {
begin {
$fso = New-Object -ComObject Scripting.FileSystemObject }
process {
If ($_.psiscontainer) {
$fso.getfolder($_.fullname).ShortName }
ELSE {
$fso.getfile($_.fullname).ShortName }
} }
#Formatting today's date
$TodayDate = (Get-Date).ToString('DTyyMMdd')
#Formatting the first day of the month
$Year = (get-date).Year
$Month = (get-date).Month
$FirstDayMth = Get-Date -Year $Year -Month $Month -Day 1
$FirstDayOfMonth = $FirstDayMth.ToString('DTyyMMdd')
#Download Nightly and Monthly files
Get-ChildItem -Path "C:\users\in\FileZIP*_nightly.$TodayDate" -Recurse | Copy-Item -Destination "C:\data_files\Provisioning_Files" -Force -Verbose
Get-ChildItem -Path "C:\users\in\FileZIP*_monthly.$FirstDayOfMonth" -Recurse | Copy-Item -Destination "C:\data_files\Provisioning_Files" -Force -Verbose
#Unzip files
Add-Type --AssemblyName System.IO.Compression.FileSystem
function Unzip {
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath) }
#Check if files exist
$checkprovfiles = Get-ChildItem -Path C:\data_files\Provisioning_Files -File
if ($checkprovfiles.Count -eq 84 ) {
echo "Files have been downloaded SUCCESSFULLY. Now we need to Extract them"
Get files shortname
$files = Get-ChildItem "C:\data_files\Provisioning_Files\*.*" | Get-Shortname
Foreach ($file in $files) {
if ($file -eq "FileZIP1_nightly.$TodayDate") {
echo 'Start extracting FileZIP1 nightly files'
Unzip ""C:\data_files\Provisioning_Files\FileZIP1_nightly.$TodayDate" "C:\data_files\Extracted_files\FILEZIP1_Nightly_Monthly_Extracted\"
echo 'Finish extracting FileZIP1 nightly files' }
if ($file -eq "FileZIP1_monthly.$FirstDayOfMonth") {
echo 'Start extracting FileZIP1 monthly files'
Unzip ""C:\data_files\Provisioning_Files\FileZIP1_monthly.$FirstDayOfMonth") "C:\data_files\Extracted_files\FILEZIP1_Nightly_Monthly_Extracted\"
echo 'Finish extracting FileZIP1 monthly files'
if ($file -eq "FileZIP2_nightly.$TodayDate") {
echo 'Start extracting FileZIP2 nightly files'
Unzip "C:\data_files\Provisioning_Files\FileZIP2_nightly.$TodayDate" "C:\data_files\Extracted_files\FILEZIP2_Nightly_Monthly_Extracted\"
echo 'Finish extracting FileZIP2 nightly files'
if ($file -eq "FileZIP2_monthly.$FirstDayOfMonth") {
echo 'Start extracting FileZIP2 monthly files'
Unzip ""C:\data_files\Provisioning_Files\FileZIP2_monthly.$FirstDayOfMonth") "C:\data_files\Extracted_files\FILEZIP2_Nightly_Monthly_Extracted\"
echo 'Finish extracting SFileZIP2 monthly files' }
.
.
.
if ($file -eq "FileZIP84_nightly.$TodayDate") {
echo 'Start extracting FileZIP84 nightly files'
Unzip "C:\data_files\Provisioning_Files\FileZIP84_nightly.$TodayDate" "C:\data_files\Extracted_files\FILEZIP84_Nightly_Monthly_Extracted\"
echo 'Finish extracting FileZIP84 nightly files' }
}
else {
echo "Provisionning files are incomplete or could not be found on ."
EXIT }
我重复了IF语句84次,但我不想这样做。很难进行故障排除。我想减少if语句,但我不知道该怎么做。谁能帮我或者有其他建议来做脚本?
答案 0 :(得分:0)
对于这样一个简单的任务和那些zip文件名,这里显示了很多内容,它们没有zip扩展名,因此在文件系统中不被视为zip文件。当然,那是您的选择。
我建议将日期字符串移动到文件基本名称,并使用常规的zip扩展名(即句点左侧)。它使您轻松执行所需的操作,否则,您需要进行额外的工作来处理非常规扩展名。例如您似乎正在做的额外的文件重命名操作。
任何时候只要您获得超过3-5个if语句,就该利用“ Switch”了。有关该想法,请参见ISE中提供的示例代码片段,或…
Get-Help -Name About_Switch -Examples
Get-Help -Name About_Switch -Full
Get-Help -Name About_Switch -Online
尽管有很多选择,但也许该是时候重新考虑您的方法了。
以下内容未经测试,因为我没有显示的文件类型,请根据需要进行调整。
针对目标并解压缩用例。尝试像这样的基线来获得 你开始了。当然,您需要添加任何其他错误/消息/字符串名称处理。
# Unpack archives from source to destination
Function Expand-ZipFile
{
[CmdletBinding()]
[Alias('ezf')]
<#
Set parameters to allow for passing in the function call
or if a parameter is not passed, prompt the use for it.
#>
Param
(
[String]$SourcePath = (Read-Host -Prompt 'Enter the full path of a root source folder'),
[String]$DestinationPath = (Read-Host -Prompt 'Enter the full path to the root destination folder.')
)
# Initialize the .Net compression namespace
Add-Type -AssemblyName System.IO.Compression.FileSystem
# Get all zipfiles in a source folder.
'Collecting all the zip files from the source folder.'
$ZipFiles = Get-ChildItem -Path $Source -Filter '*.zip'
ForEach ($ZipFile in $ZipFiles)
{
<#
Set the destination name using the destination path and add a sub folder
using the basename of the zip file
#>
'Initializing zip destination path'
$ZipPath = New-Item -Path "$DestinationPath\$($ZipFile.BaseName)" -ItemType Directory -WhatIf
# Extract to zip destination path
"Extacting zip file to the zip destination path names: $ZipPath"
[System.IO.Compression.ZipFile]::ExtractToDirectory($($ZipFile.FullName), $($ZipPath.FullName))
}
}
# .Example use case
Expand-ZipFile -SourcePath 'D:\SourcePath' -DestinationPath 'E:\DestinationPath'
OP的更新
至于……
能否请您给我一些有关如何使用REGEX的示例,或者 使用上面的功能进行订阅。
在函数的外部或内部,它只是在文件中进行管道传递,然后应用字符串提取。
'FILE1ZIP_nightly_yyyyddmm', 'FILE1ZIP_monthly_yyyymmdd' | %{$_.SubString(0,8)}
FILE1ZIP
FILE1ZIP