Add Servers to Server Manager using PowerShell

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”:
}

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

Success Expectation as it is shown:

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

What exactly am I doing wrong?

I have no experience whatsoever with windows modules, but this looks pretty clear to me:

“message”: “Cannot find path ‘C:\Users\Amitabh.Ghosh\AppData\Roaming\Microsoft\Windows\ServerManager\ServerList.xml’ because it does not exist.”,

Perhaps an escaping issue? According to
https://docs.ansible.com/ansible/latest/os_guide/windows_usage.html when yaml code is not quoted (like in your situation) the backslash does not need to be escaped.
Try without the double escapes:

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”

I guess it’s still the same thing:

Playbook:
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:

2022-12-22T16:33:26.1424400Z TASK […/roles/rds_setup : Session Based Desktop Deployment - Add Servers to Server Manager] ***
2022-12-22T16:33:26.1428096Z task path: /home/sre_admin/myagent/work/r1/a/provisioning-services/playbooks/made2manage/roles/rds_setup/tasks/main.yml:63
2022-12-22T16:33:26.1855324Z Using module file /usr/lib/python3/dist-packages/ansible_collections/ansible/windows/plugins/modules/win_powershell.ps1
2022-12-22T16:33:26.1860030Z Pipelining is enabled.
2022-12-22T16:33:26.1875537Z <10.177.38.137> ESTABLISH WINRM CONNECTION FOR USER: Cloudops_Admin on PORT 5985 TO 10.177.38.137
2022-12-22T16:33:26.2763734Z EXEC (via pipeline wrapper)
2022-12-22T16:33:29.5133486Z changed: [10.177.38.137] => {
2022-12-22T16:33:29.5134245Z “changed”: true,
2022-12-22T16:33:29.5134669Z “debug”: [],
2022-12-22T16:33:29.5135375Z “error”: [
2022-12-22T16:33:29.5135700Z {
2022-12-22T16:33:29.5136061Z “category_info”: {
2022-12-22T16:33:29.5137127Z “activity”: “Get-Item”,
2022-12-22T16:33:29.5137582Z “category”: “ObjectNotFound”,
2022-12-22T16:33:29.5137981Z “category_id”: 13,
2022-12-22T16:33:29.5138388Z “reason”: “ItemNotFoundException”,
2022-12-22T16:33:29.5138972Z “target_name”: “C:\Users\Amitabh.Ghosh\AppData\Roaming\Microsoft\Windows\ServerManager\ServerList.xml”,
2022-12-22T16:33:29.5139525Z “target_type”: “String”
2022-12-22T16:33:29.5139919Z },
2022-12-22T16:33:29.5140275Z “error_details”: null,
2022-12-22T16:33:29.5140676Z “exception”: {
2022-12-22T16:33:29.5141044Z “help_link”: null,
2022-12-22T16:33:29.5141619Z “hresult”: -2146233087,
2022-12-22T16:33:29.5142054Z “inner_exception”: null,
2022-12-22T16:33:29.5142952Z “message”: “Cannot find path ‘C:\Users\Amitabh.Ghosh\AppData\Roaming\Microsoft\Windows\ServerManager\ServerList.xml’ because it does not exist.”,
2022-12-22T16:33:29.5143625Z “source”: “System.Management.Automation”,
2022-12-22T16:33:29.5144134Z “type”: “System.Management.Automation.ItemNotFoundException”
2022-12-22T16:33:29.5144583Z },
2022-12-22T16:33:29.5145878Z “fully_qualified_error_id”: “PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand”,
2022-12-22T16:33:29.5148102Z “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\Wind …\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”,
2022-12-22T16:33:29.5149506Z “pipeline_iteration_info”: [
2022-12-22T16:33:29.5149823Z 0,
2022-12-22T16:33:29.5150108Z 1
2022-12-22T16:33:29.5150371Z ],
2022-12-22T16:33:29.5150724Z “script_stack_trace”: “at , : line 3”,
2022-12-22T16:33:29.5151281Z “target_object”: “C:\Users\Amitabh.Ghosh\AppData\Roaming\Microsoft\Windows\ServerManager\ServerList.xml”
2022-12-22T16:33:29.5151731Z }
2022-12-22T16:33:29.5152005Z ],
2022-12-22T16:33:29.5152304Z “failed_when_result”: false,
2022-12-22T16:33:29.5152838Z “host_err”: “”,
2022-12-22T16:33:29.5153549Z “host_out”: “Failed - Add Servers to Server Manager\nCannot bind argument to parameter ‘Path’ because it is null.\n”,
2022-12-22T16:33:29.5154025Z “information”: [
2022-12-22T16:33:29.5154325Z {
2022-12-22T16:33:29.5154605Z “message_data”: {
2022-12-22T16:33:29.5154943Z “BackgroundColor”: null,
2022-12-22T16:33:29.5155274Z “ForegroundColor”: {
2022-12-22T16:33:29.5155594Z “String”: “Yellow”,
2022-12-22T16:33:29.5155951Z “Type”: “System.ConsoleColor”,
2022-12-22T16:33:29.5156280Z “Value”: 14
2022-12-22T16:33:29.5156573Z },
2022-12-22T16:33:29.5157090Z “Message”: “Failed - Add Servers to Server Manager”,
2022-12-22T16:33:29.5157500Z “NoNewLine”: false
2022-12-22T16:33:29.5158470Z },
2022-12-22T16:33:29.5158849Z “source”: “Write-Host”,
2022-12-22T16:33:29.5159076Z “tags”: [
2022-12-22T16:33:29.5159298Z “PSHOST”
2022-12-22T16:33:29.5159487Z ],
2022-12-22T16:33:29.5159918Z “time_generated”: “2022-12-22T16:33:29.0534558Z”
2022-12-22T16:33:29.5160209Z },
2022-12-22T16:33:29.5160515Z {
2022-12-22T16:33:29.5160713Z “message_data”: {
2022-12-22T16:33:29.5160973Z “BackgroundColor”: null,
2022-12-22T16:33:29.5161220Z “ForegroundColor”: {
2022-12-22T16:33:29.5161457Z “String”: “Yellow”,
2022-12-22T16:33:29.5161729Z “Type”: “System.ConsoleColor”,
2022-12-22T16:33:29.5161974Z “Value”: 14
2022-12-22T16:33:29.5162169Z },
2022-12-22T16:33:29.5162667Z “Message”: “Cannot bind argument to parameter ‘Path’ because it is null.”,
2022-12-22T16:33:29.5162997Z “NoNewLine”: false
2022-12-22T16:33:29.5163216Z },
2022-12-22T16:33:29.5163552Z “source”: “Write-Host”,
2022-12-22T16:33:29.5163785Z “tags”: [
2022-12-22T16:33:29.5163997Z “PSHOST”
2022-12-22T16:33:29.5164184Z ],
2022-12-22T16:33:29.5164605Z “time_generated”: “2022-12-22T16:33:29.0690815Z”
2022-12-22T16:33:29.5164896Z }
2022-12-22T16:33:29.5165066Z ],
2022-12-22T16:33:29.5165254Z “invocation”: {
2022-12-22T16:33:29.5165483Z “module_args”: {
2022-12-22T16:33:29.5165707Z “arguments”: null,
2022-12-22T16:33:29.5165929Z “chdir”: null,
2022-12-22T16:33:29.5166163Z “creates”: null,
2022-12-22T16:33:29.5166377Z “depth”: 2,
2022-12-22T16:33:29.5166609Z “error_action”: “continue”,
2022-12-22T16:33:29.5166870Z “executable”: null,
2022-12-22T16:33:29.5167103Z “parameters”: null,
2022-12-22T16:33:29.5167329Z “removes”: null,
2022-12-22T16:33:29.5172545Z “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"
2022-12-22T16:33:29.5175101Z }
2022-12-22T16:33:29.5175292Z },
2022-12-22T16:33:29.5175478Z “output”: ,
2022-12-22T16:33:29.5175682Z “result”: {},
2022-12-22T16:33:29.5175910Z “verbose”: ,
2022-12-22T16:33:29.5176114Z “warning”:
2022-12-22T16:33:29.5176295Z }

Not sure why it can’t find the path when it is right there…

Permissions ?

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

The issue has been fixed.

Apparently, when a new VM is provisioned with Windows Server edition, the Server Manager that opens up at first logon, has no ServerList.xml to reference to, or even a Server Manager folder for that matter. The XML file comes into picture when one tries to add remote server/s onto the Server Manager of the local machine. This is a standard way to do Remote Desktop Services (RDS) deployment. Once the servers are added, the ServerList.xml file will be automatically generated at {User}\AppData\Roaming\Microsoft\Windows\ServerManager\ServerList.xml, which will show the local machine and the remote machine/s that were added to the Server Manager GUI.

The workaround I did, was create a ServerList.xml in my local repo and push to the remove server. This ServerList.xml contains only the local host.
I am then using win_powershell module to run a PS script to add the Licensing Server (Domain Controller) and Gateway Server (RD Gateway) to that ServerList.xml, worked so far without issues.