如何合并多个脚本并使用函数来显示输出

时间:2011-08-10 11:00:39

标签: powershell

你们中的几个人(特别是谢谢!)昨天帮助了我,我设法提出了多个脚本,现在我们将它们合并为一个。

问题是我已将它们全部放入“函数”中,并且一个函数依赖于对修补程序的成功检查 - 如果未安装此修补程序,则脚本必须在那里停止然后 - 这是第一次检查脚本的确如此。如何获取下一个函数来调用修补程序函数的成功输出?

此外我不知道如何调用这些函数 - 即在底部我将函数的名称放在一行之后但最终循环遍历!

希望有人可以提供帮助。

Write-Host "=================================="
Write-Host "Pre-Staging Script for DFSR Server"
Write-Host "=================================="

Function Service
{
    Write-Host "=================================="
    Write-Host "Checking Service Installation"
    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 ""

    $hotfix1 = Get-HotFix -id KB979808 -ErrorAction SilentlyContinue

        If($hotfix1)
        {
            Write-Host "Hotfix is installed you may proceed" -foregroundcolor "green"
            Write-Host ""
        }
    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 ""
        }
}

Function Robocopy ($hotfix1)
 {
    Write-Host "============="
    Write-Host "Robocopy Data" 
    Write-Host "============="

    $Source = Read-Host "Please enter path of SOURCE"
    $Destination = Read-Host "Please enter path of TARGET"

    $Output = Read-Host "Please enter where to place output file eg c:\temp\COPY.log"

    robocopy $Source $Target /b /e /copyall /r:1 /xd dfsrprivate /log:$Output /tee
 }

Function Comparision
{
    Write-Host "==============================================="
    Write-Host "Checking Directory Count and Folder comparision" -ErrorAction SilentlyContinue -BackgroundColor Cyan -ForegroundColor Black
    Write-Host "==============================================="
    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
}

1 个答案:

答案 0 :(得分:4)

要使用函数Service中的修补程序,您应该将修补程序返回给变量。该功能如下所示:

Function Service
{
    Write-Host "=================================="
    Write-Host "Checking Service Installation"
    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 ""

    # This will return any output.
    Get-HotFix -id KB979808 -ErrorAction SilentlyContinue

}

然后函数调用将是:

$hotfix = Service
if($hotfix) {
    Robocopy
}
else {
    # This will exit the script.
    return
}

函数Robocopy不需要$hotfix1作为参数,因为它不会在函数的任何地方使用。

Robocopy函数可能正在循环,因为对robocopy.exe的调用与Robocopy函数相同;尝试将“.exe”添加到robocopy.exe调用。命名功能以准确反映其目的非常重要。 Service可以是Get-HotFixKB979808Robocopy可以是Start-MyRobocopy

说了这么多,既然你的功能做了非常具体的事情,他们真的不需要成为他们自己的功能。您可以通过让它们接受参数来将它们更改为可重用。