When I try to use Import-PfxCertificate
I get an error when the user account I'm running the script with isn't logged on:
Import-PfxCertificate : The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
When I open an RDP session to the server the script runs at on that user account, it works without problems. The weird thing is that Test-Path $certfile -PathType Leaf
returns true in both cases. What is going on?
Test-Path $certfile -PathType Leaf #always true
Import-PfxCertificate -FilePath $certfile -CertStoreLocation Cert:\CurrentUser\My -Password $Secure_String_Pwd
So the script is run as a background job on a timer on the server and when,
case 1: user account that is used to run the script has an active RDP session, the script runs fine.
case 2: user account that is used to run the script doesn't have an active RDP session, the script fails.
Edit: I found something in error[1].
New-Item : Could not find a part of the path 'C:\Users\Default\.Azure\AzInstallationChecks.json'.
At C:\Program Files\WindowsPowerShell\Modules\AzureRM.profile\5.8.2\StartupScripts\AzureRmError.ps1:17 char:9
What's this then?
I ran your command on my console, and I had no issues with the import. I am an admin on the machine that contains my console. I tried this with the powerShell session being elevated and non-elevated. Both worked.
$certfile = "C:\temp\cert.pfx"
$secure_string_pwd = convertto-securestring -string "password" -asplaintext -force
Import-PfxCertificate -FilePath $certfile -CertStoreLocation Cert:\CurrentUser\My -Password $Secure_String_Pwd
If this is not helpful at all, I will just delete this.
Try this piece of code and see it it works. Please post a complete exception if it doesn't work.
try
{
$Secure_String_Pwd = ConvertTo-SecureString 'DDDDD12345' -AsPlainText -Force
Import-PfxCertificate -FilePath 'C:\LocalPath\local.pfx' -CertStoreLocation Cert:\LocalMachine\My -Password $Secure_String_Pwd
$thumbprint = (Get-ChildItem -Path cert:\LocalMachine\my| Where-Object {$_.Subject -eq "CN=certificateNameWithoutextension"}).Thumbprint
# code for connect-Azure RM account
Connect-AzureRmAccount -ApplicationId $appid -CertificateThumbprint $thumbprint -Tenant $tenant -ServicePrincipal
}
catch
{
Write-Output $_
echo $_.Exception|format-list -force
}
Hope it helps.
It didn't help. The full exception is
PSParentPath: Microsoft.PowerShell.Security\Certificate::LocalMachine\My
Thumbprint Subject
---------- -------
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX CN=derp
Connect-AzureRmAccount : The system cannot find the file specified.At C:\temp\script.ps1:8 char:5 + Connect-AzureRmAccount -ApplicationId $appid -CertificateThumbpri ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Connect-AzureRmAccount], Crypto graphicException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.ConnectAzureRmA ccountCommand
Anyway, I went around the problem using C#.
User contributions licensed under CC BY-SA 3.0