---

- name: recompile .NET assemblies
  win_dotnet_ngen:

#- name: enable custom answer file
#  win_regedit:
#    path: HKLM:\System\Setup
#    name: UnattendFile
#    data: C:\Windows\system32\sysprep\unattend.xml
#    type: string

- name: ensure Panther directory exists
  win_file:
    path: c:\Windows\Panther
    state: directory

- name: enable winrm
  win_shell: '& $([scriptblock]::Create((New-Object Net.WebClient).DownloadString("https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"))) -ForceNewSSLCert -EnableCredSSP'
  ignore_errors: yes
  when: "'Windows Server 2008' in ansible_distribution"

- name: copy unattend.xml
  win_template:
    src: unattend.xml.j2
    dest: C:\Windows\system32\sysprep\unattend.xml
  when:
    - ('VMware' not in ansible_product_name) or ('VMware' in ansible_product_name and target_vagrant | bool)
    - not target_ovirt | bool
    - not ('KubeVirt' in ansible_system_vendor | default(''))

#- name: run sysprep
#  win_shell: C:\Windows\system32\sysprep\sysprep.exe /generalize /shutdown /oobe /quiet
#  args:
#    executable: cmd
#  async: 1200
#  poll: 0

- name: create scheduled task to delete WinRM listeners and shutdown
  win_scheduled_task:
    name: sysprep-shutdown
    username: SYSTEM
    disallow_start_if_on_batteries: no
    stop_if_going_on_batteries: no
    actions:
    - path: powershell.exe
      arguments: Remove-Item -Path WSMan:\localhost\Listener\* -Recurse -Force
    - path: C:\windows\system32\sysprep\sysprep.exe
      arguments: /generalize /oobe /quiet /shutdown
  when:
    - ('VMware' not in ansible_product_name) or ('VMware' in ansible_product_name and target_vagrant | bool) or (target_ovirt | bool) or ('KubeVirt' in ansible_system_vendor | default(''))

- name: create scheduled task to delete WinRM listeners and shutdown
  win_scheduled_task:
    name: sysprep-shutdown
    username: SYSTEM
    disallow_start_if_on_batteries: no
    stop_if_going_on_batteries: no
    actions:
    - path: powershell.exe
      arguments: Remove-Item -Path WSMan:\localhost\Listener\* -Recurse -Force
    - path: shutdown.exe
      arguments: /s /t 10 /f /d p:4:1 /c "Ansible Shutdown"
  when:
    - "'VMware' in ansible_product_name"
    - not target_vagrant | bool
    - not target_ovirt | bool
    - not ('KubeVirt' in ansible_system_vendor | default(''))

- name: set flag to recreate pagefile after next sysprep
  win_shell: |
    $system = Get-WmiObject -Class Win32_ComputerSystem -EnableAllPrivileges
    if ($system -ne $null) {
        $system.AutomaticManagedPagefile = $true
        $system.Put()
    }