我编写了这个 PowerShell 脚本,它运行良好,但它在一个文件上运行需要 55 秒,但我需要为 435 个文本文件运行它,每个文件的文本约为 650 kB。
完成 435 个文件需要 7 个多小时!有什么办法可以使这个过程更快?也许把这个过程减半?不知道是不是循环的问题。
$Path = "C:\Users\rfp6fkj\Desktop\Group\*"
#OK so the Get-ChildItem cmdlet in Powershell creates an array to enumerate/loop thru
$Files = Get-ChildItem "$Path.Group"
(Get-Content $Path) -notmatch '^#' | Where { $_.Trim(" `t") } | Set-Content $Path
function GoParseandInsert {
try {
#$connection.Open()
$cmd = $connection.CreateCommand()
$insert_stmt = "INSERT INTO PasswordAge.[dbo].[tblDataParsed]
([Server]
,[Group]
,[UserID])
SELECT I.Server,[Group],F.Val AS TheUserID
FROM PasswordAge.[dbo].[GroupPasswordAge] I
CROSS APPLY PasswordAge.[dbo].ParseDelimValues(CASE WHEN I.UserID = '' THEN 'No Data in Column' ELSE I.UserID END,',') F
WHERE I.UserID <> ''"
$cmd.CommandText = $insert_stmt
#Write-Output $insert_stmt
$cmd.ExecuteNonQuery()
} finally {
if ($connection -and ($connection.State -eq 'Open')) {
$connection.Close()
}
}
}
try {
foreach ($File in $Files) {
$TheFileName = $File.Basename
$StringContent = Get-Content $File
foreach ($Thing in $StringContent) {
$index = $Thing.IndexOf(":")
$GroupName = $Thing.Substring(0, $index)
$cmd = $Connection.CreateCommand()
$BetterThing = $Thing.Split(":")[3]
$insert_stmt = "INSERT INTO [dbo].[GroupPasswordAge]
([Server]
,[Group]
,[UserID])
VALUES
('$thefileName','$GroupName','$BetterThing')" -replace "\s+"," "
$cmd.CommandText = $insert_stmt
Write-Host $insert_stmt -ForegroundColor DarkCyan
$cmd.ExecuteNonQuery()
}
}
} catch {
#if Files dont exist throw a flag
Write-Host "Caught an exception:" -ForegroundColor Red
Write-Host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor DarkRed
Write-Host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
} finally {
Invoke-Sqlcmd -Query "Truncate table PasswordAge..tblDataParsed"
GoParseandInsert
}
if ($connection -and ($connection.state -eq 'Open')) {
$connection.Close()
}
最佳答案
也许也可以尝试多线程方法。每个文本文件一个“线程”?
https://blogs.technet.microsoft.com/uktechnet/2016/06/20/parallel-processing-with-powershell/
关于mysql - PowerShell 脚本需要几个小时才能完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45819351/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只
我有一个在Linux服务器上运行的ruby脚本。它不使用rails或任何东西。它基本上是一个命令行ruby脚本,可以像这样传递参数:./ruby_script.rbarg1arg2如何将参数抽象到配置文件(例如yaml文件或其他文件)中?您能否举例说明如何做到这一点?提前谢谢你。 最佳答案 首先,您可以运行一个写入YAML配置文件的独立脚本:require"yaml"File.write("path_to_yaml_file",[arg1,arg2].to_yaml)然后,在您的应用中阅读它:require"yaml"arg
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Rubysyntaxquestion:Rational(a,b)andRational.new!(a,b)我正在阅读ruby镐书,我对创建有理数的语法感到困惑。Rational(3,4)*Rational(1,2)产生=>3/8为什么Rational不需要new方法(我还注意到例如我可以在没有new方法的情况下创建字符串)?
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
//1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json
在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.
有没有一种简单的方法可以判断ruby脚本是否已经在运行,然后适本地处理它?例如:我有一个名为really_long_script.rb的脚本。我让它每5分钟运行一次。当它运行时,我想看看之前运行的是否还在运行,然后停止第二个脚本的执行。有什么想法吗? 最佳答案 ps是一种非常糟糕的方法,并且可能会出现竞争条件。传统的Unix/Linux方法是将PID写入文件(通常在/var/run中)并在启动时检查该文件是否存在。例如pid文件位于/var/run/myscript.pid然后你会在运行程序之前检查它是否存在。有一些技巧可以避免