question

RashiAgrawal-2328 avatar image
0 Votes"
RashiAgrawal-2328 asked RashiAgrawal-2328 commented

Method Invocation Failed

Hi,

Need help with below PS script :


Script -

param([string[]]
[string[]]$computertargets,
[string]$wsusserver = "localhost"
)
$Report = @()

             [void][reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")
             $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($wsusserver,$false)
             #$computerscope = New-Object Microsoft.UpdateServices.Administration.ComputerTargetScope
            $UpdateScope = new-object Microsoft.UpdateServices.Administration.UpdateScope; 
             $UpdateScope.ExcludedInstallationStates = [Microsoft.UpdateServices.Administration.UpdateInstallationStates]::Installed, [Microsoft.UpdateServices.Administration.UpdateInstallationStates]::NotApplicable
             $Classification = $wsus.GetUpdateClassifications() | ?{ $_.Title -eq "Security Updates" }
             $UpdateScope.Classifications.Add($Classification)

             

             $groups = $wsus.GetComputerTargetGroups() | where {$_.name -eq "x1"}
             $ComputerTargets = $groups.GetComputerTargets() 

             Write-Host "Found " $ComputerTargets.Count " computer(s)"

             $i = 0
             foreach($Computer in $ComputerTargets) {
                 $i+=1
                 Write-Host "Processing $i $($Computer.FullDomainName)..."
                 $Updates = $Computer.GetUpdateInstallationInfoPerUpdate($UpdateScope)
                 $ReportEntry = "" | Select FQDN, Critical, Important, Moderate, Low, Unknown, LastStatusTime
                 $ReportEntry.FQDN = $Computer.FullDomainName
                 $ReportEntry.Critical = 0
                 $ReportEntry.Important = 0
                 $ReportEntry.Moderate = 0
                 $ReportEntry.Low = 0
                 $ReportEntry.Unknown = 0
                 $ReportEntry.LastStatusTime = $Computer.LastReportedStatusTime
                 foreach($Update in $Updates){
                     $UpdateDetails = $Update.GetUpdate()
                     #$UpdateInstallationState = $Update.UpdateInstallationState
                     #$UpdateTitle = $UpdateDetails.Title
                     $Severity = $UpdateDetails.MsrcSeverity
                     switch($Severity){
                         'Critical' { $ReportEntry.Critical+=1 }
                         'Important' { $ReportEntry.Important+=1 }
                         'Moderate' { $ReportEntry.Moderate+=1 }
                         'Low' { $ReportEntry.Low+=1 }
                         default { $ReportEntry.Unknown+=1 }
                     }
                  
                 }
                 $Report+=$ReportEntry
             }

$Report | Format-Table
$Report | Export-Csv -Path C:\tmp\WSUSReports_OIT\WSUS_OIT_$wsusserver.csv -NoTypeInformation






Error --


Method invocation failed because [System.String] does not contain a method
named 'GetUpdateInstallationInfoPerUpdate'.
At C:\tmp\WSUSReports_OIT\WSUS_report_OIT.ps1:28 char:21
+ $Updates =
$Computer.GetUpdateInstallationInfoPerUpdate($Upd ...
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JoeGladston-8890 avatar image
0 Votes"
JoeGladston-8890 answered RichMatheisen-8856 commented

can you please verify the return value of line 13 ($groups)
In case it returns multiple groups, then the for loop can go wrong

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

There is just one group, validated that already. the script is just prompting the error for line 20.

0 Votes 0 ·

check the output of line 19. it should print fulldomainname... if this is ok
this can confirm is its string or object

Print the line inside the forloop to check if GetUpdateInstallationInfoPerUpdate is a method of $Computer
$Computer | GM

0 Votes 0 ·

He's casting the variable $computertargets as an array of strings ([string[]]$computertargets). So $computertargets no longer has the object(s), just a bunch of strings. Therefore there's no method name GetUpdateInstallationInfoPerUpdate available.

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RashiAgrawal-2328 commented

Seems pretty easy to understand. The variable "$Computer" contains a string, and a string does not have a method named "GetUpdateInstallationInfoPerUpdate".

Check the "$ComputerTargets" object and verify it's the type you expect.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

[string[]]$computertargets

Its already declared at the start of the code.

0 Votes 0 ·

So it is . . . just not in the code part of your post.

You're casting the results of the GetComputerTargets method, which returns Microsoft.UpdateServices.Administration.ComputerTargetCollection objects, to strings. Don't do that! Remove the explicit type an just leave it a a simple variable declaration -- or maybe just remove the declaration of the variable altogether and let the variable be typed by the return value from the method.


1 Vote 1 ·

It worked. Thanks Rich!

0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT rolled back

Hi,

The $ComputerTargets is already declared as string[] which has no GetUpdateInstallationInfoPerUpdate method. Why not use another variable to receive the return value in Line 12?

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.