Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell:用户未登录时系统找不到文件_Powershell_Certificate_Certificate Store - Fatal编程技术网

PowerShell:用户未登录时系统找不到文件

PowerShell:用户未登录时系统找不到文件,powershell,certificate,certificate-store,Powershell,Certificate,Certificate Store,当我尝试使用Import PfxCertificate时,当运行脚本的用户帐户未登录时,我收到一个错误: 导入PfxCertificate:系统找不到指定的文件。(HRESULT的异常:0x80070002) 当我打开一个RDP会话到脚本在该用户帐户上运行的服务器时,它可以正常工作。奇怪的是,testpath$certfile-PathType Leaf在这两种情况下都返回true。发生了什么事 Test-Path $certfile -PathType Leaf #always true Im

当我尝试使用
Import PfxCertificate
时,当运行脚本的用户帐户未登录时,我收到一个错误:

导入PfxCertificate:系统找不到指定的文件。(HRESULT的异常:0x80070002)

当我打开一个RDP会话到脚本在该用户帐户上运行的服务器时,它可以正常工作。奇怪的是,
testpath$certfile-PathType Leaf
在这两种情况下都返回true。发生了什么事

Test-Path $certfile -PathType Leaf #always true
Import-PfxCertificate -FilePath $certfile -CertStoreLocation Cert:\CurrentUser\My -Password $Secure_String_Pwd
因此,脚本作为后台作业在服务器上的计时器上运行

案例1:用于运行脚本的用户帐户具有活动RDP会话,脚本运行正常

案例2:用于运行脚本的用户帐户没有活动的RDP会话,脚本失败

编辑:我发现错误[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

这是什么?

我在控制台上运行了您的命令,导入没有问题。我是包含控制台的计算机上的管理员。我尝试将powerShell会话提升和非提升。两者都起作用了

$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

如果这没有任何帮助,我将删除它。

试试这段代码,看看它是否有效。如果不起作用,请发布一个完整的异常

试试看
{
$Secure\u String\u Pwd=转换为SecureString'ddddd 12345'-AsPlainText-Force
导入PfxCertificate-FilePath'C:\LocalPath\local.pfx'-CertStoreLocation证书:\LocalMachine\My-Password$Secure\u String\u Pwd
$thumbprint=(Get ChildItem-Path cert:\LocalMachine\my | Where对象{$\uU3.Subject-eq“CN=certificateNameWithoutextension”})。thumbprint
#连接Azure RM帐户的代码
Connect AzurerAccount-ApplicationId$appid-CertificateThumbprint$thumbprint-Tenant$Tenant-ServicePrincipal
}
抓住
{
写入输出$_
echo$|异常|格式列表-强制

}
这没用。完全的例外是

PSParentPath:Microsoft.PowerShell.Security\Certificate::LocalMachine\My

指纹主题
--------------
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX CN=derp
Connect AzurerAccount:系统找不到指定的文件

在C:\temp\script.ps1:8 char:5 +连接AzurerAccount-ApplicationId$appid-CertificateThumbpri。。。 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:CloseError:(:)[Connect AzurerAccount],加密 图形异常 +FullyQualifiedErrorId:Microsoft.Azure.Commands.Profile.ConnectAzureRmA 帐户命令


无论如何,我使用C#解决了这个问题。

这可能不适合您的情况,但您可以使用其他证书存储:cert:\localMachine\myThis帮助我解决了这个问题。因此,我将证书存储更改为LocalMachine,它要求我授予正在运行的用户帐户管理员权限,但现在它可以工作了。唯一的问题是,它只会把同一个问题踢得更远,使问题变得更奇怪。现在我在Connect AzurerAccount中遇到了同样的错误,它甚至不使用文件!