2

I have the following script which brings back any certificates on the local machine needed for our VPN client and shows the expiry date:

$asset = $env:COMPUTERNAME

Set-Location cert:\LocalMachine\My

Write-Host = "Asset ID:"$asset

Get-ChildItem -Recurse cert: | select subject, notafter

write-host "`n"

Read-Host "Press any key to exit..."

(Get-Host).SetShouldExit(0)

It runs perfectly on my local machine, bring back the following:

Subject                                              NotAfter                                            
-------                                              --------                                            
CN=HW008551D.hca.local                               21/07/2018 09:46:08                                

Is there a way I can run this on a remote machine, which looks at that machines certificate store rather than the local machines?

I have tried a few different methods but unable to find anything that works

1
  • Invoke-Command -computername $asset -scriptblock {Get-ChildItem -Recurse cert: | select subject, notafter} Commented Feb 15, 2017 at 15:56

1 Answer 1

1

Make sure you have the PSRemoting configured. Then you can use the Invoke-Command to get your job done.

$Computername= 'remotecomputer'
$RemoteMachine_cred =Get-Credential
Invoke-Command -ComputerName $Computername  -ScriptBlock {Get-ChildItem -Recurse cert: | select subject, notafter} -Credential $RemoteMachine_cred

Links to configure PSRemoting:

1) Use Remote Commands in PowerShell

2) Run PS Commands on Remote Computers

Note: You can put all the script inside a scriptblock and you can pass it as a value of scriptblock in single shot.

Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this. I have tested on a test network and this works great, unfortunately the way our domain is setup, I can't get it to run successfully remotely. I've created a workaround using a batch file which is run locally on the machine and this will do the trick. Thanks again.
I had following issue when I tried to read all certificates from the remote machine: windowsserver.uservoice.com/forums/301869-powershell/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.