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

时间:2011-08-18 15:22:01

标签: 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

3 个答案:

答案 0 :(得分:10)

powershell中的变量是上下文敏感的。如果我定义一个像:

这样的函数
$bar = "Hi"
function foo {
   $bar = "Hey!"
}
$bar <-- returns "Hi"

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

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

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

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

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

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

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

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

Help About_Scope

答案 1 :(得分:1)

hackish方式是将其添加到顶部:

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

使用$ Gobal:$和$ Gobal:target搜索并替换$ source,然后您可以在脚本中的任何位置使用这些新的全局变量。

正如所建议的那样,你可以在另一个功能中保护它们,但是对于一个简单的工作\自动化任务可能有点过分。取决于它的用途。

答案 2 :(得分:0)

你有几个选择。其中两个是全局状态(在文件范围内声明$ source和$ target,两个函数都使用它们)并将Comparison作为参数。假设您的robocopy函数向用户查询源和目标(如果它们为null),那么全局状态是最简单的解决方案。就个人而言,我可能会编写一个名为Get-SourceAndTarget的第三个函数来处理该部分并输出源和目标,这样您就可以将它们传递给Start-MyRobocopy和Comparison。我并没有很好地破解PowerShell,所以我对语法有点不清楚,但这应该可以让你开始。