在同一脚本中分离进程的逻辑

时间:2019-04-23 17:21:53

标签: powershell

场景: 我正在压缩文件并将其移动到另一台服务器。
我要复制的文件在文本文件中以“ file =“开头的行中定义 可以通过两种方式格式化“ file =”行 1:文件包含unc路径 我抓住UNC路径将其压缩并复制 2:该文件包含UNC格式的文件链接,该链接链接到“ * .lst文件” * .lst文件又链接到我捕获,压缩和复制的一堆UNC路径

我收到一个日志文件,其中显示了我正在用来检索数据路径的所有文本文件。照原样,我正在获取该日志文件,根据第一个条目创建一个文件夹,将所有txt文件复制到此“处理文件夹”。从那里,我尝试打开每个txt文件,并查找* .lst文件的存在,如果* .lst存在,则继续执行LST进程,如果LST不存在,则运行“ else”,这只是定期抓取unc路径过程。

我似乎正在追寻将这两个过程都放在同一脚本中的最佳方法。想知道stackoverflow是否可以帮助我思考实现此目标的最佳方法。这是我到目前为止的内容:

###DEFINE GLOBAL_VARIABLES
$global:ordrepopath = "C:\test_environment\ORD_REPO"
$env:path = "c:\program files\7-zip"
$global:datestr = (Get-Date).ToString("MMddyyyyhhmmss")
$global:ordlogpath = "C:\test_environment\ORD_REPO\ORD_LOGS\"
$global:ordlogcheck = "C:\test_environment\ORD_REPO\ORD_LOGS\*.log"
$global:ordlogstagingpath = "C:\test_environment\ORD_REPO\ORD_STAGING"
$global:ordlogarchivepath = "C:\test_environment\ORD_REPO\ORD_LOG_ARCHIVE"
$global:ordprocessingpath = "C:\test_environment\ORD_REPO\ORD_PROCESSING"
$global:copypath = "C:\test_environment_2\share\STAGING\PRE_STAGING"
$global:ordlogdestpath = "C:\test_environment_2\Share\Staging\Pre_staging\processed_logs"

###DEFINE LOG FILE
$script:scriptlogfile = "C:\test_environment\ORD_REPO\SCRIPT_LOGS\ORD_PROCESS_LOG_$(get-date -format `"yyyyMMdd_hhmmss`").log"
Start-Transcript -Path $script:scriptlogfile -NoClobber

 if (!(Test-Path -Path $global:ordlogcheck)) 
  {
   Write-Host "NO FILES TO PROCESS"
  }
 else
  {
  ### CREATE ARCHIVE DIRECTORY
  New-Item -Path "C:\test_environment\ORD_REPO\Archive\$global:datestr" -ItemType Directory
  $script:archivepath = "C:\test_environment\ORD_REPO\Archive\$global:datestr"
  $script:ordlogfiles = Get-ChildItem -Path $global:ordlogpath -File
   ForEach ($script:ordlogfile in $script:ordlogfiles)
   {
    Set-Location $global:ordlogpath
    $script:ordlogimport = (Import-Csv $script:ordlogfile.FullName).file |sort
    $script:ordprocfoldername = ($script:ordlogimport |Select-Object -First 1)
    New-Item -Path "C:\test_environment\ORD_REPO\ORD_PROCESSING\$script:ordprocfoldername" -ItemType Directory
    $script:ordprocfolder = "C:\test_environment\ORD_REPO\ORD_PROCESSING\$script:ordprocfoldername"
    Set-Location $global:ordrepopath
    $script:ordlogimport |ForEach-Object {move-item $_ $script:ordprocfolder}
    Set-Location $global:ordlogpath
    copy-item $script:ordlogfile $script:archivepath
    Set-Location $script:ordprocfolder
    $script:ordfiles = Get-ChildItem $script:ordprocfolder -Include *.ord,*.nwp | ForEach-Object { $_.Name }
    FOREACH ($script:ordfile in $script:ordfiles)
     {
      $script:ordlogcount = ($script:ordlogimport).count
      $script:ordcount = (get-childitem $script:ordprocfolder).count
      if ($script:ordlogcount -ne $script:ordcount)
       {
        WRITE-HOST "MISSING ORD FILE"
       }
      else
       {
        $script:lstfilepath = ((Get-Content -path $script:ordfile) | Select-String -pattern "^file\s*=\s*(\\\\.*.lst)").matches.groups[1].value
        if (-not ([string]::IsNullOrEmpty($script:lstfilepath)))
         {
          ###LST PROCESS
          Write-Host "LST FILE PRESENT"
          $script:lstpayload = Get-Content $script:lstfilepath |ForEach-Object { ($_ -split '"')[-2] }
           FOREACH ($script:lstfile in $script:lstpayload)
            {
              $script:lstzipshortname = (-join ((48..57) + (97..122) |get-random -count 11 |% {[char]$_}))
              $script:lstzipname = $script:lstzipshortname + ".zip"
              7z a -spf  $script:lstzipname $script:lstfile
            }
         else   
            { 
             ###REGULAR PROCESS
             $script:filepath = ((Get-Content -path $script:ordfile) | Select-String -pattern "^file\s*=\s*(\\\\.*\\)").matches.groups[1].value
             $script:zipshortname = $script:ordfile
             $script:zipname = $script:zipshortname + ".zip"
             7z a -spf $script:zipname $script:filepath
      }
     }
    copy-item "C:\test_environment\ORD_REPO\keepalive.donotremove" $global:copypath
    copy-item "C:\test_environment\ORD_REPO\keepalive.donotremove" $global:copypath
    Set-Location $script:ordprocfolder
    copy-item $script:ordprocfolder $global_copypath -Recurse
    Get-ChildItem -Path $script:ordprocfolder -Filter *.log |% {copy-item $_ $global:ordlogdestpath}
    Set-Location $global_ordprocessingpath
    Remove-Item -Recurse $script:ordprocfolder
            }


        }


       }    
     }

0 个答案:

没有答案