# Problem clean SPN from Ansible but works in MSTSC session

**URL:** <https://forum.ansible.com/t/problem-clean-spn-from-ansible-but-works-in-mstsc-session/36315>\
**Category:** Ansible Project\
**Created:** [February 10, 2022, 7:42am UTC](https://forum.ansible.com/t/problem-clean-spn-from-ansible-but-works-in-mstsc-session/36315 "2022-02-10T07:42:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcoussi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mcoussi/32/8056_2.png) [@mcoussi](https://forum.ansible.com/u/mcoussi)\
**Post date:** [February 10, 2022, 7:42am UTC](https://forum.ansible.com/t/problem-clean-spn-from-ansible-but-works-in-mstsc-session/36315/1 "2022-02-10T07:42:03Z")

</div>

Hi everyone,

I have a problem to clean SPN of MSSQL database instance by Ansible.

First i copy a script on the server :

- name : “{{ titre\_clean\_spn }} - Copie du fichier ps1 pour le nettoyage des SPN sur le serveur”  
win\_template:  
src: “./templates/230-SPN/clean-spn-sqlserver.j2”  
dest: ‘C:\temp\clean-spn-sqlserver.ps1’

After i execute it with an ActiveDirectory account :

# Execution du script pour le nettoyage des SPN

- name: “{{ titre\_clean\_spn }} - Execution du script pour le nettoyage des SPN”  
win\_command: powershell.exe -executionpolicy bypass -File “{{ item }}”  
with\_items:
- ‘C:/temp/clean-spn-sqlserver.ps1’

I get this error message :  
{  
“start”: “2022-02-09 04:32:01.974026”,  
“stdout”: “”,  
“cmd”: “powershell.exe -executionpolicy bypass -File "C:/temp/clean-spn-sqlserver.ps1"”,  
“stderr”: “Failed to bind to DC of domain [mydomain.fr](http://mydomain.fr), error 0x5/5 → Access is denied.\r\n\r\nFailed to bind to DC of domain [mydomain.fr](http://mydomain.fr), error 0x5/5 → Access is denied.\r\n\r\n”,  
“changed”: true,  
“rc”: 0,  
“delta”: “0:00:00.296875”,  
“end”: “2022-02-09 04:32:02.270902”,  
“stdout\_lines”: ,  
“stderr\_lines”: [  
“Failed to bind to DC of domain [mydomain.fr](http://mydomain.fr), error 0x5/5 → Access is denied.”,  
“”,  
“Failed to bind to DC of domain [mydomain.fr](http://mydomain.fr), error 0x5/5 → Access is denied.”,  
“”  
],  
“\_ansible\_no\_log”: false,  
“item”: “C:/temp/clean-spn-sqlserver.ps1”,  
“ansible\_loop\_var”: “item”,  
“\_ansible\_item\_label”: “C:/temp/clean-spn-sqlserver.ps1”  
}

When i open an MSTC session on the same server with the same ActiveDirectory account uses by Ansible, it works ..  
PS C:\Temp\> .\clean-spn-sqlserver.ps1  
Unregistering ServicePrincipalNames for CN=server01,OU=Serveurs,DC=mydomain,DC=fr MSSQLSvc/server01.mydomain.fr  
Updated object  
Unregistering ServicePrincipalNames for CN=server01,OU=Serveurs,DC=mydomain,DC=fr MSSQLSvc/server01.mydomain.fr:1433  
Updated object

For information : it is not the first time that i execute script with same account from Ansible and it works

Can you help me please,

Thank for your help,  
Matt

---

<div class="post-metadata">

**Author:** ![mcoussi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mcoussi/32/8056_2.png) [@mcoussi](https://forum.ansible.com/u/mcoussi)\
**Post date:** [February 11, 2022, 9:13am UTC](https://forum.ansible.com/t/problem-clean-spn-from-ansible-but-works-in-mstsc-session/36315/2 "2022-02-11T09:13:19Z")

</div>

Hi,

I have resolved my problem.

- name: “{{ titre\_clean\_spn }} - Execution du script pour le nettoyage des SPN”  
win\_command: powershell.exe -executionpolicy Unrestricted -NonInteractive -File “{{ item }}”  
with\_items:
- ‘C:/temp/clean-spn-sqlserver.ps1’  
vars:  
ansible\_become: yes  
ansible\_become\_method: runas  
ansible\_become\_flags: logon\_type=new\_credentials logon\_flags=netcredentials\_only  
ansible\_become\_user: ‘MYDOMAIN\usr\_ansible’  
ansible\_become\_pass: “{{ usr\_ansible\_password }}”  
no\_log: “{{ ansible\_no\_log }}”

Matt
