PowerShell开始作业运行

时间:2018-08-03 19:18:03

标签: powershell jobs start-job

我需要有关PowerShell中作业的帮助。我在下面编写了脚本,该作业运行了,但实际上什么也没做。我已经尝试了诸如Invoke-Command之类的一些操作,但是PowerShell等待完成后才移至我所要查找的下一行。我需要将此代码与下面的一些其余代码并行运行。

#Arhive Files in Data Folder
$ArchiveDataFiles = {
    function test {
        $ArchiveDataFilesList = Get-ChildItem "$PSScriptRoot\Data\*.pdf" | Select- 
        Object -ExpandProperty Name | Sort-Object -Descending
        for ($c = 0 ; $c -lt $ArchiveDataFilesList.count; $c++) {
            $ArchiveDataFile = $ArchiveDataFilesList.Item($c)

            if ((New-TimeSpan -Start ([DATETIME] ($ArchiveDataFile.ToString().SubString(8,15) -replace "_", " ").Insert(13, ":")) -End ((Get-Date).ToUniversalTime())).TotalHours -gt $ArchiveDataHours) {
                $ArchiveDataFileDepartureDateTime = ($ArchiveDataFile.ToString().SubString(8,15) -replace "_", " ").Insert(13, ":")
                $ArchiveDataFileAirlineCodeFlightNumber = $ArchiveDataFile.ToString().SubString(0,7)
                $ArchiveDataFileDepartureIATA = $ArchiveDataFile.ToString().SubString(24,3)
                $ArchiveDataFileArrivalIATA = $ArchiveDataFile.ToString().SubString(28,3)

                "---Archiving Release---
Flight Date/Time: $ArchiveDataFileDepartureDateTime
Flight: $ArchiveDataFileAirlineCodeFlightNumber
Departure: $ArchiveDataFileDepartureIATA
Arrival: $ArchiveDataFileArrivalIATA
File Name: $ArchiveDataFile
"

                $DateTimeNow = (Get-Date).ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss") + "z"
                if (!(Test-Path "$PSScriptRoot\Log")) {
                    New-Item -Path "$PSScriptRoot\Log" -Type "directory"
                }
                if (!(Test-Path "$PSScriptRoot\Log\DataLog.txt")) {
                    New-Item -Path "$PSScriptRoot\Log" -Name "DataLog.txt" -Type "file"
                }
                Add-Content "$PSScriptRoot\Log\DataLog.txt" -value "---Archive Release--- 
$DateTimeNow
Flight Date/Time: $ArchiveDataFileDepartureDateTime
Flight: $ArchiveDataFileAirlineCodeFlightNumber
Departure: $ArchiveDataFileDepartureIATA
Arrival: $ArchiveDataFileArrivalIATA
File Name: $ArchiveDataFile
"

                if (!(Test-Path "$PSScriptRoot\Data")) {
                    New-Item -Path "$PSScriptRoot\Data" -Type "directory"
                }
                if (!(Test-Path "$PSScriptRoot\Archive")) {
                    New-Item -Path "$PSScriptRoot\Archive" -Type "directory"
                }
                Move-Item -Path "$PSScriptRoot\Data\$ArchiveDataFile" -Destination "$PSScriptRoot\Archive"
            }
        }
    }
}

Start-Job -InitializationScript $ArchiveDataFiles -ScriptBlock {
    Start-ArchiveDataFiles
} -Name ArchiveDataFiles

0 个答案:

没有答案