Add Servers to Server Manager using PowerShell

23 views
Skip to first unread message

Amitabh Ghosh

unread,
Dec 21, 2022, 11:08:24 AM12/21/22
to Ansible Development
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 <ScriptBlock>, <No file>: 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": []
}

Amitabh Ghosh

unread,
Dec 21, 2022, 11:12:02 AM12/21/22
to Ansible Development
The same command works fine when I run locally from the remote server.
PowershellSuccess.JPG

Success Expectation as it is shown:
ServerManagerXML.JPG

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

What exactly am I doing wrong?
Reply all
Reply to author
Forward
0 new messages