Skip to content

Commit

Permalink
Session options plus progressbar fix (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee authored Sep 19, 2022
1 parent c9c841b commit 8da5161
Show file tree
Hide file tree
Showing 11 changed files with 413 additions and 171 deletions.
15 changes: 14 additions & 1 deletion kbupdate.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,17 @@ if ($internet) {
Write-PSFMessage -Level Verbose -Message "Internet connection not detected. Setting source for Get-KbUpdate to Database."
$PSDefaultParameterValues['Get-KbUpdate:Source'] = "Database"
$PSDefaultParameterValues['Save-KbUpdate:Source'] = "Database"
}
}

# Disables session caching
Set-PSFConfig -FullName PSRemoting.Sessions.Enable -Value $true -Initialize -Validation bool -Handler { } -Description 'Globally enables session caching for PowerShell remoting'

# New-PSSessionOption
Set-PSFConfig -FullName PSRemoting.PsSessionOption.IncludePortInSPN -Value $false -Initialize -Validation bool -Description 'Changes the value of -IncludePortInSPN parameter used by New-PsSessionOption which is used for kbupdate internally when working with PSRemoting.'
Set-PSFConfig -FullName PSRemoting.PsSessionOption.SkipCACheck -Value $false -Initialize -Validation bool -Description 'Changes the value of -SkipCACheck parameter used by New-PsSessionOption which is used for kbupdate internally when working with PSRemoting.'
Set-PSFConfig -FullName PSRemoting.PsSessionOption.SkipCNCheck -Value $false -Initialize -Validation bool -Description 'Changes the value of -SkipCNCheck parameter used by New-PsSessionOption which is used for kbupdate internally when working with PSRemoting.'
Set-PSFConfig -FullName PSRemoting.PsSessionOption.SkipRevocationCheck -Value $false -Initialize -Validation bool -Description 'Changes the value of -SkipRevocationCheck parameter used by New-PsSessionOption which is used for kbupdate internally when working with PSRemoting.'

# New-PSSession
Set-PSFConfig -FullName PSRemoting.PsSession.UseSSL -Value $false -Initialize -Validation bool -Description 'Changes the value of -UseSSL parameter used by New-PsSession which is used for kbupdate internally when working with PSRemoting.'
Set-PSFConfig -FullName PSRemoting.PsSession.Port -Value $null -Initialize -Validation integerpositive -Description 'Changes the -Port parameter value used by New-PsSession which is used for kbupdate internally when working with PSRemoting. Use it when you don''t work with default port number. To reset, use Set-PSFConfig -FullName PSRemoting.PsSession.Port -Value $null'
93 changes: 93 additions & 0 deletions private/Invoke-Command2.ps1
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
}
}
Loading

0 comments on commit 8da5161

Please sign in to comment.