У меня проблема, надеюсь, кто-нибудь сможет помочь...
У меня есть следующий код, который имеет меню пользователя и рекурсивно ищет текстовые файлы и файлы, содержащие строку «привет», а затем печатает 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, ваша помощь в этом будет очень признательна, спасибо