I have the below snippet in PowerShell that is returning me the information I need for local administrators on a remote PC. I am trying to convert this code into c#, but have been having very little luck with it.
$ADMINS = get-wmiobject -computername $computername -Credential $Credential -query "select * from win32_groupuser where GroupComponent=""Win32_Group.Domain='$computername',Name='administrators'""" | % {$_.partcomponent}
I am able to get a basic wmi query working in c# as seen below : Method
public IEnumerable<CimInstance> WmiQuerier(CimSession session , string wmiquery)
{
try
{
string Namespace = @"root\cimv2";
IEnumerable<CimInstance> CimInstances = new List<CimInstance>();
CimInstances = session.QueryInstances(Namespace, "WQL", wmiquery);
//IEnumerable<CimInstance> CimInstances2 = CimInstances.SelectMany()
return CimInstances;
}
catch (Exception ex )
{
Console.WriteLine(ex.Message);
throw;
}
TEST
[Test]
public void CimQuery()
{
string querystring = "SELECT * FROM win32_groupuser";
Wmi wmi = new Wmi();
NetworkCredential mynetcred = new NetworkCredential("Domain\\User", "Password%");
CimCredential mycimCred = wmi.ConvertCred(mynetcred);
CimSession mySession = wmi.WmiConnection(testcomp, mycimCred);
IEnumerable<CimInstance> querierResults = wmi.WmiQuerier(mySession, querystring).Take(5).ToList();
Assert.IsInstanceOf<IEnumerable<CimInstance>>(querierResults);
}
}
However, When I attempt to add in any kind of Where clause like I have in the powershell code, (See my attempt below )
"SELECT * FROM win32_groupuser Where GroupComponent = \"Win32_Group.Domain='MachineName',Name='administrators' \""
I get the error
Microsoft.Management.Infrastructure.CimException: 'The WS-Management service cannot process the request. The WQL query is invalid. '
What am I doing incorrectly in my WQL string?