脚本结果>文本文件
我有以下两个脚本。我希望创建一个文本文件,其结果位于
a) 原始执行所在的同一目录或
b) 在 c: 目录
1)
$sum = 0
foreach ($i in dir -filter *.log -Rec)
{
$sum += (gc $i.fullname | select -Skip 5 | ConvertFrom-Csv -Delimiter "`t" | ? {$_.Details -match "^(\d+)"} |% {$matches[1]} | Measure-Object -Sum).Sum
}
$sum
[Console]::Write("Press any key to continue . . . ")
[Console]::ReadKey()
2)中
foreach ($i in dir -filter *.log -Rec)
{
$cameras = gc $i.fullname | select -Skip 5 | ConvertFrom-Csv -Delimiter "`t" | group "Entity "
$cameras | select Name, @{n="Total";e={ ($_.group | ? {$_.Details -match "^(\d+)"} |% {$matches [1]} | Measure-Object -Sum).Sum}} | ? {$_.Total -gt 0}
}
[Console]::Write("Press any key to continue . . . ")
[Console]::ReadKey()
I have the following two scripts. I would like to have a Text file created with the results in either
a)the same directory were the orginal execution took place or
b)On the c: directory
1)
$sum = 0
foreach ($i in dir -filter *.log -Rec)
{
$sum += (gc $i.fullname | select -Skip 5 | ConvertFrom-Csv -Delimiter "`t" | ? {$_.Details -match "^(\d+)"} |% {$matches[1]} | Measure-Object -Sum).Sum
}
$sum
[Console]::Write("Press any key to continue . . . ")
[Console]::ReadKey()
2)
foreach ($i in dir -filter *.log -Rec)
{
$cameras = gc $i.fullname | select -Skip 5 | ConvertFrom-Csv -Delimiter "`t" | group "Entity "
$cameras | select Name, @{n="Total";e={ ($_.group | ? {$_.Details -match "^(\d+)"} |% {$matches [1]} | Measure-Object -Sum).Sum}} | ? {$_.Total -gt 0}
}
[Console]::Write("Press any key to continue . . . ")
[Console]::ReadKey()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Chris Browne 说的是你可以使用 CmdLet Out-File
我添加的东西是为了获取脚本执行目录:
The thing @Chris Browne says is that you can use CmdLet Out-File
The thing I add is for getting script exécution directory :
我相信您正在寻找的 cmdlet 称为 Out-File。
http://technet.microsoft.com/en-us/library/ee176924.aspx
I believe the cmdlet you're looking for is called Out-File.
http://technet.microsoft.com/en-us/library/ee176924.aspx
通过管道 $sum 或 $cameras 到 Out-File cmdlet。要在当前目录中创建文件,只需传递文件名。要在 C: 驱动器中创建它,请传递完整路径:
顺便说一句,您可以使用 Write-Host cmdlet(而不是 [Console]::Write)将消息写入控制台,然后使用 Read 等待按键-Host cmdlet(而不是 [Console]::ReadKey)。
Pipe $sum or $cameras to the Out-File cmdlet. To create the file in the current directory pass just the name of the file. To create it in the C: drive pass the full path:
By the way, you can write messages to the console by using the Write-Host cmdlet (instead of [Console]::Write, and wait for a key press with the Read-Host cmdlet (instead [Console]::ReadKey).