2017-04-04 54 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

對於最後一行工作,您需要將路徑放在引號中並使用調用操作符。 –