我有一个问题我希望有人能够帮助...
我有以下代码,它有一个用户菜单并递归搜索文本文件和包含字符串“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 的文件,它找到的任何结果都将添加到$MyReport
使用为 html 文件构造 html 的函数中。<-- 这一切都完美无缺
但是,当我尝试printeverything
使用变量调用该函数$MyReport
以便它为我创建 HTML 文件时,它不起作用。
创建 HTML 文件的代码正如我测试过的那样完美运行,我相信问题是$MyReport
变量没有正确传递给printeverything
函数,但我无法锻炼我做错了什么
由于我是 Powershell 的新手,非常感谢您的帮助,谢谢