Как l oop ps1 подать переменную из csv - PullRequest
0 голосов
/ 14 февраля 2020

Нужна помощь от гуру PS. У меня есть ps1, который я использую для запуска команды ICACLS для принудительного наследования вложенных папок и файлов. У меня есть много из них, мне нужно запустить скрипт, и я хотел бы знать, как просто передать переменную ps1 с помощью csv, который я скомпилирую, чтобы он мог перебирать список. Прямо сейчас у меня есть подсказка для пути UN C с каждым прогоном. Любая помощь приветствуется! Вот мой сценарий:



  $Path = $(
      Add-Type -AssemblyName Microsoft.VisualBasic
      [Microsoft.VisualBasic.Interaction]::InputBox('Enter in the UNC path')
     )

  #Start the job that will reset permissions for each file, don't even start if there are no direct sub-files
  $SubFiles = Get-ChildItem $Path -File
  If ($SubFiles)  {
    $Job = Start-Job -ScriptBlock {$args | %{icacls $_.FullName /Reset /C}} -ArgumentList $SubFiles
  }

  #Now go through each $Path's direct folder (if there's any) and start a process to reset the permissions, for each folder.
  $Processes = @()
  $SubFolders = Get-ChildItem $Path -Directory
  If ($SubFolders)  {
    Foreach ($SubFolder in $SubFolders)  {
      #Start a process rather than a job, icacls should take way less memory than Powershell+icacls
      $Processes += Start-Process icacls -WindowStyle Hidden -ArgumentList """$($SubFolder.FullName)"" /Reset /T /C" -PassThru
    }
  }

  #Now that all processes/jobs have been started, let's wait for them (first check if there was any subfile/subfolder)
  #Wait for $Job
  If ($SubFiles)  {
    Wait-Job $Job -ErrorAction SilentlyContinue | Out-Null
    Remove-Job $Job
  }
  #Wait for all the processes to end, if there's any still active
  If ($SubFolders)  {
    Wait-Process -Id $Processes.Id -ErrorAction SilentlyContinue
  }

  Write-Host "The script has completed resetting permissions under $($Path)."
}
Catch  {
  $ErrorMessage = $_.Exception.Message
  Throw "There was an error during the script: $($ErrorMessage)"
}
...