Имам проблем, надявам се някой да може да помогне с...
Имам следния код, който има потребителско меню и рекурсивно търси текстови файлове и файлове, съдържащи низа "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, помощта ви за това ще бъде много оценена, благодаря