# PS Script Not Installing RDS

**URL:** <https://forum.ansible.com/t/ps-script-not-installing-rds/29468>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [January 18, 2019, 6:11pm UTC](https://forum.ansible.com/t/ps-script-not-installing-rds/29468 "2019-01-18T18:11:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DC1](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@DC1](https://forum.ansible.com/u/DC1)\
**Post date:** [January 18, 2019, 6:11pm UTC](https://forum.ansible.com/t/ps-script-not-installing-rds/29468/1 "2019-01-18T18:11:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [January 18, 2019, 9:29pm UTC](https://forum.ansible.com/t/ps-script-not-installing-rds/29468/2 "2019-01-18T21:29:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [January 18, 2019, 10:38pm UTC](https://forum.ansible.com/t/ps-script-not-installing-rds/29468/3 "2019-01-18T22:38:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DC1](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@DC1](https://forum.ansible.com/u/DC1)\
**Post date:** [January 22, 2019, 4:57pm UTC](https://forum.ansible.com/t/ps-script-not-installing-rds/29468/4 "2019-01-22T16:57:00Z")

</div>

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!!
