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

Powershell 使用函数中的值并将其传递给另一个函数,powershell,Powershell,我有以下两个函数,我希望在比较函数中使用ROBOCOPY函数的源和目标。谁能告诉我怎么做吗。谢谢 Write-Host "==================================" -ForegroundColor Magenta -BackgroundColor White Write-Host "Pre-Staging Script for DFSR Server" -ForegroundColor Magenta -BackgroundColor White Write-Hos

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

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
Write Host”============================================================“-前底色洋红色-后底色白色
编写主机“DFSR服务器的预暂存脚本”-前底色洋红色-后底色白色
写入主机“=======================================================”-背景色洋红色-背景色白色
写入主机“”
函数GetHot-Fix
{
写入主机“=======================================================”-背景色洋红色-背景色白色
写主机“检查服务安装”-前底色洋红色-后底色白色
写入主机“=======================================================”-背景色洋红色-背景色白色
写入主机“”
写入主机“这将检查是否安装了修补程序KB979808。”-ForegroundColor黑色-BackgroundColor青色
写入主机“这是Windows Server 2008 R2机器人复制所必需的”-ForegroundColor黑色-BackgroundColor青色
写入主机“”
获取修补程序-id KB979808-ErrorAction SilentlyContinue
}
函数启动MyRobocopy($source,$Target)
{
写入主机“================”-背景色洋红色-背景色白色
写入主机“Robocopy数据”-背景色洋红-背景色白色
写入主机“================”-背景色洋红色-背景色白色
写入主机“”
$Source=读取主机“请输入源路径”
If($Source-and(测试路径-Path$Source-PathType容器))
{
$Target=读取主机“请输入目标路径”
}
其他的
{
写入主机“请输入目录”
}
If($Target-and(测试路径-Path$Target-PathType容器))
{
$Output=Read Host“请输入输出文件的放置位置,例如c:\temp\COPY.log”
}
其他的
{
写入主机“请输入目录”
}
robocopy.exe$Source$Target/b/e/copyall/r:1/xd dfsrprivate/log:$Output/tee
}
功能比较
{
写入主机“”
写入主机“”
写入主机“===============================================================================”-背景颜色洋红色-背景颜色白色
写入主机“检查目录计数和文件夹比较”-ErrorAction SilentlyContinue-ForegroundColor品红-BackgroundColor白色
写入主机“===============================================================================”-背景颜色洋红色-背景颜色白色
写入主机“”
#$Source=读取主机“请输入要检查的源目录”
#$Target=读取主机“请输入要检查的目标目录”
写入主机“”
If($source-and(测试路径-Path$source-PathType容器))
{
“在“$Source”目录中有$(@(Get ChildItem$Source).Count)个项目”
}
其他的
{
写入主机“请输入目录”
}
If($source-and(测试路径-Path$Target-PathType容器))
{
“在“$Target”目录中有$(@(Get ChildItem$Target).Count)个项目”
}
其他的
{
写入主机“请输入目录”
}
写入主机“”
$child1=Get ChildItem-Path$Source-Recurse-Force
$child2=Get ChildItem-Path$Target-Recurse-Force
比较对象$child1-差异对象$child2-属性名称
写入主机“”
写主机“注意:”-背景色青色-背景色黑色
Write Host“任何带有“=>”的符号表示文件在源中不存在,但在目标中”-BackgroundColor青色-ForegroundColor黑色

Write Host“任何带有“的符号您有几个选择。其中两个是全局状态(在文件范围内声明$source和$target,两个函数都使用它们)并将其作为参数进行比较。假设robocopy函数在源和目标为空时向用户查询源和目标,那么全局状态是最简单的解决方案。我个人可能会编写第三个函数,名为Get SourceAndTarget,它处理该部分并输出源和目标,以便您可以在Start MyRobocopy和Comparison两个方面都进行了测试。我对powershell的黑客攻击不是很好,所以我对语法有点不清楚,但这可能会让您开始学习。

powershell中的变量是上下文敏感的。如果我定义了如下函数:

$bar = "Hi"
function foo {
   $bar = "Hey!"
}
$bar <-- returns "Hi"
由于变量$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
$aaa = 123
$script:bbb = 123
$global:ccc = 123 
Help About_Scope
$Gobal:source = ""
$Gobal:target = ""