使用函数中的值并将其传递给另一个函数
我有以下两个函数,我希望在比较函数中使用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
powershell中的变量是上下文相关的。如果我定义的功能如下:
$bar = "Hi"
function foo {
$bar = "Hey!"
}
$bar <-- returns "Hi"
然后$ bar变量在该函数之外对我不可用。为了使变量在函数外可用,您可以控制函数的作用域。如果我使用脚本或全局前缀在函数中设置了一个变量,那么该变量将可用于整个脚本或全局在powershell运行空间中。在这里看到:
function foo {
$script:fooVar = "world"
}
function bar {
foo
$global:barVar = "Hello " + $fooVar
}
在foo的函数变量$ fooVar将提供给脚本中的所有其他功能因变量$ fooVar的范围前缀脚本。 barVar函数将在运行空间中全局可用。即当脚本完成后,变量仍然存在于命令行,甚至还存在于其他脚本中。
正如你可以在bar函数中看到的,我首先调用foo,然后使用foovVar变量。当我使用$ fooVar变量时,我不必指定$ script:fooVar,如果我想要但不是必需的。
这些都是有效的变量赋值:
$aaa = 123
$script:bbb = 123
$global:ccc = 123
所以你的情况使用$脚本:源和$脚本:目标或$全局:源和$全球:目标。欲了解更多信息运行以下命令:
Help About_Scope
您有几个选择。其中两个是全局状态(在文件范围内声明$ source和$ target,并且这两个函数都使用它们),并将其作为参数进行比较。假设您的robocopy函数查询源和目标的用户是否为空,那么全局状态是最简单的解决方案。就我个人而言,我可能会编写一个名为Get-SourceAndTarget的第三个函数来处理该部分并输出源和目标,以便将它们传递给Start-MyRobocopy和Comparison。我并没有很好地攻击powershell,所以我对语法有点不清楚,但这可能会让你开始。
的hackish的方式是把它添加到顶部:
$Gobal:source = ""
$Gobal:target = ""
和搜索,并用$ Gobal替换$来源:源和$目标与$ Gobal :target - 那么你可以在脚本的任何一点使用这些新的全局变量。
正如建议你可以在另一个功能,但一个简单的工作\自动化任务,可能是矫枉过正保护他们。取决于它的用途。
比较如何调用?它是与robocopy分离还是会从robocopy中调用? – SirPentor
嗨 - 我已经更新了整个脚本,你可以看到它是如何运行的。 – lara400
有完整源代码的最终解决方案? – Kiquenet