Forum Discussion
Powershell SQLLogin CIMException (Object not found)
Hi
I am trying to add user to SQL Server using below Ansible task
- name: add users to SQL Server
ansible.windows.win_dsc:
resource_name: SqlLogin
Ensure: Present
Name: "{{ item }}"
LoginType: 'WindowsUser'
ServerName: 'LocalHost'
InstanceName: '{{ sql_server_instance_name }}'
PsDscRunAsCredential_username: '{{ mssql_username }}'
PsDscRunAsCredential_password: '{{ mssql_password }}'
loop: "{{ [sql_server_svc_account, sql_server_agt_svc_account, cluster_svc_account] | unique }}"
It fails with below error
resource = Get-CimClass -ClassName $ClassName -Namespace root\Micros ...
+ CategoryInfo : ObjectNotFound: (root\Microsoft\...on:DSC_SqlLogin:String) [Get-CimClass], CimException
+ FullyQualifiedErrorId : HRESULT 0x80041002,Microsoft.Management.Infrastructure.CimCmdlets.GetCimClassCommand
Please help to understand root cause and solution
- LeonPavesicSilver Contributor
Hi slash83,
To resolve the "CIMException (Object not found)" error when attempting to add users to SQL Server using Ansible and PowerShell DSC, follow these steps:
1. Confirm that the SQL Server DSC module is installed on the target system. You can install it using the following command:
Install-Module -Name SqlServerDsc -Force -Scope AllUsers
2. Import the SQL Server DSC module at the beginning of your Ansible playbook to make its resources accessible. This step is crucial for Ansible to interact with DSC resources effectively.
- name: Import SQL Server DSC module ansible.windows.win_shell: Import-Module -Name SqlServerDsc become: yes
3. Verify that the SQL Server instance is properly installed and running on the target system. Ensure that the sql_server_instance_name variable is correctly set in your playbook.
4. Validate that the credentials (mssql_username and mssql_password) you are using in your Ansible task have the necessary permissions to create SQL logins.
5. Enable verbose logging in your Ansible task for more detailed error information:
- name: Add users to SQL Server ansible.windows.win_dsc: resource_name: SqlLogin Ensure: Present Name: "{{ item }}" LoginType: 'WindowsUser' ServerName: 'LocalHost' InstanceName: '{{ sql_server_instance_name }}' PsDscRunAsCredential_username: '{{ mssql_username }}' PsDscRunAsCredential_password: '{{ mssql_password }}' loop: "{{ [sql_server_svc_account, sql_server_agt_svc_account, cluster_svc_account] | unique }}" environment: PSOOptions: 'IncludeDetailedScriptStackTrace'
By following these steps, you can try to add users to SQL Server using Ansible and PowerShell DSC without encountering the "Object not found" error.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)