Увеличивайте число один раз при каждом запуске функции в Powershell 5.0 - PullRequest
0 голосов
/ 12 марта 2020

Я просто ищу способ увеличивать переменную $ Num каждый раз, когда запускается скрипт ниже. Таким образом, сценарий начинается с использования 168, выполняется, затем увеличивается до 169, выполняется снова и т. Д. c. до 999. Спасибо!

$Path = "H:\ClientFiles\CHS\Processed\"
$Num = 168
$ZipFile = "FileGroup0000000$Num.zip"
$File = "*$Num*.83*"
$n = dir -Path $Path$File | Measure

        if($n.count -gt 0){
        Remove-Item $Path$ZipFile
        Compress-Archive -Path $Path$File -DestinationPath $Path
        Rename-Item $Path'.zip' $Path'FileGroup0000000'$Num'.zip'          
        Remove-Item $Path$File            

        }    
        else {
            Write-Output "No Files to Move for FileGroup$File"
        }

Ответы [ 2 ]

0 голосов
/ 12 марта 2020

Не уверен, что вы хотите go этот маршрут. У вас может быть модуль, который поддерживает переменную скрипта:

# file.psm1

$script:num = 1

function myfunc {
  $script:num
  $script:num++
}
import-module .\file.psm1

myfunc
1

myfunc
2

myfunc
3

remove-module file

Я отвечаю на заголовок поста. Модуль для вас будет выглядеть так, но подсказка powershell должна быть всегда открыта.

$num = $script:Num = 168

function myfunc { 
  $Path = ".\"
  $ZipFile = "FileGroup0000000$Num.zip"
  $File = "*$Num*.83*"
  $n = dir -Path $Path$File | Measure

  if($n.count -gt 0){
    Remove-Item $Path$ZipFile
    Compress-Archive -Path $Path$File -DestinationPath $Path
    Rename-Item $Path'.zip' $Path'FileGroup0000000'$Num'.zip'          
    Remove-Item $Path$File            
  } else {
    Write-Output "No Files to Move for FileGroup$File"
  }

  $script:num++
}
0 голосов
/ 12 марта 2020

Это, по сути, то, для чего for циклы!

$Path = "H:\ClientFiles\CHS\Processed\"
for($Num = 168; $Num -le 999; $Num++){
    $ZipFile = "FileGroup0000000$Num.zip"
    $File = "*$Num*.83*"
    $n = dir -Path $Path$File | Measure

    if ($n.count -gt 0) {
        Remove-Item $Path$ZipFile
        Compress-Archive -Path $Path$File -DestinationPath $Path
        Rename-Item "${Path}.zip" "${Path}FileGroup0000000${Num}.zip"
        Remove-Item $Path$File

    }
    else {
        Write-Output "No Files to Move for FileGroup$File"
    }
}

Разбивка нашей for() l oop декларации:

for($Num = 168; $Num -le 999; $Num++){
 #       ^            ^          ^
 #       |            |          | Increase by one every time
 #       |            | Keep running as long as $num is less than or equal to 999
 #       | Start with an initial value of 168
}
...