首页 > 解决方案 > Powershell 函数中的多个参数。减少重复代码片段

问题描述

我在为我的 PS 脚本编写函数时遇到问题。我见过很多标题非常相似的主题,但在我的案例中却无法应用它们。

我有一个简单的脚本,它从公共来源收集日志并将它们放在单独的文件夹/档案中。开始的时候比较短,但现在在存储更改后,它变得有点傻。

有人可以帮助我了解如何将此代码简化为一个功能吗?它应该每天安静地工作,无需任何输入。

BR 并提前致谢

$Date = (get-date).AddDays(-1).ToString("yyyy-MM-dd")

#$Date = "2021-02-28"

$PathFailedEU = 'P:\IntegrationFileShare\failed\europe'
$PathLogsEU = 'P:\IntegrationFileShare\logs\europe'

$PathFailedAPAC = 'P:\IntegrationFileShare\failed\apac'
$PathLogsAPAC = 'P:\IntegrationFileShare\logs\apac'

$PathFailedLATAM = 'P:\IntegrationFileShare\failed\latam'
$PathLogsLATAM = 'P:\IntegrationFileShare\logs\latam'

#$Dest = 'C:\tmp\SCRPT\'
$DestEU = 'C:\tmp\SCRPT\europe\'
$DestAPAC = 'C:\tmp\SCRPT\apac\'
$DestLATAM = 'C:\tmp\SCRPT\latam\'

$compress_EU_log = @{
Path = $DestEU + '*.log' 
CompressionLevel = "Fastest"
DestinationPath = ($DestEU + 'Logs_EU_' + ($Date) + '.zip')
}
$compress_EU_failed = @{
Path = $DestEU + '*.log'
CompressionLevel = "Fastest"
DestinationPath = ($DestEU + 'Failed_EU_' + ($Date) + '.zip')
}
$compress_APAC_log = @{
Path = $DestAPAC + '*.log'
CompressionLevel = "Fastest"
DestinationPath = ($DestAPAC + 'Logs_APAC_' + ($Date) + '.zip')
}
$compress_APAC_failed = @{
Path = $DestAPAC + '*.log'
CompressionLevel = "Fastest"
DestinationPath = ($DestAPAC + 'Failed_APAC_' + ($Date) + '.zip')
}
$compress_LATAM_log = @{
Path = $DestLATAM + '*.log'
CompressionLevel = "Fastest"
DestinationPath = ($DestLATAM + 'Logs_LATAM_' + ($Date) + '.zip')
}
$compress_LATAM_failed = @{
Path = $DestLATAM + '*.log'
CompressionLevel = "Fastest"
DestinationPath = ($DestLATAM + 'Failed_LATAM_' + ($Date) + '.zip')
}

gci -path $PathLogsEU -filter *$Date* -recurse | %{cp $_.pspath -destination $DestEU}
if(Test-Path $compress_EU_log.Path){Compress-Archive @compress_EU_log}
Remove-Item -Path $DestEU* -Include *.log

gci -path $PathFailedEU -filter *$Date* -recurse | %{cp $_.pspath -destination $DestEU}
if(Test-Path $compress_EU_failed.Path){Compress-Archive @compress_EU_failed}
Remove-Item -Path $DestEU* -Include *.log

gci -path $PathLogsAPAC -filter *$Date* -recurse | %{cp $_.pspath -destination $DestAPAC}
if(Test-Path $compress_APAC_log.Path){Compress-Archive @compress_APAC_log}
Remove-Item -Path $DestAPAC* -Include *.log

gci -path $PathFailedAPAC -filter *$Date* -recurse | %{cp $_.pspath -destination $DestAPAC}
if(Test-Path $compress_APAC_failed){Compress-Archive @compress_APAC_failed}
Remove-Item -Path $DestAPAC* -Include *.log

gci -path $PathLogsLATAM -filter *$Date* -recurse | %{cp $_.pspath -destination $DestLATAM}
if(Test-Path $compress_LATAM_log.Path){Compress-Archive @compress_LATAM_log}
Remove-Item -Path $DestLATAM* -Include *.log

gci -path $PathFailedLATAM -filter *$Date* -recurse | %{cp $_.pspath -destination $DestLATAM}
if(Test-Path $compress_LATAM_failed.Path){Compress-Archive @compress_LATAM_failed}
Remove-Item -Path $DestLATAM* -Include *.log

标签: powershell

解决方案


塞尔吉,

基本上,您想查找正在重复的事物以及它们之间的差异。然后将差异转换为函数的参数,然后使用这些参数替换语句。

在您的情况下,差异在于区域,因此您将其设置为参数,然后消除代码中对该区域的所有引用,并在必要时将其替换为参数 $Area。

Function Process-Logs {

  Param (
    [Parameter(Mandatory=$True)]
     [string] $Area
  )

  $PathFailed  = "P:\IntegrationFileShare\failed\$Area"
  $PathLogs    = "P:\IntegrationFileShare\logs\$Area"
  
  #$Dest = "C:\tmp\SCRPT\"
  $Dest = "C:\tmp\SCRPT\$Area\"
  
  $compress_log = @{
    Path             = $Dest + "*.log" 
    CompressionLevel = "Fastest"
    DestinationPath  = ($Dest + "Logs_$Area_" + ($Date) + ".zip")
  }
  $compress_failed = @{
    Path             = $Dest + "*.log"
    CompressionLevel = "Fastest"
    DestinationPath  = ($Dest + "Failed_$Area_" + ($Date) + ".zip")
  }
  
  gci -path $PathLogs -filter *$Date* -recurse | 
    %{cp $_.pspath -destination $Dest}
  if(Test-Path $compress_log.Path) {Compress-Archive @compress_log}
  Remove-Item -Path $Dest* -Include *.log
  
  gci -path $PathFailed -filter *$Date* -recurse | 
    %{cp $_.pspath -destination $Dest}
  if(Test-Path $compress_failed.Path) {Compress-Archive @compress_failed}
  Remove-Item -Path $Dest* -Include *.log

} #End Process-Logs

现在您确实遇到了一个问题,因为您与“欧洲”不一致,您在某些命令中使用它,然后在其他命令中使用“欧盟”。您需要对功能的良好运行进行标准化。使用“欧洲”或“欧盟”,但要保持一致。

你会这样调用函数:

$Date = (get-date).AddDays(-1).ToString("yyyy-MM-dd")
Process-Logs -Area "Europe"
Process-Logs -Area "LATAM"
Process-Logs -Area "APAC"

当然,您可以将这些区域放在一个数组中,然后使用 for each 循环来处理它们。

另外,我建议您将代码分开以使其更易于阅读。请记住,您可以在后面总是有更多代码(+、-、{、(、|、)和运算符(-and、-not、-like、-eq 等)的任何字符上换行。

高温高压


推荐阅读