How does CPM manage Microsoft SQL Service with WMI?
Objective
How does CPM manage Microsoft SQL Service with WMI?
Environment
11.2;11.3;11.4;11.5;11.6;11.7;12.0;12.1;12.2;12.4;12.3;12.5;12.6;13.0 Privileged Access Manager (PAM, self-hosted);Privilege Cloud Central Policy Manager (CPM)
Procedure
cls
$target = "<address>" # replace <address> with the address of the target system
$service = "<service>" # replace <service> with the name of the service to manage IE. MSSQLSERVER, SQLSERVERAGENT,
# SQLBrowser, SQLWriter, MSSQLFDLauncher, MSSQLServerADHelper, msftesql, or MSSQL
$currentpass = "<currentpass>" # replace <currentpass> with the current password for the service account on the target service
$newpass = "<newpass>" # replace <newpass> with the password to set for the service account on the target service
$cred = Get-Credential
$CMs = @(Get-WmiObject -Query 'SELECT * FROM __namespace WHERE Name LIKE "ComputerManagement%"' -ComputerName $target -Credential $cred -Namespace 'root\Microsoft\SqlServer')
If ($CMs.count -gt 1) {
Write-Warning "Multiple WMI ComputerManagement Namespaces found for SqlServer: $($CMs.Count)"
}
#Loop through each namespace to query the object.
foreach ($namespace in $CMs) {
$WMIService = Get-WmiObject -Query "SELECT * from sqlservice where ServiceName = `"$service`"" -ComputerName $target -Credential $cred -Namespace "$($namespace.__NAMESPACE)\$($namespace.Name)"
If ($WMIService -eq $null) {
Write-Error "$service not found in $($namespace.__NAMESPACE)\$($namespace.Name)"
}
Else {
Write-Host "$service found in $($namespace.__NAMESPACE)\$($namespace.Name). Trying to set password."
Try {
$result = $WMIService.SetServiceAccountPassword($currentpass,$newpass)
}
Catch {
Throw ($Error[0] | Select-Object -ExpandProperty Exception | Select-Object -ExpandProperty InnerException).tostring()
}
If ($result.ReturnValue -eq 0) {
Write-Host -Object "Password Updated Successfully" -ForegroundColor Green
}
Else {
Write-Host -Object "Password NOT Updated" -ForegroundColor Red
}
break
}
}