【问题标题】:Windows Powershell - Returning and supplying variables to functions does not workWindows Powershell - 向函数返回和提供变量不起作用
【发布时间】:2013-08-21 12:00:40
【问题描述】:

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

我有以下代码,它有一个用户菜单并递归搜索文本文件和包含字符串“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

但是,当我尝试使用 $MyReport 变量调用 printeverything 函数以便它为我创建 HTML 文件时,它不起作用。

创建 HTML 文件的代码正如我测试过的那样完美运行,我认为问题在于 $MyReport 变量没有正确传递给 printeverything 函数,但我无法锻炼自己做错了

由于我是 Powershell 新手,非常感谢您的帮助,谢谢

【问题讨论】:

    标签: function powershell scope powershell-2.0 subroutine


    【解决方案1】:

    在您的do-while 外观中,$MyReport 只是传递给print everything 方法。但是,此时它不在范围内,因为您只是在函数中分配给$MyReport

    尝试在任何地方使用$MyReport 作为$script:MyReport,这样它就会在脚本范围内。这不是理想的解决方案,但应该足以让您入门。

    另外,您可能需要阅读 powershell 中的管道/函数输出。你用错了——http://stacktoheap.com/blog/2013/06/15/things-that-trip-newbies-in-powershell-pipeline-output/

    【讨论】:

    • 我会试一试,当然也要看看你提供的链接......你说这个解决方案并不理想,那么理想的方法是什么?感谢您的帮助
    • @perl-user - 理想的方法是从函数获得适当的范围和返回值,并将返回的值分配给本地范围内的变量,并通过将其传递给其他函数等来使用它。
    • 是的,我同意,这就是我想要实现的目标。我已经尝试过多次这样做,但仍然没有运气。我不知道它是否在语法上我做错了,但我无法让它工作(我可以在 perl 中轻松地做那种事情,但在 powershell 中遇到麻烦)。您能否向我展示如何正确完成此操作的示例,谢谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2017-08-08
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2018-05-10
    相关资源
    最近更新 更多