-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Session options plus progressbar fix (#177)
- Loading branch information
1 parent
c9c841b
commit 8da5161
Showing
11 changed files
with
413 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
function Invoke-Command2 { | ||
[cmdletbinding()] | ||
param( | ||
[Parameter(Mandatory)] | ||
[string]$ComputerName, | ||
[Parameter(Mandatory)] | ||
[scriptblock]$ScriptBlock, | ||
[object[]]$ArgumentList, | ||
[PSCredential]$Credential, | ||
[switch]$HideComputerName, | ||
[int]$ThrottleLimit = 32, | ||
[switch]$EnableException | ||
) | ||
if (-not (Get-Module PSFramework)) { | ||
Import-Module PSFramework 4>$null | ||
} | ||
$computer = [PSFComputer]$ComputerName | ||
|
||
Write-PSFMessage -Level Verbose -Message "Adding ErrorActon Stop to Invoke-Command and Invoke-PSFCommand" | ||
$PSDefaultParameterValues['*:ErrorAction'] = "Stop" | ||
$PSDefaultParameterValues['*:ErrorAction'] = "Stop" | ||
|
||
if ($EnableException) { | ||
$null = $PSDefaultParameterValues.Remove('*:EnableException') | ||
$PSDefaultParameterValues['*:EnableException'] = $true | ||
} else { | ||
$null = $PSDefaultParameterValues.Remove('*:EnableException') | ||
$PSDefaultParameterValues['*:EnableException'] = $false | ||
} | ||
if (-not $computer.IsLocalhost) { | ||
Write-PSFMessage -Level Verbose -Message "Computer is not localhost, adding $ComputerName to PSDefaultParameterValues" | ||
$PSDefaultParameterValues['Invoke-Command:ComputerName'] = $ComputerName | ||
} | ||
if ($Credential) { | ||
Write-PSFMessage -Level Verbose -Message "Adding Credential to Invoke-Command and Invoke-PSFCommand" | ||
$PSDefaultParameterValues['Invoke-Command:Credential'] = $Credential | ||
$PSDefaultParameterValues['Invoke-PSFCommand:Credential'] = $Credential | ||
} | ||
|
||
try { | ||
if (-not (Get-PSFConfigValue -Name PSRemoting.Sessions.Enable) -or $computer.IsLocalhost) { | ||
Write-PSFMessage -Level Verbose -Message "Sessions disabled, just using Invoke-Command" | ||
Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $ArgumentList | ||
} else { | ||
Write-PSFMessage -Level Verbose -Message "Sessions enabled, using Invoke-PSFCommand" | ||
$null = Get-PSSession | Where-Object { $PSItem.Name -eq "kbupdate-$ComputerName" -and $PSItem.State -eq "Broken" } | Remove-PSSession | ||
$session = Get-PSSession | Where-Object Name -eq "kbupdate-$ComputerName" | ||
|
||
if ($session.State -eq "Disconnected") { | ||
Write-PSFMessage -Level Verbose -Message "Session is disconnected, reconnecting" | ||
$null = $session | Connect-PSSession -ErrorAction Stop | ||
} | ||
|
||
if (-not $session) { | ||
Write-PSFMessage -Level Verbose -Message "Creating session objects" | ||
$sessionoptions = @{ | ||
IncludePortInSPN = Get-PSFConfigValue -FullName PSRemoting.PsSessionOption.IncludePortInSPN | ||
SkipCACheck = Get-PSFConfigValue -FullName PSRemoting.PsSessionOption.SkipCACheck | ||
SkipCNCheck = Get-PSFConfigValue -FullName PSRemoting.PsSessionOption.SkipCNCheck | ||
SkipRevocationCheck = Get-PSFConfigValue -FullName PSRemoting.PsSessionOption.SkipRevocationCheck | ||
} | ||
$sessionOption = New-PSSessionOption @sessionoptions | ||
|
||
$sessionparm = @{ | ||
ComputerName = $ComputerName | ||
Name = "kbupdate-$ComputerName" | ||
SessionOption = $sessionOption | ||
ErrorAction = "Stop" | ||
} | ||
if (Get-PSFConfigValue -FullName PSRemoting.PsSession.UseSSL) { | ||
$null = $sessionparm.Add("UseSSL", (Get-PSFConfigValue -FullName PSRemoting.PsSession.UseSSL)) | ||
} | ||
if (Get-PSFConfigValue -FullName PSRemoting.PsSession.Port) { | ||
$null = $sessionparm.Add("Port", (Get-PSFConfigValue -FullName PSRemoting.PsSession.Port)) | ||
} | ||
|
||
Write-PSFMessage -Level Verbose -Message "Creating new session" | ||
$session = New-PSSession @sessionparm | ||
} | ||
Write-PSFMessage -Level Verbose -Message "Connecting to session using Invoke-PSFCommand" | ||
|
||
$commandparm = @{ | ||
ComputerName = $session | ||
ScriptBlock = $ScriptBlock | ||
ArgumentList = $ArgumentList | ||
} | ||
|
||
Invoke-PSFCommand @commandparm | ||
} | ||
} catch { | ||
Stop-PSFFunction -Message "Failure on $ComputerName" -ErrorRecord $PSItem | ||
} | ||
} |
Oops, something went wrong.