Function Windows Powershell-向函数返回和提供变量不起作用

Function Windows Powershell-向函数返回和提供变量不起作用,function,powershell,scope,powershell-2.0,subroutine,Function,Powershell,Scope,Powershell 2.0,Subroutine,我有一个问题,我希望有人能帮助我 我有下面的代码,它有一个用户菜单,递归地搜索文本文件和包含字符串hello的文件,然后打印一个html文件并显示结果: Foreach ($Target in $Targets){ #ip address from the text file supplied Function gettextfiles { Write-Output "Collating Detail for $Target" $Results = Get-Chi

我有一个问题,我希望有人能帮助我

我有下面的代码,它有一个用户菜单,递归地搜索文本文件和包含字符串hello的文件,然后打印一个html文件并显示结果:

Foreach ($Target in $Targets){     #ip address from the text file supplied

Function gettextfiles { 

    Write-Output "Collating Detail for $Target"

    $Results = Get-ChildItem -Path $Target -Recurse -Include *.txt
    Write-Output "output from recursively searching for text files $Results"
    $MyReport = Get-CustomHTML "$Target Audit"
    $MyReport += Get-CustomHeader0  "$Target Details"
    $MyReport += Get-CustomHeader "2" "Text files found"

    foreach ($file in $Results) {
        $MyReport += Get-HTMLDetail "Path to the file" ($file)
    }

    $MyReport += Get-CustomHeaderClose

    return $MyReport
}

Function gethello {
    $Results = Get-ChildItem -Path $Target -Recurse | Select-String -pattern hello | group path | select -ExpandProperty name
    Write-Output "output from recursively looking for the string hello $Results"

    $MyReport += Get-CustomHeader "2" "Hello Strings Found"

    foreach ($file in $Results) {
        $MyReport += Get-HTMLDetail "Path to the file" ($file)
    }

    $MyReport += Get-CustomHeaderClose

    return $MyReport
}

####################################################################
# To create the html document from the data gathered in the above functions

Function printeverything([string]$MyReport) {

    $Date = Get-Date
    $Filename = "C:\Desktop" + "_" + $date.Hour + $date.Minute + "_" + $Date.Day + "-" + $Date.Month + "-" + $Date.Year + $Date.Second + ".htm"
    $MyReport | out-file -encoding ASCII -filepath $Filename
    Write "HTML file saved as $Filename"

}
###################################################################
User input menu, call the functions the user chooses then when they press 3 creates the html file

do {
[int]$xMenuChoiceA = 0
while ( $xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 4 ){
Write-host "1. Get Text Files"
Write-host "2. Get Files With The String Hello"
[Int]$xMenuChoiceA = read-host "Please enter an option 1 to 4..." }
Switch( $xMenuChoiceA ){
  1{gettextfiles}
  2{gethello}
  3{printeverything "$MyReport"}
default{<#run a default action or call a function here #>}
}
} while ($xMenuChoiceA -ne 4) 

}  #ending bracket of Targets foreach
我遇到的问题是:

使用用户菜单,我可以成功地运行脚本来查找文本文件和包含字符串hello的文件,它找到的任何结果都将使用为html文件构造html的函数添加到$MyReport中 在do-while外观中,$MyReport只是传递给print-everything方法。但是,此时它将不在范围内,因为您只是在函数中分配给$MyReport

尝试将$MyReport用作$script:MyReport-everywhere,以便它位于脚本范围内。这不是理想的解决方案,但应该足够好,让您开始


此外,您可能希望了解powershell中的管道/函数输出。你用错了-

我会尝试一下,当然也会看看你提供的链接……你说这个解决方案不理想,那么理想的方法是什么呢?谢谢你的帮助help@perl-用户-理想的方法是从函数中获得适当的作用域和返回值,并将返回值分配给本地作用域中的一个变量,然后通过将其传递给其他函数来处理该变量。是的,我同意,这就是我试图实现的目标。我已经试过这么做很多次了,但仍然没有运气。我不知道我是否在语法上做错了,但我无法让它正常工作。我可以用perl轻松地完成这类工作,但在powershell中遇到了麻烦。你能给我举个例子说明如何正确地完成这项工作吗?谢谢你的帮助