# Add Servers to Server Manager using PowerShell

**URL:** <https://forum.ansible.com/t/add-servers-to-server-manager-using-powershell/37487>\
**Category:** Ansible Developer\
**Tags:** windows\
**Created:** [December 21, 2022, 4:08pm UTC](https://forum.ansible.com/t/add-servers-to-server-manager-using-powershell/37487 "2022-12-21T16:08:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sabrthor](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sabrthor/32/4870_2.png) [@Sabrthor](https://forum.ansible.com/u/Sabrthor)\
**Post date:** [December 21, 2022, 4:08pm UTC](https://forum.ansible.com/t/add-servers-to-server-manager-using-powershell/37487/1 "2022-12-21T16:08:24Z")

</div>

Hi All,  
I am trying to run a PowerShell code as ansible task and it isn’t working. The same script when run locally from the remote server works fine.

**Code:**

- name: Session Based Desktop Deployment - Add Servers to Server Manager  
become: true  
become\_method: runas  
become\_user: “{{ DomainUser }}”  
vars:  
ansible\_become\_password: “{{ DomainPassword }}”  
ansible.windows.win\_powershell:  
script: |  
try {  
Get-Process | Where-Object {$_.ProcessName -eq “ServerManager.exe”} | Stop-Process -Force  
$file = Get-Item “$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\ServerManager\ServerList.xml”  
Copy-Item -Path $file -Destination $file-backup -Force  
$xml = [xml] (Get-Content $file )  
$newserver1 = @($xml.ServerList.ServerInfo)[0].clone()  
$newserver1.name = “{{ DomainController }}”  
$newserver1.lastUpdateTime = “$(Get-Date -Format “yyyy-MM-ddTHH:mm:ss”)”  
$newserver1.status = “1”  
$xml.ServerList.AppendChild($newserver1)  
$xml.Save($file.FullName)  
$newserver2 = @($xml.ServerList.ServerInfo)[0].clone()  
$newserver2.name = “{{ RDSServer }}”  
$newserver2.lastUpdateTime = “$(Get-Date -Format “yyyy-MM-ddTHH:mm:ss”)”  
$newserver2.status = “1”  
$xml.ServerList.AppendChild($newserver2)  
$xml.Save($file.FullName)  
}  
catch {  
Write-Host “Failed - Add Servers to Server Manager” -ForegroundColor Yellow  
Write-Host $_.Exception.Message -ForegroundColor Yellow  
}

**Error:**  
TASK […/roles/rds\_setup : Session Based Desktop Deployment - Add Servers to Server Manager] \*\*\*  
task path: /home/sre\_admin/myagent/_work/r1/a/provisioning-services/playbooks/made2manage/roles/rds\_setup/tasks/main.yml:63  
Using module file /usr/lib/python3/dist-packages/ansible\_collections/ansible/windows/plugins/modules/win\_powershell.ps1  
Pipelining is enabled.  
\<10.177.38.137\> ESTABLISH WINRM CONNECTION FOR USER: Cloudops\_Admin on PORT 5985 TO 10.177.38.137  
EXEC (via pipeline wrapper)  
changed: [10.177.38.137] =\> {  
“changed”: true,  
“debug”: [],  
“error”: [  
{  
“category\_info”: {  
“activity”: “Get-Item”,  
“category”: “ObjectNotFound”,  
“category\_id”: 13,  
“reason”: “ItemNotFoundException”,  
“target\_name”: “C:\Users\Amitabh.Ghosh\AppData\Roaming\Microsoft\Windows\ServerManager\ServerList.xml”,  
“target\_type”: “String”  
},  
“error\_details”: null,  
“exception”: {  
“help\_link”: null,  
“hresult”: -2146233087,  
“inner\_exception”: null,  
“message”: “Cannot find path ‘C:\Users\Amitabh.Ghosh\AppData\Roaming\Microsoft\Windows\ServerManager\ServerList.xml’ because it does not exist.”,  
“source”: “System.Management.Automation”,  
“type”: “System.Management.Automation.ItemNotFoundException”  
},  
“fully\_qualified\_error\_id”: “PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand”,  
“output”: “Get-Item : Cannot find path ‘C:\Users\Amitabh.Ghosh\AppData\Roaming\Microsoft\Windows\ServerManager\ServerList.xml’ \r\nbecause it does not exist.\r\nAt line:3 char:13\r\n+ $file = Get-Item "$env:USERPROFILE\\AppData\\Roaming\\Microsoft\\ …\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \r\n + CategoryInfo : ObjectNotFound: (C:\Users\Amitab…\ServerList.xml:String) [Get-Item], \r\nItemNotFoundException\r\n + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand\r\n \r\n”,  
“pipeline\_iteration\_info”: [  
0,  
1  
],  
“script\_stack\_trace”: “at , : line 3”,  
“target\_object”: “C:\Users\Amitabh.Ghosh\AppData\Roaming\Microsoft\Windows\ServerManager\ServerList.xml”  
}  
],  
“failed\_when\_result”: false,  
“host\_err”: “”,  
“host\_out”: “Failed - Add Servers to Server Manager\nCannot bind argument to parameter ‘Path’ because it is null.\n”,  
“information”: [  
{  
“message\_data”: {  
“BackgroundColor”: null,  
“ForegroundColor”: {  
“String”: “Yellow”,  
“Type”: “System.ConsoleColor”,  
“Value”: 14  
},  
“Message”: “Failed - Add Servers to Server Manager”,  
“NoNewLine”: false  
},  
“source”: “Write-Host”,  
“tags”: [  
“PSHOST”  
],  
“time\_generated”: “2022-12-21T15:20:48.4275023Z”  
},  
{  
“message\_data”: {  
“BackgroundColor”: null,  
“ForegroundColor”: {  
“String”: “Yellow”,  
“Type”: “System.ConsoleColor”,  
“Value”: 14  
},  
“Message”: “Cannot bind argument to parameter ‘Path’ because it is null.”,  
“NoNewLine”: false  
},  
“source”: “Write-Host”,  
“tags”: [  
“PSHOST”  
],  
“time\_generated”: “2022-12-21T15:20:48.4431276Z”  
}  
],  
“invocation”: {  
“module\_args”: {  
“arguments”: null,  
“chdir”: null,  
“creates”: null,  
“depth”: 2,  
“error\_action”: “continue”,  
“executable”: null,  
“parameters”: null,  
“removes”: null,  
“script”: "try {\n Get-Process | Where-Object {$.ProcessName -eq "ServerManager.exe"} | Stop-Process -Force\n $file = Get-Item "$env:USERPROFILE\\AppData\\Roaming\\Microsoft\\Windows\\ServerManager\\ServerList.xml"\n Copy-Item -Path $file -Destination $file-backup -Force\n $xml = [xml] (Get-Content $file )\n $newserver1 = @($xml.ServerList.ServerInfo)[0].clone()\n $newserver1.name = "DM2MDC01.DM2MDEV.LOCAL" \n $newserver1.lastUpdateTime = "$(Get-Date -Format "yyyy-MM-ddTHH:mm:ss")"\n $newserver1.status = "1"\n $xml.ServerList.AppendChild($newserver1)\n $xml.Save($file.FullName)\n $newserver2 = @($xml.ServerList.ServerInfo)[0].clone()\n $newserver2.name = "DM2MSAASRDS01.DM2MDEV.LOCAL" \n $newserver2.lastUpdateTime = "$(Get-Date -Format "yyyy-MM-ddTHH:mm:ss")"\n $newserver2.status = "1"\n $xml.ServerList.AppendChild($newserver2)\n $xml.Save($file.FullName)\n}\ncatch {\n Write-Host "Failed - Add Servers to Server Manager" -ForegroundColor Yellow\n Write-Host $_.Exception.Message -ForegroundColor Yellow \n}\n"  
}  
},  
“output”: ,  
“result”: {},  
“verbose”: ,  
“warning”:   
}

---

<div class="post-metadata">

**Author:** ![Sabrthor](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sabrthor/32/4870_2.png) [@Sabrthor](https://forum.ansible.com/u/Sabrthor)\
**Post date:** [December 21, 2022, 4:12pm UTC](https://forum.ansible.com/t/add-servers-to-server-manager-using-powershell/37487/2 "2022-12-21T16:12:01Z")

</div>

The same command works fine when I run locally from the remote server.

 ![PowershellSuccess.JPG](https://us1.discourse-cdn.com/ansible/original/2X/d/dfaf14b14d72cc20e43f373c14f941d1dc68f14e.jpeg)

Success Expectation as it is shown:

 ![ServerManagerXML.JPG](https://us1.discourse-cdn.com/ansible/original/2X/2/243243e7734cfedeffffa775393691c4adbf442c.jpeg)

The user is a domain administrator as well as local administrator to the remote machine.

What exactly am I doing wrong?
