首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将powershell脚本转换为Runspace

将powershell脚本转换为Runspace
EN

Stack Overflow用户
提问于 2016-04-01 03:35:50
回答 2查看 813关注 0票数 0

我写了一个快速脚本来查找一个用户列表(TEMP.txt)中的用户百分比,这些用户也在另一个用户列表(TEMP2.txt)中。它在一段时间内工作得很好,直到我的用户列表超过了几个100,000左右……太慢了。我想把它转换成运行空间来加速它,但我失败得很痛苦。原始脚本为:

代码语言:javascript
复制
$USERLIST1 = gc .\TEMP.txt
$i = 0

ForEach ($User in $USERLIST1){
If (gc .\TEMP2.txt |Select-String $User -quiet){
$i = $i + 1
}
}
$Count = gc .\TEMP2.txt | Measure-object -Line

$decimal = $i / $count.lines

$percent = $decimal * 100

Write-Host "$percent %"

对不起,我在powershell还是个新手。

EN

回答 2

Stack Overflow用户

发布于 2016-04-01 04:14:59

代码语言:javascript
复制
$Runspace = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($Host)

$Runspace.ApartmentState = 'STA'
$Runspace.ThreadOptions = 'ReuseThread'
$Runspace.Open()

#Add the Form object to the Runspace environment
$Runspace.SessionStateProxy.SetVariable('Form', $Form)

#Create a new PowerShell object (a Thread)
$PowerShellRunspace = [System.Management.Automation.PowerShell]::Create()

#Initializes the PowerShell object with the runspace
$PowerShellRunspace.Runspace = $Runspace

#Add the scriptblock which should run inside the runspace
$PowerShellRunspace.AddScript({
    [System.Windows.Forms.Application]::Run($Form)
})

#Open and run the runspace asynchronously
$AsyncResult = $PowerShellRunspace.BeginInvoke()

#End the pipeline of the PowerShell object
$PowerShellRunspace.EndInvoke($AsyncResult)

#Close the runspace
$Runspace.Close()

#Remove the PowerShell object and its resources
$PowerShellRunspace.Dispose()
票数 0
EN

Stack Overflow用户

发布于 2016-04-01 04:41:14

除了运行空间的概念,下一个脚本可以运行得更快一些:

代码语言:javascript
复制
$USERLIST1 = gc .\TEMP.txt
$USERLIST2 = gc .\TEMP2.txt

$i = 0

ForEach ($User in $USERLIST1) {
    if ($USERLIST2.Contains($User)) {
        $i += 1
    }
}

$Count = $USERLIST2.Count

$decimal = $i / $count
$percent = $decimal * 100
Write-Host "$percent %"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36342190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档