github.com/hashicorp/packer@v1.14.3/website/content/docs/communicators/winrm.mdx (about)

     1  ---
     2  description: |
     3    The `winrm` communicator establishes a WinRM connection, letting Packer upload files or execute scripts on the machine it creates. 
     4  page_title: Establish a WinRM Connection
     5  ---
     6  
     7  # Establish a WinRM Connection
     8  
     9  This topic describes how to use the `winrm` communicator to establish a WinRM connection to the machine it creates so that Packer can perform actions, such as upload files and execute scripts.
    10  
    11  ## Introduction 
    12  
    13  Communicators are the mechanism Packer uses to upload files, execute scripts,
    14  etc. with the machine being created. The `winrm` communicator uses the
    15  Windows Remote Management protocol to do this.
    16  
    17  ## Getting Ready to Use the `winrm` Communicator
    18  
    19  The `winrm` communicator is not the default communicator, so you will always have
    20  to set the `"communicator": "winrm",` template option explicitly. In addition,
    21  you will almost always have to provide a pre-run script that enables and
    22  configures WinRM on the guest machine. This will generally be in the form of a
    23  PowerShell script or a batch file.
    24  
    25  If you are building from a brand-new and unconfigured operating system
    26  image, you will need to provide this pre-run script as part of your
    27  Autounattend.xml file, required by Windows for automatic operating system
    28  installation. If you are building in a cloud or from a pre-installed image, your
    29  method for providing this pre-run script will vary based on the builder. Please
    30  refer to each builder's documentation for more information on how to supply the
    31  winrm configuration script.
    32  
    33  If you are unfamiliar with how to use an autounattend file, take a look at our
    34  [quick guides](/packer/guides/automatic-operating-system-installs); knowing
    35  how to automatically initalize your operating system is critical for being able
    36  to successfully use Packer to build from an iso.
    37  
    38  ## `winrm` Communicator Options
    39  
    40  @include "packer-plugin-sdk/communicator/WinRM-not-required.mdx"
    41  
    42  ## Examples
    43  
    44  ### Basics of WinRM Connection
    45  
    46  Please note that WinRM is not a Packer-specific protocol. Microsoft has a great
    47  deal of documentation about WinRM. If you find after reading this guide that
    48  you are still not able to connect via WinRM, check the
    49  [Microsoft documentation](https://docs.microsoft.com/en-us/windows/win32/winrm/installation-and-configuration-for-windows-remote-management)
    50  to make sure there isn't anything you're missing.
    51  
    52  There are some steps that you will normally need to take in order for Packer
    53  to be able to connect via WinRM
    54  
    55  1. Set up a username and password that Packer to connect with.
    56  2. Make any necesary registry edits to enable remote execution
    57     (and remote execution with elevated privileges, if needed)
    58  3. Start WinRM, setting any config needed for allowing basic auth
    59  4. Open ports 5985 and/or 5986 depending on how you're connecting
    60  5. launch WinRM and set it to automatically launch when the computer restarts
    61  6. If necessary, generate a self-signed certificate or provide a real certificate
    62     to the WinRM listener.
    63  
    64  #### Configuring WinRM in VMware
    65  
    66  If you are configuring WinRM using an Autounattend.xml, the simplest way to set
    67  up WinRM is to put the configuration commands directly into the Autounattend
    68  file as shown [here](https://github.com/StefanScherer/packer-windows/blob/6e603e904e9b280eeb97f7eb542940a043954112/answer_files/2008_r2_core/Autounattend.xml#L157-L234)
    69  
    70  Instead of entering each line individually, you can also add a batch file to
    71  your autounattend that contains the commands for configuring winrm. Depending
    72  on your winrm setup, this could be a complex batch file, or a very simple one.
    73  
    74  Below is an example of how we would call a batch file from inside the
    75  Autounattend file.
    76  
    77  ```xml
    78  <FirstLogonCommands>
    79    ...
    80    <SynchronousCommand wcm:action="add">
    81        <CommandLine>cmd.exe /c a:\winrmConfig.bat</CommandLine>
    82        <Description>Configure WinRM</Description>
    83        <Order>3</Order>
    84        <RequiresUserInput>true</RequiresUserInput>
    85    </SynchronousCommand>
    86    ...
    87  </FirstLogonCommands>
    88  ```
    89  
    90  It is also possible to call PowerShell scripts in a similar manner.
    91  
    92  The winrmConfig.bat referenced above can be as simple as
    93  
    94  ```powershell
    95  rem basic config for winrm
    96  cmd.exe /c winrm quickconfig -q
    97  
    98  rem allow unencrypted traffic, and configure auth to use basic username/password auth
    99  cmd.exe /c winrm set winrm/config/service @{AllowUnencrypted="true"}
   100  cmd.exe /c winrm set winrm/config/service/auth @{Basic="true"}
   101  
   102  rem update firewall rules to open the right port and to allow remote administration
   103  cmd.exe /c netsh advfirewall firewall set rule group="remote administration" new enable=yes
   104  
   105  rem restart winrm
   106  cmd.exe /c net stop winrm
   107  cmd.exe /c net start winrm
   108  ```
   109  
   110  Please note that the above batch file is _extremely_ simplistic, and not secure.
   111  It is intended to be an example of the bare minimum configuration. Below, you'll
   112  find a more complicated example of a more secure WinRM configuration process.
   113  
   114  This batch file will only work for HTTP connections, not HTTPS, but will enable
   115  you to connect using only the username and password created earlier in the
   116  Autounattend file. The above batchfile will allow you to connect using a very
   117  simple Packer config:
   118  
   119  ```json
   120          "communicator": "winrm",
   121          "winrm_username": "packeruser",
   122          "winrm_password": "SecretPassword"
   123  ```
   124  
   125  A more complex example of a PowerShell script used for configuration can be seen
   126  below.
   127  
   128  ```powershell
   129  # A Packer config that works with this example would be:
   130  #
   131  #
   132  #    "winrm_username": "Administrator",
   133  #    "winrm_password": "SuperS3cr3t!!!",
   134  #    "winrm_insecure": true,
   135  #    "winrm_use_ssl": true
   136  #
   137  #
   138  
   139  # Create username and password
   140  net user Administrator SuperS3cr3t!!!
   141  wmic useraccount where "name='Administrator'" set PasswordExpires=FALSE
   142  
   143  Set-ExecutionPolicy Unrestricted -Scope LocalMachine -Force -ErrorAction Ignore
   144  
   145  # Don't set this before Set-ExecutionPolicy as it throws an error
   146  $ErrorActionPreference = "stop"
   147  
   148  # Remove HTTP listener
   149  Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse
   150  
   151  # Create a self-signed certificate to let ssl work
   152  $Cert = New-SelfSignedCertificate -CertstoreLocation Cert:\LocalMachine\My -DnsName "packer"
   153  New-Item -Path WSMan:\LocalHost\Listener -Transport HTTPS -Address * -CertificateThumbPrint $Cert.Thumbprint -Force
   154  
   155  # WinRM
   156  write-output "Setting up WinRM"
   157  write-host "(host) setting up WinRM"
   158  
   159  # Configure WinRM to allow unencrypted communication, and provide the
   160  # self-signed cert to the WinRM listener.
   161  cmd.exe /c winrm quickconfig -q
   162  cmd.exe /c winrm set "winrm/config/service" '@{AllowUnencrypted="true"}'
   163  cmd.exe /c winrm set "winrm/config/client" '@{AllowUnencrypted="true"}'
   164  cmd.exe /c winrm set "winrm/config/service/auth" '@{Basic="true"}'
   165  cmd.exe /c winrm set "winrm/config/client/auth" '@{Basic="true"}'
   166  cmd.exe /c winrm set "winrm/config/service/auth" '@{CredSSP="true"}'
   167  cmd.exe /c winrm set "winrm/config/listener?Address=*+Transport=HTTPS" "@{Port=`"5986`";Hostname=`"packer`";CertificateThumbprint=`"$($Cert.Thumbprint)`"}"
   168  
   169  # Make sure appropriate firewall port openings exist
   170  cmd.exe /c netsh advfirewall firewall set rule group="remote administration" new enable=yes
   171  cmd.exe /c netsh advfirewall firewall add rule name="Port 5986" dir=in action=allow protocol=TCP localport=5986 profile=any
   172  
   173  # Restart WinRM, and set it so that it auto-launches on startup.
   174  cmd.exe /c net stop winrm
   175  cmd.exe /c sc config winrm start= auto
   176  cmd.exe /c net start winrm
   177  ```
   178  
   179  Please note that having WinRM auto-launch on all start ups may not be the right
   180  choice for you, if you don't need the server to recieve WinRM connections in the
   181  future. Clean up after yourself and close unnecesary firewall ports at a final
   182  provisioning step to make sure your image is secure.
   183  
   184  #### Configuring WinRM in the Cloud
   185  
   186  Most clouds allow you to provide a configuration script that runs when the
   187  instance is launched. In AWS, this is the
   188  [user_data_file](/packer/plugins/builders/amazon/ebs#user_data_file). In Google
   189  Cloud, this is provided using the `windows-startup-script-cmd`
   190  [metadata](/packer/plugins/builders/googlecompute#metadata) tag.
   191  [Example](/packer/plugins/builders/googlecompute#windows-example)
   192  
   193  Essentially, these files are powershell or cmd scripts that configure winrm,
   194  without having to be wrapped in an Autounattend. Provide the script in the
   195  format requested by each cloud, and make sure you manually configure any
   196  firewall rules that the cloud doesn't allow you to manage internally. More
   197  specific details for each cloud can be found in the builder sections.
   198  
   199  The above examples will work in cloud prep too, but may be overkill depending on
   200  how much preconfiguration the cloud has done for you.