gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:24:59 25 4
gpt4 key购买 nike

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

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

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

希望有人能帮忙。

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
}

最佳答案

要使用在函数 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 调用中。命名函数以准确反射(reflect)其用途非常重要。 Service 可以是 Get-HotFixKB979808Robocopy 可以是 Start-MyRobocopy

话虽如此,由于您的函数执行非常具体的操作,因此它们实际上不需要是自己的函数。您可以通过让它们接受参数来将它们更改为更可重用。

关于powershell - 如何合并多个脚本并使用函数显示输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7009735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com