无法将函数输出写入文件(Can't write function output to file)

系统教程 行业动态 更新时间:2024-06-14 16:52:52
无法将函数输出写入文件(Can't write function output to file)

我目前一直在寻找关于如何轻松将函数结果输出到文件的信息。

$Balls = 5 $Plays = 1..26 function PowerBall { foreach ($Trys in $Plays) { $random = Get-Random -Input @(1..69) -Count $Balls Write-Host $random -NoNewline Write-Host " $Trys" -ForegroundColor Red } } PowerBall | Out-File C:\Windows\Temp\numbers2.txt

I'm currently stuck on finding information on how to easily output the function results to a file.

$Balls = 5 $Plays = 1..26 function PowerBall { foreach ($Trys in $Plays) { $random = Get-Random -Input @(1..69) -Count $Balls Write-Host $random -NoNewline Write-Host " $Trys" -ForegroundColor Red } } PowerBall | Out-File C:\Windows\Temp\numbers2.txt

最满意答案

你的函数没有返回任何东西,因为最后一行是Write-Host命令。 改变你的功能:

function PowerBall { Foreach($Trys in $Plays) { $random = get-random -input @(1..69) -count $Balls Write-Host $random -nonewline write-host " $Trys" -ForegroundColor Red "$random $Trys" } }

另外如果你想输出为文本文件,那么你应该使用Set-Content而不是Export-Csv。

Your function is not returning anything because the last line is a Write-Host command. Change your function to this:

function PowerBall { Foreach($Trys in $Plays) { $random = get-random -input @(1..69) -count $Balls Write-Host $random -nonewline write-host " $Trys" -ForegroundColor Red "$random $Trys" } }

Also if you want to output as a text file then you should use Set-Content instead of Export-Csv.

更多推荐

本文发布于:2023-04-05 12:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/eb59ba0ffb1f3837f64f36d7eecda5dd.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   文件   write   file   output

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!