I have this script that places a variable to adjust for PS 2.0 but as my DCs are all higher, wanted to try it with a Remote Variable ($Using:<VariableName>)
I read this: about Remote Variables - PowerShell | Microsoft Docs
…but not seeing how to adapt for my particular script.
Current script:
$DCs = [DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest() | Select-Object -ExpandProperty Sites | Select-Object -ExpandProperty Servers | Select-Object -ExpandProperty Name Invoke-Command -ComputerName $DCs { $Patches = 'KB4586793', 'KB4580422', 'KB4587735' #Server 2016 Get-HotFix -Id $Patches } -ErrorAction SilentlyContinue -ErrorVariable Problem foreach ($p in $Problem) { if ($p.origininfo.pscomputername) { Write-Warning -Message "Patch not found on $($p.origininfo.pscomputername)" } elseif ($p.targetobject) { Write-Warning -Message "Unable to connect to $($p.targetobject)" } }
How would I adapt this:
$ps = "*PowerShell*" Invoke-Command -ComputerName S1 -ScriptBlock { Get-WinEvent -LogName $Using:ps }
…into my code, pulling the "$Patches = " line out of the Invoke-Command?
Would it be:
$Patches = “‘KB4586793’, ‘KB4580422’, ‘KB4587735’”
then, under Invoke-Command:
Get-HotFix -Id $Using:Patches