我只是跳进了Runspace的事情,因为我需要我的模块是多线程的,我读到Runspaces是最好的选择,因为它是最快的。我不知道我现在在做什么。
我的目标是从FTP服务器下载数据。今天+ 3天。我正在使用这个PS FTP模块。
没有多线程就可以正常工作。但是在一天内有3400个数据,我不能等待一个线程结束另一个线程开始。我想要4个线程同时下载每天的数据。理想情况下......将有4个线程连接到FTP服务器,然后我会检查我是否已经有数据,因为我不想重新下载我已经拥有的数据,然后让1个ftp连接线程说10个线程。 / p>
(1个FTP连接线程x 10个下载线程)x4
使用我选择的工具是否可以实现这一目标?
这是我到目前为止所拥有的。 什么有效:
什么行不通:
$t = 3
$RSDefault = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$RSPool = [RunspaceFactory]::CreateRunspacePool(1, $t, $RSDefault, $Host)
$RSPool.ApartmentState = "MTA"
$RSPool.Open()
$ScriptBlock = {
Param($k,[String]$DataOutput = "\\domain.local\outputfolder")
Import-Module PSFTP
$PWD_Path = "C:\Utils\scripts"
$PWD_File = "$PWD_Path\OSWD_password.txt"
$Password = Get-Content $PWD_File -ErrorAction SilentlyContinue | ConvertTo-SecureString
Start-Sleep -Seconds 1
$Creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList user, $Password
Set-FTPConnection -Credentials $Creds -Server ftp.server.com -UsePassive -Session "OWSD-$k" -EnableSsl -ignoreCert | Out-File C:\TEMP\ftp.txt
$Data = Get-FTPChildItem -Session "OSWD-$k" -Path /wpp/$k -Filter *.csv | Select-Object -ExpandProperty Name
ForEach ($FTPItem in $Data) {
$DataExists = Test-Path -Path $DataOutput\$k\$FTPItem
If (!$DataExists) {
Get-FTPItem -Session "OSWD-$k" -Path /wpp/$k/$FTPItem -LocalPath $DataOutput\$k
}
}
} #end scriptblock
$threads = @()
$StopWatch = [System.Diagnostics.Stopwatch]::StartNew()
for ($d=0; $d -le $t; $d++) {
$kp = (Get-Date (Get-Date).AddDays(-$d) -UFormat %Y%m%d)
foreach ($k in $kp) {
[String]$DataOutput = "\\domain.local\outputfolder"
If (!(Test-Path "$DataOutput\$k")) {
New-Item -Path $DataOutput -Name $k -Value $k -ItemType Directory | Out-Null
}
$RunspaceObject = [PSCustomObject] @{
RunSpace = [PowerShell]::Create()
Invoker = $null
}
$RunspaceObject.Runspace.RunSpacePool = $RunspacePool
$RunspaceObject.Runspace.AddScript($ScriptBlock) | Out-Null
$RunspaceObject.Runspace.AddArgument($k) | Out-Null
$RunspaceObject.Invoker = $RunspaceObject.Runspace.BeginInvoke()
$threads += $RunspaceObject
$elapsed = $StopWatch.Elapsed
Write-Output "Finished creating runspace for $k. Elapsed time: $elapsed"
}
$elapsed = $StopWatch.Elapsed
Write-Output "Finished creating all runspaces. Elapsed time: $elapsed"
}
While ($threads.Invoker.IsCompleted -contains $false) {}
$elapsed = $StopWatch.Elapsed
Write-Output "All runspaces completed. Elapsed time: $elapsed"
<#$threadResults = @()
Foreach ($thread in $threads) {
$threadResults += $thread.Runspace.Endinvoke($thread.Invoker)
$thread.Runspace.Dispose()
}#>
[更新] 好吧,似乎“Set-FTPConnection -Credentials $ Creds -Server ftp.server.com -UsePassive -Session”中的会话名称OWSD- $ k“-EnableSsl - ignoreCert“在设置后丢失。我删除了Out-File,它现在告诉我下一个命令无法执行,因为没有这样的Session。