使用函数中的值并将其传递给另一个函数

lar*_*400 3 powershell

我有以下两个函数,我想要在Comparision函数中使用的ROBOCOPY函数的SOURCE和TARGET.任何人都可以告诉我该怎么做.谢谢.

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
Run Code Online (Sandbox Code Playgroud)

Cos*_*Key 10

PowerShell中的变量是上下文敏感的.如果我定义一个函数,如:

$bar = "Hi"
function foo {
   $bar = "Hey!"
}
$bar <-- returns "Hi"
Run Code Online (Sandbox Code Playgroud)

然后在该函数之外我无法使用$ bar变量.要在函数外部使变量可用,您可以控制函数的范围.如果我使用脚本或全局前缀在函数中设置变量,那么该变量将可用于整个脚本,或者在powershell运行空间中全局可用.看这里:

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

function bar {
   foo
   $global:barVar = "Hello " + $fooVar
}
Run Code Online (Sandbox Code Playgroud)

由于变量$ fooVar的作用域前缀脚本,foo函数中的变量$ fooVar将可用于脚本中的所有其他函数.barVar函数将在运行空间中全局可用.即,当您的脚本完成后,变量仍然存在于命令行甚至其他脚本中.

正如您在bar函数中看到的,我首先调用foo然后使用foovVar变量.当我使用$ fooVar变量时,我不必指定$ script:fooVar,如果我愿意,我可以,但它没有必要.

这些都是有效的变量赋值:

$aaa = 123
$script:bbb = 123
$global:ccc = 123 
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下使用$ script:source和$ script:target或$ global:source和$ global:target.有关更多信息,请运行以下命令:

Help About_Scope
Run Code Online (Sandbox Code Playgroud)