Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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 - Fatal编程技术网

使用powershell在两台服务器之间传递安全文本

使用powershell在两台服务器之间传递安全文本,powershell,Powershell,我正在编写一个脚本,将证书从服务器a导出到PFX,复制到服务器B,然后将PFX导入到服务器。 我使用以下代码将密码从纯文本加密为安全字符串: $pfxPass = "PassW0rd" $File = "D:\backup_conf\Password.txt" [Byte[]] $key = (1..16) $pfxSecure = $pfxPass | ConvertTo-SecureString -AsPlainText -Force $pfxSecure | ConvertFrom-Sec

我正在编写一个脚本,将证书从服务器a导出到PFX,复制到服务器B,然后将PFX导入到服务器。 我使用以下代码将密码从纯文本加密为安全字符串:

$pfxPass = "PassW0rd"
$File = "D:\backup_conf\Password.txt"
[Byte[]] $key = (1..16)
$pfxSecure = $pfxPass | ConvertTo-SecureString -AsPlainText -Force
$pfxSecure | ConvertFrom-SecureString -key $key | Out-File $File
dir Cert:\LocalMachine\my | Where-Object { $_.NotAfter -clike "*2019*"} | % { certutil.exe -f -exportPFX -p $pfxSecure $_.Thumbprint D:\backup_conf\certificates\$($_.Thumbprint).pfx }
PFX已创建,我可以看到Password.txt包含哈希字符串。 PFX和Password.txt被复制到新服务器上,我使用了以下代码进行导入:

$file = "D:\backup_conf\Password.txt"
[Byte[]] $key = (1..16)
$pfxSecure = Get-Content $File | ConvertTo-SecureString -Key $key
$certs = (Get-ChildItem -recurse -Path  "D:\backup_conf\certificates" -Include *.pfx)|%{Import-PfxCertificate $_.FullName -Exportable -Password $pfxSecure -CertStoreLocation Cert:\LocalMachine\My }
此操作失败,错误如下:

导入PfxCertificate:您尝试导入的PFX文件需要不同的密码或活动帐户中的成员身份 它受保护的目录主体


知道它为什么不工作吗?

当您在这里使用
SecureString
对象时:
certutil.exe-f-exportPFX-p$pfxSecure
无法正确识别它。密码将按字面意思

System.Security.SecureString
对于cmd应用程序,
SecureString
不是它可以处理的东西,因此您必须将其转换为纯文本,然后将其传递到
certutil.exe

示例如何将
SecureString
转换为纯文本():


这里使用的是
SecureString
对象:
certutil.exe-f-exportPFX-p$pfxSecure
。密码的字面意思是
System.Security.SecureString
。对于cmd
SecureString
,它无法处理。谢谢。但是,我理解,我无法将纯文本传递给certutil.exe。我被迫将其转换为SecureString。你知道我该怎么做吗?据我所知,
certutil.exe
将不接受
SecureString
,因此您必须在运行它之前将其转换回纯文本()。谢谢,我最终按照建议从服务器A删除了SecureString,并使用certutil导入pfx,而不在服务器B上转换为SecureString。工作正常。很高兴听到这个消息!我会把它转换成一个答案,如果你愿意,你可以,如果你不介意的话。
$SecurePassword = ConvertTo-SecureString $pfxPass 
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
$UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)