Well, for starters, the updates are read like this:
$updatescope = New-Object Microsoft.UpdateServices.Administration.UpdateScope
In order to execute a command via Invoke-Script, something like this would work:
Param( [string]$ComputerName = "", [string]$UserName = "", [string]$Password = "" )
# We need a credential object
#######################################
function createCredentials(){
if((($env:COMPUTERNAME) -ne $ComputerName)){
# Generate Credentials Object first
$SecPasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials= New-Object System.Management.Automation.PSCredential ($Username, $secpasswd)
return $Credentials
}
else{ return "false" }
}
function getUpdates(){
$Credentials = (CreateCredentials);
# Generate CIMSession
$RemoteSession = New-PSSession -ComputerName $HostName -Credential $Credentials -Name "PRTG Scheduled Task Remote Session"
$UpdateScope = (Invoke-Command -Session $RemoteSession -ScriptBlock { New-Object Microsoft.UpdateServices.Administration.UpdateScope })
<# the output has to go here #>
if (!($RemoteSession)){
Write-Host 1":Error creating remote session.";
Remove-PSSession($RemoteSession);
exit 1;
}
Remove-PSSession($RemoteSession)
exit 0
}
# Action!
getUpdates;
Since I'm not quite sure how you need the output, have a look at this "Hey, Scripting Guy!" article, it explains how to retrieve the various updates accordingly.
Add comment