2011-08-18 25 views
1

我有以下兩個函數,我希望在比較函數中使用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 
+0

比較如何調用?它是與robocopy分離還是會從robocopy中調用? – SirPentor

+0

嗨 - 我已經更新了整個腳本,你可以看到它是如何運行的。 – lara400

+0

有完整源代碼的最終解決方案? – Kiquenet

回答

7

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 
0

您有幾個選擇。其中兩個是全局狀態(在文件範圍內聲明$ source和$ target,並且這兩個函數都使用它們),並將其作爲參數進行比較。假設您的robocopy函數查詢源和目標的用戶是否爲空,那麼全局狀態是最簡單的解決方案。就我個人而言,我可能會編寫一個名爲Get-SourceAndTarget的第三個函數來處理該部分並輸出源和目標,以便將它們傳遞給Start-MyRobocopy和Comparison。我並沒有很好地攻擊powershell,所以我對語法有點不清楚,但這可能會讓你開始。

0

的hackish的方式是把它添加到頂部:

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

和搜索,並用$ Gobal替換$來源:源和$目標與$ Gobal :target - 那麼你可以在腳本的任何一點使用這些新的全局變量。

正如建議你可以在另一個功能,但一個簡單的工作\自動化任務,可能是矯枉過正保護他們。取決於它的用途。

相關問題