使用powershell创建自签名证书 - ./makecert

时间:2017-04-04 06:23:39

标签: powershell

我使用以下代码启用WinRM https侦听器,但在执行Powershell中的代码时,我收到以下错误:

  

。\ makecert:术语'。\ makecert'不被识别为a的名称   cmdlet,函数,脚本文件或可操作程序。检查拼写   如果包含名称,或者包含路径,请验证路径是否正确   纠正,然后再试一次。

我试图提供cert.exe的完全限定路径,但这没有用。提供完全限定的路径后,我开始收到以下新错误:

  

Get-Random:由于参数名称,无法处理参数   ' E'很暧昧。可能的匹配包括:-ErrorAction   -ErrorVariable

完整代码:

function Configure-WinRMHttpsListener
{
param(
[Parameter(Mandatory = $true)]
[string] $HostName,
[Parameter(Mandatory = $true)]
[string] $port)

# Delete the WinRM Https listener if it is already configured
Delete-WinRMListener

# Create a test certificate
$thumbprint = (Get-ChildItem cert:\LocalMachine\My | Where-Object { $_.Subject -eq "CN=" + $hostname } | Select-Object -Last 1).Thumbprint
if(-not $thumbprint)
{

    #$serial = Get-Random 
    #"C:\Program Files (x86)\Windows Kits\10\bin\x64\makecert.exe" -r -pe -n CN=$hostname -b 01/01/2012 -e 01/01/2022 -eku 1.3.6.1.5.5.7.3.1 -ss my -sr localmachine -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12 -# $serial 

    #$thumbprint=(Get-ChildItem cert:\Localmachine\my | Where-Object { $_.Subject -eq "CN=" + $hostname } | Select-Object -Last 1).Thumbprint
    #C:\Program Files (x86)\Windows Kits\10\bin\x86\makecert -r -pe -n CN=$hostname -b 01/01/2012 -e 01/01/2022 -eku 1.3.6.1.5.5.7.3.1 -ss my -sr localmachine -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12


    $serial = Get-Random .\makecert -r -pe -n CN=$hostname -b 01/01/2012 -e 01/01/2022 -eku 1.3.6.1.5.5.7.3.1 -ss my -sr localmachine -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12 -# $serial

    $thumbprint=(Get-ChildItem cert:\Localmachine\my | Where-Object { $_.Subject -eq "CN=" + $hostname } | Select-Object -Last 1).Thumbprint


    if(-not $thumbprint)
    {
        throw "Failed to create the test certificate."
    }
}    

$response = cmd.exe /c .\winrmconf.cmd $hostname $thumbprint
}

1 个答案:

答案 0 :(得分:1)

$serial = Get-Random .\makecert -r -pe -n CN=$hostname -b 01/01/2012 -e 01/01/2022 -eku 1.3.6.1.5.5.7.3.1 -ss my -sr localmachine -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12 -# $serial

实际上是两行独立的代码似乎无意中被合并到一行($serial = get-random是第一行)。它们要么需要用分号分开(在随机后),要么用两个单独的线作为fllows。您可能还需要使用Makecert.exe的完整路径(或者从它所在的目录运行脚本但仍以.\makecert.exe引用它):

将上述代码更正为:

$serial = Get-Random 
& "C:\Program Files (x86)\Windows Kits\10\bin\x64\makecert.exe" -r -pe -n CN=$hostname -b 01/01/2012 -e 01/01/2022 -eku 1.3.6.1.5.5.7.3.1 -ss my -sr localmachine -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12 -# $serial