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

发布于 2024-11-28 23:52:20 字数 3249 浏览 0 评论 0原文

你们中的一些人(尤其是谢伊!)昨天帮助了我,我已经设法想出了多个脚本,现在正在将它们合并为一个。

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

另外,我不知道如何调用这些函数 - 即在底部,我将函数的名称一个接一个地放在一行,但最终一遍又一遍地循环!

希望有人可以帮忙。

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
}

A couple of you (Shay especially!) helped me yesterday and I have managed to come up with multiple scripts and am now merging them into one.

The problem is that I have put them all into "Functions" and one function relies on the successful check on a hotfix - if the hotfix is not installed then the script has to stop right there and then - that is the first check the script does. How do I get the next function to call the successful output of the hotfix function?

Also I have no idea how to call the functions - ie at the bottom I put the name of the functions one line after each other but ended up looping over and over!

hope someone can assist.

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£噩梦荏苒 2024-12-05 23:52:20

要使用函数 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-HotFixKB979808 ,而 Robocopy 可以是 Start-MyRobocopy

话虽如此,因为您的函数确实非常具体的东西,它们确实不需要是自己的函数。您可以通过让它们接受参数来将它们更改为更可重用。

To use the hotfix found in the function Service, you should return the hotfix to a variable. The function would look like this:

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

}

Then the function calls would be:

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

Function Robocopy doesn't need $hotfix1 as a parameter since it's not used anywhere in the function.

The Robocopy function is probably looping because the call to robocopy.exe is the same as your Robocopy function; try adding ".exe" to the robocopy.exe call. It is important to name functions to accurately reflect their purpose. Service could be Get-HotFixKB979808 and Robocopy could be Start-MyRobocopy

Having said all that, since your functions do very specific things, they really don't need to be their own functions. You could change them to be more reusable by having them accept parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文