Powershell 如何合并多个脚本并使用函数显示输出
你们中的一些人(特别是Shay!)昨天帮助了我,我已经想出了多个脚本,现在正在将它们合并成一个 问题是,我已经将它们全部放入“函数”中,其中一个函数依赖于对修补程序的成功检查-如果没有安装修补程序,则脚本必须立即停止,然后-这是脚本执行的第一次检查。如何让下一个函数调用修补程序函数的成功输出 我也不知道如何调用函数——也就是说,在底部我把函数的名称放在一行之后,但最后循环了一次又一次 希望有人能帮忙Powershell 如何合并多个脚本并使用函数显示输出,powershell,Powershell,你们中的一些人(特别是Shay!)昨天帮助了我,我已经想出了多个脚本,现在正在将它们合并成一个 问题是,我已经将它们全部放入“函数”中,其中一个函数依赖于对修补程序的成功检查-如果没有安装修补程序,则脚本必须立即停止,然后-这是脚本执行的第一次检查。如何让下一个函数调用修补程序函数的成功输出 我也不知道如何调用函数——也就是说,在底部我把函数的名称放在一行之后,但最后循环了一次又一次 希望有人能帮忙 Write-Host "=================================="
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
}
写主机”=====================================================================================================================================================================================================================================================================
编写主机“DFSR服务器的预暂存脚本”
写入主机“==========================================================”
功能服务
{
写入主机“==========================================================”
编写主机“检查服务安装”
写入主机“==========================================================”
写入主机“这将检查是否安装了修补程序KB979808。”-ForegroundColor黑色-BackgroundColor青色
写入主机“这是Windows Server 2008 R2机器人复制所必需的”-ForegroundColor黑色-BackgroundColor青色
写入主机“”
$hotfix1=获取修补程序-id KB979808-ErrorAction SilentlyContinue
如果($hotfix1)
{
写入主机“已安装修补程序,您可以继续”-foregroundcolor“绿色”
写入主机“”
}
其他的
{
写入主机“未安装修补程序-请确保在安装此修补程序之前”-ForegroundColor“红色”
写入主机“复制任何数据”-foregroundcolor“红色”
写入主机“”
}
}
功能机器人复制($hotfix1)
{
写入主机“======================”
写入主机“Robocopy数据”
写入主机“======================”
$Source=读取主机“请输入源路径”
$Destination=读取主机“请输入目标路径”
$Output=Read Host“请输入输出文件的放置位置,例如c:\temp\COPY.log”
robocopy$Source$Target/b/e/copyall/r:1/xd dfsrprivate/log:$Output/tee
}
功能比较
{
写入主机“==================================================================================”
写入主机“检查目录计数和文件夹比较”-ErrorAction SilentlyContinue-BackgroundColor青色-ForegroundColor黑色
写入主机“==================================================================================”
写入主机“”
$Source=读取主机“请输入要检查的源目录”
$Target=读取主机“请输入要检查的目标目录”
写入主机“”
If($source-and(测试路径-Path$source-PathType容器))
{
“在“$Source”目录中有$(@(Get ChildItem$Source).Count)个项目”
}
其他的
{
写入主机“请输入目录”
}
If($source-and(测试路径-Path$Target-PathType容器))
{
“在“$Target”目录中有$(@(Get ChildItem$Target).Count)个项目”
}
其他的
{
写入主机“请输入目录”
}
写入主机“”
$child1=Get ChildItem-Path$Source-Recurse-Force
$child2=Get ChildItem-Path$Target-Recurse-Force
比较对象$child1-差异对象$child2-属性名称
写入主机“”
写主机“注意:”-背景色青色-背景色黑色
Write Host“任何带有“=>”的符号表示文件在源中不存在,但在目标中”-BackgroundColor青色-ForegroundColor黑色
将Host“Any symbols with'写入要使用函数服务中找到的修补程序,应将修补程序返回到变量。函数如下所示:
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。命名函数以准确反映其用途非常重要。服务
可以是Get-hotfix KB979808
,robocopy
可以是Start myrobocy
话虽如此,由于你的函数做了非常具体的事情,它们真的不需要是它们自己的函数。你可以通过让它们接受参数来更改它们,使其更易于重用。非常棒!非常感谢这一点-我更改了脚本并运行了它,一切都很好。一个问题-如何将脚本更改为accept参数?…可能对此有一个了解..您可以接受诸如,函数MyRobocopy($Sorce,$Target)之类的参数{robocopy$Source$Target/b/e/copyall/r:1/xd dfsrprivate/log:$Output/tee
。您可以像myrobocy C:\Temp\MyFolder D:\Temp\MyFolder
一样调用它。有关函数的详细信息,请参阅有关函数的帮助。