Getting certificates details using Invoke-Command powershell gives error

Sam White 26 Reputation points
2024-06-10T14:05:33.4566667+00:00

Invoke-command -computername server1 -ScriptBlock { dir cert: -Recurse | Where-Object { $_Subject -like 'server1' }}

Gives error The system cannot open the device or file specified. Please help on how to this.

Windows for business | Windows Server | User experience | PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 61,116 Reputation points
    2024-06-10T15:18:15.1766667+00:00

    You are missing a dot between the object and Subject. You should confirm this command runs locally in PS before adding it to the script block to ensure the command is correct.

    Invoke-command -computername server1 -ScriptBlock { dir cert: -Recurse | Where-Object { $_.Subject -like '*server1*' }}
    

    For the comparison itself like is a pattern so if you want to find any value with the string in it then you need to wrap it in wildcards (*). Alternatively you can use match which uses regular expression. In this case something like -match 'server1' produces the same results because RE matching will look for any string that contains the given value.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.