Query and list certificates installed on remote computers using WMIobject

Below script lists installed certificates on remote computers having 'Token" in the subject stream. This works fine using Invoke-Command but I need to use Invoke-WMImethod as we don’t have PS enabled on lot of computers in domain. I have tried to replace the Invoke-command with Invoke-WMImethod in below script, but when I run it fails with this message - "A parameter cannot be found that matches parameter name ‘ScriptBlock’. I don’t know what am I missing here or I am making incorrect use of the remoting command. Would appreciate if someone can take a look in Forum and suggest to fix it.

# Input file
$Servers = Get-Content “C:\Users\Downloads\servers.txt”
$ErrorActionPreference = ‘Stop’
$Array = @()
$cred = Get-Credential

# Looping each server
foreach($Server in $Servers)
Write-Host Processing $Server -ForegroundColor yellow

# Checking hostname of a server provided in input file
$hostname = ([System.Net.Dns]::GetHostByName(“$Server”)).hostname

# Querying for certificates
$Certs = Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList “gpupdate.exe /force” -ComputerName $Server -Credential $cred -ScriptBlock{ Get-Childitem Cert:\LocalMachine\Root } | Where-Object {$_.Subject -Match “Token-CA”}


If($hostname -and $Certs)
Foreach($Cert in $Certs)
# Adding certificate properties and server name to object
$Object = New-Object PSObject
$Object | Add-Member Noteproperty “Server name” -Value $hostname
$Object | Add-Member Noteproperty “Certificate Subject” -Value $cert.Subject
$Object | Add-Member Noteproperty “Certificate expiration date” -Value $cert.notafter
$Object | Add-Member Noteproperty “Certificate thumbprint” -Value $cert.thumbprint

# Adding object to an array
$Array += $Object
Write-Warning “Something went wrong”

# To export to CSV
$Array | Export-Csv -Path C:\Users\Downloads\results.csv -Force -NoTypeInformation

Invoke-WMIMethod is NOT a direct replacement for Invoke-Command. Recommend you read the documentation for Invoke-WmiMethod. The error message tells you exactly what the problem is. Invoke-WMIMethod does not have a -ScriptBlock parameter. You cannot use Invoke-WmiMethod to execute a script block on a remote machine. You can only use it to call methods of WMI objects.

Also, per the docs you should consider using Invoke-CimMethod instead if possible.

Get-Help Invoke-WMIMethod

Thanks Mike R. for your response. I believe that this is a PSdrive Cert:\LocalMachine\Root folder and I have to make use of this to retrieve the certificates information. I am trying Invoke-WMImethod but I am unable to figure out which class should I use with this and what should I put after -ArgumentList. I have gone through the documentation of Invoke-WMIMethod and I am able to test some other operations such as invoking notepad.exe in remote computers. I am trying something like this but its not working, but it’s not giving me proper output. Would appreciate if you could suggest how can I correct this ?

Invoke-WmiMethod -Class win32_directory -ComputerName $computer -Credential domain\username -Name Create -ArgumentList ‘Get-ChildItem Cert:\LocalMachine\Root | Where-Object {$_.Subject -Match “Token”}’