From 21a5613a8a93c4c42c85ff7b2dcb3be7bfd13f0e Mon Sep 17 00:00:00 2001 From: Przemyslaw Klys Date: Tue, 9 Apr 2024 22:01:05 +0200 Subject: [PATCH] Add credential support --- .../Get-ComputerInstalledUpdates.ps1 | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Public/Computers/Get-ComputerInstalledUpdates.ps1 b/Public/Computers/Get-ComputerInstalledUpdates.ps1 index c76c3e8..cb79b3d 100644 --- a/Public/Computers/Get-ComputerInstalledUpdates.ps1 +++ b/Public/Computers/Get-ComputerInstalledUpdates.ps1 @@ -16,7 +16,8 @@ 'Servicing Stack Update', 'Other' )][string[]] $ExcludeType, - [string] $SearchKB + [string] $SearchKB, + [pscredential] $Credential ) # https://learn.microsoft.com/en-us/windows/win32/api/wuapi/ <# @@ -121,14 +122,29 @@ $Data = if ($Computers.Count -gt 0) { foreach ($Computer in $Computers) { try { - Invoke-Command -ComputerName $Computer -ScriptBlock $ScriptBlock -ErrorAction Stop | Select-Object -Property $Properties + $invokeCommandSplat = @{ + ComputerName = $Computer + ScriptBlock = $ScriptBlock + ErrorAction = 'Stop' + } + if ($Credential) { + $invokeCommandSplat.Credential = $Credential + } + Invoke-Command @invokeCommandSplat | Select-Object -Property $Properties } catch { Write-Warning -Message "Get-ComputerInstalledUpdates - No data for computer $Computer. Failed with error: $($_.Exception.Message)" } } } else { try { - Invoke-Command -ScriptBlock $ScriptBlock -ErrorAction Stop + $invokeCommandSplat = @{ + ScriptBlock = $ScriptBlock + ErrorAction = 'Stop' + } + if ($Credential) { + $invokeCommandSplat.Credential = $Credential + } + Invoke-Command @invokeCommandSplat } catch { Write-Warning -Message "Get-ComputerInstalledUpdates - No data for computer $($Env:COMPUTERNAME). Failed with error: $($_.Exception.Message)" }