Author Posts

March 3, 2016 at 10:38 am

Hi,

I am trying to copy a .pfx file to remote servers and then import that .pfx into IIS. The script I have come up with copies the file to the servers but throws the following errors when the script trys to implement the function on the remote computer.
Exception calling "Import" with "3" argument(s): "The specified path is invalid.
"
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CryptographicException
+ PSComputerName : MTWEBTEST1

Exception calling "Add" with "1" argument(s): "pCertContext is an invalid handle."
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CryptographicException
+ PSComputerName : MTWEBTEST1

Here is the script:

March 3, 2016 at 10:43 am

You're using a path of "\\$server\c$\SSL\$certName.pfx" in your Invoke-Command block. The $server and $certName variables don't exist in the remote session; you'd need to either use $using:server and $using:certName (if both the client and server are using PowerShell v3 or later), or define arguments and use Invoke-Command's -ArgumentList parameter (if PSv2 compatibility is needed.)

Incidentally, since you're invoking this command on the server anyway, you could just use a path of C:\SSL\$using:certName.pfx ; no need for the UNC path.

You may wind up running into another challenge, though. Certificates and private keys get imported into the user's profile, and I'm not 100% sure that the profile will be loaded to a state that you need when you use Invoke-Command. Will take some trial and error.

March 3, 2016 at 12:34 pm

Thanks Dave. I can't believe I missed that. It works!