2017-04-04 36 views
2

我用下面的代码实现的WinRM HTTPS侦听器,但在执行中PowerShell中的代码,我收到以下错误:创建自签名证书 - ./makecert使用PowerShell

.\makecert : The term '.\makecert' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

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

Get-Random : Parameter cannot be processed because the parameter name 'e' is ambiguous. Possible matches include: -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 
} 
+0

'$ 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我的-sr localmachine -sky交换-sp“Microsoft RSA SChannel加密提供程序”-sy 12 - #$ serial“您如何期待这起作用? – 4c74356b41

+0

为什么不使用本机powershell cmdlet?新SelfSignedCertificate? – bluuf

回答

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是第一行)。它们或者需要用分号分隔(在获得随机数之后),或者用两条单独的线作为分隔线。您还可能会需要使用的完整路径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 
+0

对于最后一行工作,您需要将路径放在引号中并使用调用操作符。 –