functionpowershellscopepowershell-2.0subroutine

Windows Powershell - Returning and supplying variables to functions does not work


I have the following code that has a user menu and recursively searches for text files and files containing the string "hello", it then prints a HTML file with the results:

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

The problem I am having:

Using the user menu I can successfully run the script to find text files and to find files containing the string hello, any results it finds are added to $MyReport using functions that construct HTML for the HTML file. This all works perfectly.

However when I try to then call the printeverything function with the $MyReport variable so that it will create the HTML file for me, it does not work.

The code to create the HTML file works perfectly as I have tested it, I believe the problem is that the $MyReport variable is not being passed to the printeverything function correctly but I cannot work out what I am doing wrong.


Solution

  • In your do-while look, $MyReport is just passed to the print everything method. But, it will not be in scope at that point as you are just assigning to $MyReport within the functions.

    Try using $MyReport as $script:MyReport everywhere, so that it will be in script scope. This is not the ideal solution, but should be good enough to get you started.

    Also, you might want to read up on pipeline / function output in powershell. You are using it wrong - http://stacktoheap.com/blog/2013/06/15/things-that-trip-newbies-in-powershell-pipeline-output/