首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用函数中的值并将其传递给另一个函数

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

Stack Overflow用户
提问于 2011-08-18 23:22:01
回答 3查看 29.3K关注 0票数 4

我有以下两个函数,我希望在比较函数中使用ROBOCOPY函数的源和目标。有人能告诉我怎么做吗?谢谢。

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-19 05:30:34

powershell中的变量是上下文相关的。如果我定义如下函数:

代码语言:javascript
复制
$bar = "Hi"
function foo {
   $bar = "Hey!"
}
$bar <-- returns "Hi"

那么在该函数之外,我就不能使用$bar变量。要使变量在函数之外可用,则可以控制函数的作用域。如果我使用脚本或全局前缀在函数中设置一个变量,那么该变量将可用于整个脚本或powershell运行空间中的全局。请看这里:

代码语言:javascript
复制
function foo {
   $script:fooVar = "world"
}

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

由于变量$fooVar的作用域前缀script,foo函数中的变量$fooVar将可用于脚本中的所有其他函数。barVar函数将在运行空间中全局可用。也就是说,当你的脚本结束时,变量仍然存在于命令行甚至其他脚本中。

正如您在bar函数中看到的,我首先调用foo,然后使用foovVar变量。当我使用$fooVar变量时,我不需要指定$script:fooVar,如果我想的话,我可以这样做,但这不是必须的。

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

代码语言:javascript
复制
$aaa = 123
$script:bbb = 123
$global:ccc = 123 

因此,在您的示例中,使用$script:source和$script:target或$global:source和$global:target。有关详细信息,请运行以下命令:

代码语言:javascript
复制
Help About_Scope
票数 13
EN

Stack Overflow用户

发布于 2011-08-19 17:41:32

粗俗的方法是将以下内容添加到顶部:

代码语言:javascript
复制
$Gobal:source = ""
$Gobal:target = ""

用$Gobal:source搜索并替换$source,用$Gobal:target替换$target --这样您就可以在脚本中的任何地方使用这些新的全局变量。

正如所建议的,你可以在另一个函数中保护它们,但对于一个简单的作业\自动化任务来说,这可能是过度杀伤力。这要看它是用来做什么的。

票数 1
EN

Stack Overflow用户

发布于 2011-08-18 23:46:46

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7109920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档