无法将函数输出写入文件

时间:2016-01-10 15:44:09

标签: csv powershell

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

$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

1 个答案:

答案 0 :(得分:0)

您的函数没有返回任何内容,因为最后一行是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。