如何合并多个脚本并使用函数显示输出
你们中的一些人(尤其是谢伊!)昨天帮助了我,我已经设法想出了多个脚本,现在正在将它们合并为一个。
问题是我已将它们全部放入“函数”中,并且一个函数依赖于对修补程序的成功检查 - 如果未安装修补程序,则脚本必须在那里停止,然后 - 这是脚本执行的第一个检查。如何获取下一个函数来调用修补程序函数的成功输出?
另外,我不知道如何调用这些函数 - 即在底部,我将函数的名称一个接一个地放在一行,但最终一遍又一遍地循环!
希望有人可以帮忙。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用函数
Service
中找到的修补程序,您应该将该修补程序返回到变量。该函数将如下所示:那么函数调用将是:
函数
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:Then the function calls would be:
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 yourRobocopy
function; try adding ".exe" to the robocopy.exe call. It is important to name functions to accurately reflect their purpose.Service
could beGet-HotFixKB979808
andRobocopy
could beStart-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.