Use persistent PSSession for replication
This commit is contained in:
33
certy.ps1
33
certy.ps1
@@ -248,6 +248,8 @@ function Invoke-Replication {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$sessions = @{}
|
||||||
|
try {
|
||||||
foreach ($server in $targets) {
|
foreach ($server in $targets) {
|
||||||
$usesToken = $Command -match "\{server\}"
|
$usesToken = $Command -match "\{server\}"
|
||||||
$cmd = if ($usesToken) { $Command.Replace("{server}", $server) } else { $Command }
|
$cmd = if ($usesToken) { $Command.Replace("{server}", $server) } else { $Command }
|
||||||
@@ -255,17 +257,37 @@ function Invoke-Replication {
|
|||||||
if ($cmd -match "^(?i)\s*/repadmin\b") {
|
if ($cmd -match "^(?i)\s*/repadmin\b") {
|
||||||
$cmd = $cmd -replace "^(?i)\s*/repadmin\b", "repadmin"
|
$cmd = $cmd -replace "^(?i)\s*/repadmin\b", "repadmin"
|
||||||
}
|
}
|
||||||
if ($UseRemote -and $cmd -match "(?i)\brepadmin\b" -and $cmd -match "(?i)\bsyncall\b") {
|
|
||||||
$remoteCmd = [regex]::Replace($cmd, "(?i)\\brepadmin\\s+/syncall\\s+\\S+", "repadmin /syncall")
|
if ($UseRemote) {
|
||||||
|
if (-not $sessions.ContainsKey($server)) {
|
||||||
|
try {
|
||||||
|
$sessions[$server] = New-PSSession -ComputerName $server -ErrorAction Stop
|
||||||
|
& $Log "Replication session opened: $server"
|
||||||
|
} catch {
|
||||||
|
& $Log ("Replication session error on {0}: {1}" -f $server, $_.Exception.Message)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$remoteCmd = $cmd.Replace("{server}", "").Trim()
|
||||||
|
$remoteCmd = [regex]::Replace($remoteCmd, "\s{2,}", " ")
|
||||||
|
if ($remoteCmd -match "(?i)\brepadmin\b" -and $remoteCmd -match "(?i)\bsyncall\b") {
|
||||||
|
$remoteCmd = [regex]::Replace($remoteCmd, "(?i)\brepadmin\s+/syncall\s+\S+", "repadmin /syncall")
|
||||||
|
}
|
||||||
|
if ([string]::IsNullOrWhiteSpace($remoteCmd)) {
|
||||||
|
& $Log "Replication skipped: empty command for $server."
|
||||||
|
continue
|
||||||
|
}
|
||||||
& $Log "Replication (remote): $server -> $remoteCmd"
|
& $Log "Replication (remote): $server -> $remoteCmd"
|
||||||
try {
|
try {
|
||||||
Invoke-Command -ComputerName $server -ScriptBlock { param($c) & $env:ComSpec /c $c } -ArgumentList $remoteCmd |
|
Invoke-Command -Session $sessions[$server] -ScriptBlock { param($c) & $env:ComSpec /c $c } -ArgumentList $remoteCmd |
|
||||||
ForEach-Object { & $Log $_ }
|
ForEach-Object { & $Log $_ }
|
||||||
} catch {
|
} catch {
|
||||||
& $Log ("Replication error on {0}: {1}" -f $server, $_.Exception.Message)
|
& $Log ("Replication error on {0}: {1}" -f $server, $_.Exception.Message)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (-not $usesToken -and $server -and $cmd -match "(?i)\brepadmin\b" -and $cmd -match "(?i)\bsyncall\b") {
|
if (-not $usesToken -and $server -and $cmd -match "(?i)\brepadmin\b" -and $cmd -match "(?i)\bsyncall\b") {
|
||||||
if ($cmd -notmatch "(?i)\\bsyncall\\s+\\S+") {
|
if ($cmd -notmatch "(?i)\\bsyncall\\s+\\S+") {
|
||||||
$cmd = $cmd -replace "(?i)\\bsyncall\\b", "syncall $server"
|
$cmd = $cmd -replace "(?i)\\bsyncall\\b", "syncall $server"
|
||||||
@@ -275,6 +297,11 @@ function Invoke-Replication {
|
|||||||
& $Log "Replication: $cmd"
|
& $Log "Replication: $cmd"
|
||||||
& $env:ComSpec /c $cmd | ForEach-Object { & $Log $_ }
|
& $env:ComSpec /c $cmd | ForEach-Object { & $Log $_ }
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
foreach ($session in $sessions.Values) {
|
||||||
|
try { Remove-PSSession -Session $session } catch {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Invoke-Wacs {
|
function Invoke-Wacs {
|
||||||
|
|||||||
Reference in New Issue
Block a user