【问题标题】:Creating self signed certificate - ./makecert using powershell创建自签名证书 - ./makecert 使用 powershell
【发布时间】:2017-04-04 16:30:16
【问题描述】:

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

.\makecert : 术语 '.\makecert' 未被识别为 cmdlet、函数、脚本文件或可运行的程序。检查拼写 的名称,或者如果包含路径,请验证该路径是 正确并重试。

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

Get-Random : 参数无法处理,因为参数名称 'e' 是模棱两可的。可能的匹配项包括:-ErrorAction -错误变量

完整代码:

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
}

【问题讨论】:

  • $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 你认为这会如何工作?
  • 为什么不使用本机 powershell cmdlet 呢?新自签名证书?

标签: powershell


【解决方案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 是第一行)。它们要么需要用分号分隔(在 get-random 之后),要么放在两条单独的行中作为流。您可能还需要使用 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

【讨论】:

  • 要使最后一行起作用,您需要将路径放在引号中并使用调用运算符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-28
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
  • 2013-02-05
  • 1970-01-01
相关资源
最近更新 更多