Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Winrm https support #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion scripts/package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ if(Test-PendingReboot){ Invoke-Reboot }

Write-BoxstarterMessage "Setting up winrm"
netsh advfirewall firewall add rule name="WinRM-HTTP" dir=in localport=5985 protocol=TCP action=allow
netsh advfirewall firewall add rule name="WinRM-HTTPS" dir=in localport=5986 protocol=TCP action=allow
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows Firewall change adding 5986


$enableArgs=@{Force=$true}
try {
Expand All @@ -42,4 +43,27 @@ Enable-WSManCredSSP -Force -Role Server
winrm set winrm/config/client/auth '@{Basic="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
Write-BoxstarterMessage "winrm setup complete"
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="300"}'
winrm set winrm/config '@{MaxTimeoutms="1800000"}'

Write-BoxstarterMessage "Reusing RDP self-signed certificate for WinRM HTTPS"
$SourceStoreScope = 'LocalMachine'
$SourceStorename = 'Remote Desktop'
$SourceStore = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store -ArgumentList $SourceStorename, $SourceStoreScope
$SourceStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly)
$cert = $SourceStore.Certificates | Where-Object -FilterScript {
$_.subject -like '*'
}

$DestStoreScope = 'LocalMachine'
$DestStoreName = 'My'
$DestStore = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store -ArgumentList $DestStoreName, $DestStoreScope
$DestStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$DestStore.Add($cert)

$SourceStore.Close()
$DestStore.Close()

winrm create winrm/config/listener?Address=*+Transport=HTTPS `@`{Hostname=`"($certId)`"`;CertificateThumbprint=`"($cert.Thumbprint)`"`}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool. You'd probably also have to enable 5986 on the firewall too for this to work right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, added above at line 29

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to put this added block BEFORE lines 43-45. The reason is that as soon as those lines succeed, packer is able to successfully connect and will start the shutdown process. There's a good chance it will still succeed since the shutdown process includes a sysprep and so there is still a small amount of time for the remaining code to complete, but its just safer to always have those three lines at the very end.

Write-BoxstarterMessage "winrm setup complete"
6 changes: 4 additions & 2 deletions vbox-2012r2.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"type": "virtualbox-iso",
"vboxmanage": [
[ "modifyvm", "{{.Name}}", "--natpf1", "guest_winrm,tcp,,55985,,5985" ],
[ "modifyvm", "{{.Name}}", "--natpf1", "guest_winrm_ssl,tcp,,55986,,5986" ],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a duplicate forwarding?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a second forwarding for https, so we can test both with same image.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah right. I must be getting old.

[ "modifyvm", "{{.Name}}", "--memory", "2048" ],
[ "modifyvm", "{{.Name}}", "--vram", "36" ],
[ "modifyvm", "{{.Name}}", "--cpus", "2" ]
Expand All @@ -29,14 +30,15 @@
"scripts/PackerShutdown.bat",
"scripts/package.ps1",
"scripts/SetupComplete-2012.cmd",
"scripts/test-command.ps1"
"scripts/Test-Command.ps1"
]
},
{
"name": "virtualbox-iso-atlas",
"type": "virtualbox-iso",
"vboxmanage": [
[ "modifyvm", "{{.Name}}", "--natpf1", "guest_winrm,tcp,,55985,,5985" ],
[ "modifyvm", "{{.Name}}", "--natpf1", "guest_winrm_ssl,tcp,,55986,,5986" ],
[ "modifyvm", "{{.Name}}", "--memory", "2048" ],
[ "modifyvm", "{{.Name}}", "--vram", "36" ],
[ "modifyvm", "{{.Name}}", "--cpus", "2" ]
Expand All @@ -60,7 +62,7 @@
"scripts/PackerShutdown.bat",
"scripts/package.ps1",
"scripts/SetupComplete-2012.cmd",
"scripts/test-command.ps1"
"scripts/Test-Command.ps1"
]
}
],
Expand Down