PS Script Not Installing RDS

Hello,

I am trying to automate building an MS RDS Server 2016. The automation of the server build works great up until the last part when it tries to install RDS. Two scripts run separately to install RDS, the first one installs the RDS feature, and then Ansible should restart the server after the feature gets installed. The first PS script below is not running when called on by the Ansible playbook and it does not restart the server as well.

First PS Script that Runs

`
Add-WindowsFeature –Name RDS-RD-Server –IncludeAllSubFeature

`

After the server bounces the next PS script to install RDS and configure the deployment.

Enter code here...$servername=(Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain

Import-Module RemoteDesktop

New-RDSessionDeployment -ConnectionBroker “$servername” -WebAccessServer “$servername” -SessionHost “$servername”

New-RDSessionCollection -CollectionName “LTS” -SessionHost “$servername” -ConnectionBroker “$servername”

Set-RDSessionCollectionConfiguration -CollectionName “LTS” -UserGroup “directory\RDS-Users” -ClientDeviceRedirectionOptions 0x0000 -ClientPrinterRedirected $False

Both scripts get copied to a temp folder on the server correctly via Ansible. Both scripts work perfectly when I run them from the PS editor but when I try to use Ansible they do not work. The 2nd script should take 5-7 minutes to install RDS, it finishes within seconds I am starting to think it is the way the scripts are executing since it works perfectly in the PS editor but does not work when being called on by Ansible. Any suggestions on how to troubleshoot further?

Here is the Main.yml file to install RDS

`

  • name: Copy Windows Feature installation script
    win_copy:
    src: TS_Script1.ps1
    dest: c:\temp\TS_Script1.ps1

  • name: Install Windows Feature installation script
    win_shell: c:\temp\TS_Script1.ps1
    register: result

  • name: Reboot from Windows feature install
    win_reboot:
    when: result == “Completed”

#Wait for the machine to reboot from installing the windows features

  • name: Waiting for reboot
    local_action: shell ansible -u {{ansible_user_id}} -m ping {{ inventory_hostname }}
    register: result
    until: result.rc == 0
    retries: 30
    delay: 10

  • name: Copy RDS installation script
    win_copy:
    src: TS_Script2_Install_RDS.ps1
    dest: c:\temp\TS_Script2_Install_RDS.ps1

  • name: Install RDS installation script
    win_shell: c:\temp\TS_Script2_Install_RDS.ps1
    register: RDSInstall

  • name: Reboot from Windows feature install
    win_reboot:
    when: RDSInstall == “Completed”

`

Thanks in Advance!!
DC

I’m not a Windows peson at all, but it looks to me a bit as if you may not be waiting long enough after the reboot? To test this, just put in a fixed (say) two minute delay to make absolutely certain; f that makes a difference you can work on fine-tuning the delay and/or detecting readiness.

The other possibility is the variables in the second script - you might like to add code to the script to output the value of “$servername” to a file, then inspect the file when the script has been run. And/or experimentally hardcode a servername just to see if it then works.

Thirdly, what if anything is in the variable “RDSInstall” after the script has been run? A debug statement in your Ansible script might help there.

Regards, K.

Do you get an error message or any output when running it through Ansible. If you do, sharing that would really help with identifying what is going on.

Some other suggestion around making your playbook a bit more Ansible-like;

  • The first script, TS_Script1.ps1 can be simplified by using win_feature. This gives you idempotency, should be slightly faster, and you don’t clutter up your role and playbook with unneeded script files

  • The waiting task is a bit weird, win_reboot runs wait_for_connection at the end which is a simple action plugin that runs the (win_)ping module until it is ready. You shouldn’t need this task at all and I don’t even think it works the way you think it may. The ping module won’t work on Windows hosts, you need win_ping but once again you shouldn’t need this at all

  • The do not need to copy the script using win_copy, just use script to execute the script stored on the controller. It has all the logic to copy the file and execute it

  • You also need to make this script idempotent and handle it being run after RDS is installed. You can look at that after you get the install working but it’s something to consider

  • The last win_reboot task won’t work as RDSInstall would be a dict and not a string. You would have to access the output through something like ‘RDSInstall.stdout_lines[0] == “Complete”’ or something like that. It’s hard to tell what you need to check to idempotently do the reboot.

Overall your playbook/role could look like;

`

`

  • name: Install RDS feature
    win_feature:
    name: RDS-RD-Server
    include_sub_features: True
    state: present
    register: feature_install

  • name: Reboot after installing feature
    win_reboot:
    when: feature_install.reboot_required

  • name: Install RDS
    script: TS_Script2_Install_RDS.ps1
    register: rds_install

The below will only work if the first thing outputted is a boolean that indicates if it was installed or not

changed_when: rds_install.stdout_lines[0]|bool == True

  • name: Reboot after RDS install
    win_reboot:

Same thing here, you would need to make sure the first output object from your script is a bool that indicates if it was installed or not

when: rds_install.stdout_lines[0]|bool == True

`

`

Hopefully this helps

Jordan

Thank you Jordan and Karl for the replies, they are much appreciated! After making changes to my script per suggestions from both of you it still seems that the PS install script is not running. I have tried hard coding the server name with no luck. I have also used Jordan’s ansible playbook with no luck as well although using the Win feature works great! I am new to ansible so still trying to figure all of this out. Still have no idea why the install ps script will not run but yet works perfectly when I run it manually.

Thanks again!!