Hi,
I am trying to convert pfx to pem by using Convert-PfxToPem but I get the error below:
```
PS C:\Users\user\Desktop\CA> Convert-PfxToPem -InputFile C:\Users\user\Desktop\CA\test.pfx -Password (ConvertTo-SecureString 'test' -AsPlainText -Force) -OutputFile C:\Users\user\Desktop\CA\test.pem
Key not valid for use in specified state
At C:\Users\user\Documents\WindowsPowerShell\Modules\pspki\Client\Convert-PfxToPem.ps1:137 char:21
+ } else {throw New-Object ComponentModel.Win32Exception ([Runtime.Int ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], Win32Exception
+ FullyQualifiedErrorId : Key not valid for use in specified state
```
The private key is marked as exportable.
Thanks
Comments: The problem is caused due to improper PFX import flags. In order to work around the issue for current installations, find and change the following lines in the Convert-PfxToPem.ps1 file: 1) ``` PowerShell try {$Certificate.Import($bytes,$Password,"DefaultKeySet")} ``` replace with: ``` PowerShell try {$Certificate.Import($bytes,$Password,"Exportable")} ``` 2) ``` PowerShell $header = [Runtime.InteropServices.Marshal]::PtrToStructure($ptr,[PKI.PfxTools+PUBKEYBLOBHEADERS]) ``` replace with: ``` PowerShell $header = [Runtime.InteropServices.Marshal]::PtrToStructure($ptr,[Type][PKI.PfxTools+PUBKEYBLOBHEADERS]) ```
I am trying to convert pfx to pem by using Convert-PfxToPem but I get the error below:
```
PS C:\Users\user\Desktop\CA> Convert-PfxToPem -InputFile C:\Users\user\Desktop\CA\test.pfx -Password (ConvertTo-SecureString 'test' -AsPlainText -Force) -OutputFile C:\Users\user\Desktop\CA\test.pem
Key not valid for use in specified state
At C:\Users\user\Documents\WindowsPowerShell\Modules\pspki\Client\Convert-PfxToPem.ps1:137 char:21
+ } else {throw New-Object ComponentModel.Win32Exception ([Runtime.Int ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], Win32Exception
+ FullyQualifiedErrorId : Key not valid for use in specified state
```
The private key is marked as exportable.
Thanks
Comments: The problem is caused due to improper PFX import flags. In order to work around the issue for current installations, find and change the following lines in the Convert-PfxToPem.ps1 file: 1) ``` PowerShell try {$Certificate.Import($bytes,$Password,"DefaultKeySet")} ``` replace with: ``` PowerShell try {$Certificate.Import($bytes,$Password,"Exportable")} ``` 2) ``` PowerShell $header = [Runtime.InteropServices.Marshal]::PtrToStructure($ptr,[PKI.PfxTools+PUBKEYBLOBHEADERS]) ``` replace with: ``` PowerShell $header = [Runtime.InteropServices.Marshal]::PtrToStructure($ptr,[Type][PKI.PfxTools+PUBKEYBLOBHEADERS]) ```