脚本结果>文本文件

发布于 2024-11-15 01:36:07 字数 818 浏览 1 评论 0原文

我有以下两个脚本。我希望创建一个文本文件,其结果位于

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霊感 2024-11-22 01:36:07

@Chris Browne 说的是你可以使用 CmdLet Out-File

我添加的东西是为了获取脚本执行目录:

$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path;
$textFileName = $scriptPath + "\yourfile.txt"
$sum | out-file -FilePath $textFileName 

The thing @Chris Browne says is that you can use CmdLet Out-File

The thing I add is for getting script exécution directory :

$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path;
$textFileName = $scriptPath + "\yourfile.txt"
$sum | out-file -FilePath $textFileName 
爱格式化 2024-11-22 01:36:07

我相信您正在寻找的 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

沉溺在你眼里的海 2024-11-22 01:36:07

通过管道 $sum 或 $cameras 到 Out-File cmdlet。要在当前目录中创建文件,只需传递文件名。要在 C: 驱动器中创建它,请传递完整路径:

$sum | Out-File -Path Results.txt

or 

$sum | Out-File -Path .\Results.txt

or:

$sum | Out-File -Path C:\Results.txt

顺便说一句,您可以使用 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:

$sum | Out-File -Path Results.txt

or 

$sum | Out-File -Path .\Results.txt

or:

$sum | Out-File -Path C:\Results.txt

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文