Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use values from a function and pass it to another function

Tags:

powershell

I have the below two functions and I want the SOURCE and TARGET of the ROBOCOPY function used within the Comparision function. Can anyone tell me how to do this please. Thanks.

Write-Host "==================================" -ForegroundColor Magenta -BackgroundColor White
Write-Host "Pre-Staging Script for DFSR Server" -ForegroundColor Magenta -BackgroundColor White
Write-Host "==================================" -ForegroundColor Magenta -BackgroundColor White
Write-Host ""
Function GetHot-Fix
{
                Write-Host "==================================" -ForegroundColor Magenta -BackgroundColor White
                Write-Host "Checking Service Installation" -ForegroundColor Magenta -BackgroundColor White
                Write-Host "==================================" -ForegroundColor Magenta -BackgroundColor White
                Write-Host ""
                write-host "This will check if Hotfix KB979808 is installed." -ForegroundColor Black -BackgroundColor Cyan
                write-host "This is required for Windows Server 2008 R2 Robocopying"  -ForegroundColor Black -BackgroundColor Cyan
                Write-Host ""

                Get-HotFix -id KB979808 -ErrorAction SilentlyContinue

}

Function Start-MyRobocopy($source,$Target)
{
                Write-Host "=============" -ForegroundColor Magenta -BackgroundColor White
                Write-Host "Robocopy Data" -ForegroundColor Magenta -BackgroundColor White
                Write-Host "=============" -ForegroundColor Magenta -BackgroundColor White
                Write-Host ""

                $Source = Read-Host "Please enter path of SOURCE"

                If ($Source -and (Test-Path -Path $Source -PathType Container))
                {
                                $Target = Read-Host "Please enter path of TARGET"
                }
                Else
                {
                Write-Host "Please enter a directory"
                }
                                If ($Target -and (Test-Path -Path $Target -PathType Container))
                                {
                                $Output = Read-Host "Please enter where to place output file eg c:\temp\COPY.log"
                                }
                                Else
                                {
                                Write-Host "Please enter a directory"
                                }


robocopy.exe $Source $Target /b /e /copyall /r:1 /xd dfsrprivate /log:$Output /tee
}
Function Comparision
{
                Write-Host ""
                Write-Host ""     
                Write-Host "===============================================" -ForegroundColor Magenta -BackgroundColor White
                Write-Host "Checking Directory Count and Folder comparision" -ErrorAction SilentlyContinue -ForegroundColor Magenta -BackgroundColor White
                Write-Host "===============================================" -ForegroundColor Magenta -BackgroundColor White
                Write-Host ""

             #$Source = Read-Host "Please enter Source directory to check"
             #$Target = Read-Host "Please enter Target directory to check"
                Write-Host ""
                If($source -and (Test-Path -Path $source -PathType Container))
                {
                "There are $(@(Get-ChildItem $Source).Count) items in the '$Source' directory"  
                }
                Else
                {
                Write-Host "Please enter a directory"
                                }
                                If($source -and (Test-Path -Path $Target -PathType Container))
                                {
                                "There are $(@(Get-ChildItem $Target).Count) items in the '$Target' directory"  
                }
                Else
                {
                Write-Host "Please enter a directory"
                }

                Write-Host ""
                $child1 = Get-ChildItem -Path $Source -Recurse -Force
                $child2 = Get-ChildItem -Path $Target -Recurse -Force

                Compare-Object $child1 -DifferenceObject $child2 -Property Name

                Write-Host ""
                Write-Host "NOTE:" -BackgroundColor Cyan -ForegroundColor Black
                Write-Host "Any symbols with '=>' mean that the file Does NOT exist in SOURCE but is in the Target" -BackgroundColor Cyan -ForegroundColor Black
                Write-Host "Any symbols with '<=' mean that the file Does NOT exist in TARGET but is in the Source" -BackgroundColor Cyan -ForegroundColor Black
}


$hotfix = GetHot-Fix
If ($hotfix) {
                Write-Host "Hotfix installed" -BackgroundColor Green -ForegroundColor Black
                Write-Host ""
                Write-Host "Proceeding with Robocopy...."
                Write-Host "............................"
                Write-Host ""
                Start-MyRobocopy
                }
else {
                                Write-Host "Hotfix is NOT installed - Please ensure you install this hotfix BEFORE" -ForegroundColor "red"
        Write-host "Copying any data" -foregroundcolor "red"
                                Write-Host ""
                                return
                }

Comparision
like image 970
lara400 Avatar asked Dec 09 '22 06:12

lara400


2 Answers

Variables in powershell are context sensitive. If I define a function like:

$bar = "Hi"
function foo {
   $bar = "Hey!"
}
$bar <-- returns "Hi"

Then the $bar variable is not available to me outside that function. To make variables available outside of functions then you can control the scope of a function. If I set a variable in a function using the script or global prefix then the variable will be available for the whole script or globally in powershell runspace. See here:

function foo {
   $script:fooVar = "world"
}

function bar {
   foo
   $global:barVar = "Hello " + $fooVar
}

The variable $fooVar in the foo function will be available to all other functions within the script due to the scope prefix script of the variable $fooVar. The barVar function will be available globally in the runspace. I.e. when your script has finished the variable is still present back at the command line and even to other scripts.

As you can see in the bar function I first call foo and then use the foovVar variable. When I use the $fooVar variable I don't have to specify the $script:fooVar, I can if I want to but it's not necessary.

These are all valid variable assignments:

$aaa = 123
$script:bbb = 123
$global:ccc = 123 

So in your case use $script:source and $script:target or $global:source and $global:target. For more info run the following command:

Help About_Scope
like image 164
CosmosKey Avatar answered Mar 03 '23 18:03

CosmosKey


the hackish way is to add this to the top:

$Gobal:source = ""
$Gobal:target = ""

and search and replace $source with $Gobal:source and $target with $Gobal:target - then you can use these new global variables at any point in the script.

As suggested you could protect them in another function but for a simple job\automated task that might be overkill. Depends what it's for.

like image 43
Matt Avatar answered Mar 03 '23 19:03

Matt