apiVersion: v1
kind: ConfigMap

metadata:
  name: win-installation-scripts

data:
  post-install.ps1: |
    # Create Workdir
    $BasePath = "C:\Windows\Temp\Install"
    New-item $BasePath -itemtype directory

    # Enable RDP
    Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -value 0
    Enable-NetFirewallRule -DisplayGroup "Remote Desktop"

    # Add RedHat to Trusted Publisher
    $CertName = "balloon.cer"
    $ExportCert = Join-Path $BasePath -ChildPath $CertName

    $Cert = (Get-AuthenticodeSignature "e:\Balloon\2k19\amd64\balloon.sys").SignerCertificate
    $ExportType = [System.Security.Cryptography.X509Certificates.X509ContentType]::Cert

    [System.IO.File]::WriteAllBytes($ExportCert, $Cert.Export($ExportType))
    Import-Certificate -FilePath $ExportCert -CertStoreLocation Cert:\LocalMachine\TrustedPublisher

    # install Guest Agent
    msiexec /i e:\virtio-win-gt-x64.msi /qn /passive

    # install Qemu Tools (Drivers)
    msiexec /i e:\guest-agent\qemu-ga-x86_64.msi /qn /passive

    # Fix Guest Agent
    Start-Process  E:\vioserial\2k19\amd64\vioser.inf -Verb install

    # Get Cloud-init
    Set-ExecutionPolicy Unrestricted
    $Cloudinit = "CloudbaseInitSetup_Stable_x64.msi"
    $CloutinitLocaion =  Join-Path -Path "C:\windows\temp\" -ChildPath $Cloudinit
    invoke-webrequest https://www.cloudbase.it/downloads/$Cloudinit -o $CloutinitLocaion

    cmd /C start /wait msiexec /i $CloutinitLocaion /qn

    # Cleanup
    Remove-item $BasePath -Recurse

    # Remove AutoLogin
    reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 0 /f

    # Run Sysprep and Shutdown
    #
    #Push-Location "C:\Program Files\Cloudbase Solutions\Cloudbase-Init\conf\"
    #C:\Windows\System32\Sysprep\sysprep.exe /generalize /oobe /shutdown /unattend:Unattend.xml

    cmd /C 'cd "C:\Program Files\Cloudbase Solutions\Cloudbase-Init\conf\" && C:\Windows\System32\Sysprep\sysprep.exe /generalize /oobe /shutdown /unattend:Unattend.xml'


  autounattend.xml: |
    <?xml version="1.0" encoding="utf-8"?>
    <unattend xmlns="urn:schemas-microsoft-com:unattend">
        <settings pass="windowsPE">
            <component name="Microsoft-Windows-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <UserData>
                    <AcceptEula>true</AcceptEula>
                    <Organization>local</Organization>
                    <FullName>user</FullName>
                </UserData>
                <DiskConfiguration>
                    <Disk wcm:action="add">
                        <CreatePartitions>
                            <CreatePartition wcm:action="add">
                                <Order>1</Order>
                                <Size>500</Size>
                                <Type>Primary</Type>
                            </CreatePartition>
                            <CreatePartition wcm:action="add">
    			    <Order>2</Order>
    			    <Extend>true</Extend>
                                <Type>Primary</Type>
                            </CreatePartition>
                        </CreatePartitions>
                        <ModifyPartitions>
                            <ModifyPartition wcm:action="add">
                                <Format>NTFS</Format>
                                <Label>System</Label>
                                <PartitionID>1</PartitionID>
                                <TypeID>0x27</TypeID>
    			    <Order>1</Order>
                            </ModifyPartition>
                            <ModifyPartition wcm:action="add">
                                <Letter>C</Letter>
                                <Order>2</Order>
                                <PartitionID>2</PartitionID>
                                <Format>NTFS</Format>
                            </ModifyPartition>
                        </ModifyPartitions>
                        <DiskID>0</DiskID>
                        <WillWipeDisk>true</WillWipeDisk>
                    </Disk>
                </DiskConfiguration>
                <ImageInstall>
                    <OSImage>
                        <InstallFrom>
                            <MetaData wcm:action="add">
                                <Key>/IMAGE/INDEX</Key>
                                <Value>2</Value>
                            </MetaData>
                        </InstallFrom>
                        <InstallTo>
                            <DiskID>0</DiskID>
                            <PartitionID>2</PartitionID>
                        </InstallTo>
                    </OSImage>
                </ImageInstall>
            </component>
            <component name="Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <SetupUILanguage>
                    <UILanguage>en-us</UILanguage>
                    <WillShowUI>Always</WillShowUI>
                </SetupUILanguage>
                <InputLocale>en-us</InputLocale>
                <SystemLocale>en-us</SystemLocale>
                <UILanguage>en-us</UILanguage>
                <UserLocale>en-us</UserLocale>
            </component>
            <component name="Microsoft-Windows-PnpCustomizationsWinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <DriverPaths>
                    <PathAndCredentials wcm:action="add" wcm:keyValue="1">
                        <Path>E:\viostor\2k19\amd64</Path>
                    </PathAndCredentials>
                    <PathAndCredentials wcm:action="add" wcm:keyValue="2">
                        <Path>E:\NetKVM\2k19\amd64</Path>
                    </PathAndCredentials>
                    <PathAndCredentials wcm:action="add" wcm:keyValue="3">
                         <Path>E:\viorng\2k19\amd64</Path>
                     </PathAndCredentials>
                    </DriverPaths>
            </component>
        </settings>
        <settings pass="oobeSystem">
            <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <UserAccounts>
                    <AdministratorPassword>
                        <Value>QQBhADEAMgAzADQANQA2AEEAZABtAGkAbgBpAHMAdAByAGEAdABvAHIAUABhAHMAcwB3AG8AcgBkAA==</Value>
                        <PlainText>false</PlainText>
                    </AdministratorPassword>
    	    </UserAccounts>
    	    	<AutoLogon>
                    <Password>
                        <Value>QQBhADEAMgAzADQANQA2AFAAYQBzAHMAdwBvAHIAZAA=</Value>
                        <PlainText>false</PlainText>
                    </Password>
                    <Enabled>true</Enabled>
                    <LogonCount>1</LogonCount>
                    <Username>Administrator</Username>
    		</AutoLogon>
                <FirstLogonCommands>
                    <SynchronousCommand wcm:action="add">
    	            <CommandLine>powershell -noexit f:\post-install.ps1
    		    </CommandLine>
                        <RequiresUserInput>false</RequiresUserInput>
                        <Order>1</Order>
    		    <Description>Post Installation Script</Description>
    		</SynchronousCommand>
                </FirstLogonCommands>
            </component>
    </settings>
        <cpi:offlineImage cpi:source="wim:c:/users/administrator/documents/install.wim#Windows Server 2019 SERVERSTANDARD" xmlns:cpi="urn:schemas-microsoft-com:cpi" />
    </unattend>
