如何调用另一个函数并捕获这两个函数中的错误?
How to call another function and catch the error in these two Functions?
我想结合以下脚本,以便它可以自动复制 AD 数据库内容,而无需等待所有在线域控制器的 15 分钟。
function Replicate-AllDomainController
{
(Get-ADDomainController -Filter *).Name | Foreach-Object { repadmin /syncall $_ (Get-ADDomain).DistinguishedName /e /A | Out-Null }; Start-Sleep 10; Get-ADReplicationPartnerMetadata -Target "$env:userdnsdomain" -Scope Domain | Select-Object Server, LastReplicationSuccess
}
function Test-AllDomainController
{
$dcs = (Get-ADDomainController -Filter *).Name
foreach ($items in $dcs)
{
Test-Connection $items -Count 1
}
}
Try
{
Where-Object (Test-AllDomainController)
{
Replicate-AllDomainController
}
}
Catch
{
Write-Output "Exception Type: $($_.Exception.GetType().FullName)"
Write-Output "Exception Message: $($_.Exception.Message)"
}
如何正确执行,以便在成功复制最后一个 AD DC 后停止?
如果有问题,在单独的 Out-GridView 中显示有问题的 AD 域控制器。
我自己无法测试,但我认为最好在调用 repadmin.exe.
的函数中执行 try{}..catch{}
此外,在同一个函数中,测试是否可以通过 Test-Connection
访问服务器会更简单,因此您可以使用一个函数完成两件事:
# create a list to collect errors
$errorList = [System.Collections.Generic.List[object]]::new()
function Replicate-AllDomainController {
(Get-ADDomainController -Filter *).Name | Foreach-Object {
# put the servername from the $_ automatic variable in a variable of your own, because when you
# hit the catch block, inside there the $_ is the Exception object and no longer the server name.
$dc = $_
if (Test-Connection $dc -Count 1 -Quiet) {
try {
repadmin /syncall $dc (Get-ADDomain).DistinguishedName /e /A | Out-Null
}
catch {
# the $errorList is available here using the script scope
$script:errorList.Add(
[PsCustomObject]@{
'Server' = $dc
'Exception Type' = $_.Exception.GetType().FullName
'Exception Message' = $_.Exception.Message
}
)
}
}
else {
Write-Warning "Server '$dc' cannot be reached"
}
}
Start-Sleep -Seconds 10
Get-ADReplicationPartnerMetadata -Target "$env:userdnsdomain" -Scope Domain | Select-Object Server, LastReplicationSuccess
}
# call the function
Replicate-AllDomainController
# check if there were replication errors
if ($errorList.Count) {
# if there were errors, show them in a separate GridView
$errorlist | Out-GridView -Title "Replication errors"
}
else {
Write-Host "All done; no errors reported" -ForegroundColor Green
}
我想结合以下脚本,以便它可以自动复制 AD 数据库内容,而无需等待所有在线域控制器的 15 分钟。
function Replicate-AllDomainController
{
(Get-ADDomainController -Filter *).Name | Foreach-Object { repadmin /syncall $_ (Get-ADDomain).DistinguishedName /e /A | Out-Null }; Start-Sleep 10; Get-ADReplicationPartnerMetadata -Target "$env:userdnsdomain" -Scope Domain | Select-Object Server, LastReplicationSuccess
}
function Test-AllDomainController
{
$dcs = (Get-ADDomainController -Filter *).Name
foreach ($items in $dcs)
{
Test-Connection $items -Count 1
}
}
Try
{
Where-Object (Test-AllDomainController)
{
Replicate-AllDomainController
}
}
Catch
{
Write-Output "Exception Type: $($_.Exception.GetType().FullName)"
Write-Output "Exception Message: $($_.Exception.Message)"
}
如何正确执行,以便在成功复制最后一个 AD DC 后停止? 如果有问题,在单独的 Out-GridView 中显示有问题的 AD 域控制器。
我自己无法测试,但我认为最好在调用 repadmin.exe.
的函数中执行try{}..catch{}
此外,在同一个函数中,测试是否可以通过 Test-Connection
访问服务器会更简单,因此您可以使用一个函数完成两件事:
# create a list to collect errors
$errorList = [System.Collections.Generic.List[object]]::new()
function Replicate-AllDomainController {
(Get-ADDomainController -Filter *).Name | Foreach-Object {
# put the servername from the $_ automatic variable in a variable of your own, because when you
# hit the catch block, inside there the $_ is the Exception object and no longer the server name.
$dc = $_
if (Test-Connection $dc -Count 1 -Quiet) {
try {
repadmin /syncall $dc (Get-ADDomain).DistinguishedName /e /A | Out-Null
}
catch {
# the $errorList is available here using the script scope
$script:errorList.Add(
[PsCustomObject]@{
'Server' = $dc
'Exception Type' = $_.Exception.GetType().FullName
'Exception Message' = $_.Exception.Message
}
)
}
}
else {
Write-Warning "Server '$dc' cannot be reached"
}
}
Start-Sleep -Seconds 10
Get-ADReplicationPartnerMetadata -Target "$env:userdnsdomain" -Scope Domain | Select-Object Server, LastReplicationSuccess
}
# call the function
Replicate-AllDomainController
# check if there were replication errors
if ($errorList.Count) {
# if there were errors, show them in a separate GridView
$errorlist | Out-GridView -Title "Replication errors"
}
else {
Write-Host "All done; no errors reported" -ForegroundColor Green
}