0

I am writing a script to ultimately check a block of servers for a certificate by FriendlyName and then go back and delete them once confirmed. Right now I am just trying to get the initial check to work. Currently it is not returning any data. Can anyone help?

$ContentsPath = "C:\Servers.txt"
$Servers = Get-Content $ContentsPath
$CertDeletionFile = "C:\CertsDeleted.csv"
$Today = Get-Date

$Certificate = Read-Host -Prompt "What certificate would you like to 
REMOVE?"
write-host $Certificate

function findCert {
param ([string]$Certificate)
Invoke-Command -ComputerName $Servers -ScriptBlock {Get-Childitem -Path 
Cert:LocalMachine\My | where {$_.friendlyname -eq $Certificate } | Select- 
Object -Property FriendlyName }
}
findCert
1
  • 1
    findCert -> findCert -Certificate $certificate on the last line - otherwise the findCert function will be comparing the friendlyName to an empty string Commented Jul 18, 2018 at 22:09

1 Answer 1

1

As Mathias R. Jessen comments, your findcert function needs a certificate name as a parameter, and you aren't passing anything when you call it, so it won't run properly.

You're also trying to use a local computer variable $Certificate, on a remote computer inside an invoke-command, and the remote computer can't get to that variable across the remoting.

I've rewritten it, with $using: which is a syntax that tells PS to send the value over the remoting session, and with renamed variables so it's more clear which part is accessing which variables:

$ContentsPath = 'C:\Servers.txt'
$Servers = Get-Content -LiteralPath $ContentsPath
$CertDeletionFile = 'C:\CertsDeleted.csv'
$Today = Get-Date

$typedCertificateName = Read-Host -Prompt "What certificate would you like to 
REMOVE?"
write-host $typedCertificateName

function findCert {
    param ([string]$Certificate)

    Invoke-Command -ComputerName $Servers -ScriptBlock {

        Get-Childitem -Path  Cert:LocalMachine\My |
            where-Object {$_.friendlyname -eq $using:Certificate } |
            Select-Object -Property FriendlyName
    }
}

findCert -Certificate $typedCertificateName
Sign up to request clarification or add additional context in comments.

2 Comments

TessellatingHeckler, I tried to look up the $using syntax and got this:learn.microsoft.com/en-us/powershell/module/…, which doesn't seem to be what you used. Can you tell me where I might find more info on this solution, as I haven't quite wrapped my head around it entirely? Thanks.
I'm glad :) About using: I just found this; apparently it's called the "Using scope modifier", and is documented here - learn.microsoft.com/en-gb/powershell/module/… - under "using local variables" section

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.