Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to read Registry value using PowerShell in C#
    primarykey
    data
    text
    <p>I am not able to read specific registry values using PowerShell in C#. Here is the code:</p> <p>Calling function:</p> <pre><code> public static string UserDisplayName() { // PowerShell Command: (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\SessionData\1').LoggedOnDisplayName return GetPowerShellOutputString(@"(Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\SessionData\1').LoggedOnDisplayName"); } </code></pre> <p>Function definition:</p> <pre><code> private static string GetPowerShellOutputString(string PsCmd) { try { string PsOut = string.Empty; Debug.Write("PsCmd: " + PsCmd + "; "); Runspace rs = RunspaceFactory.CreateRunspace(); rs.Open(); Pipeline pipeline = rs.CreatePipeline(); pipeline.Commands.AddScript(PsCmd); Collection&lt;PSObject&gt; results = pipeline.Invoke(); rs.Close(); foreach (PSObject obj in results) if (obj != null) PsOut += obj.ToString() + ", "; PsOut = (PsOut == string.Empty) ? strUnavailableString : PsOut.TrimEnd(',', ' '); Debug.WriteLine("PsOut: " + PsOut); return PsOut; } catch (Exception ex) { Debug.WriteLine("! " + ex.Message + ex.InnerException + "\n"); return strUnavailableString; } } </code></pre> <p>However, the same Function definition is working perfectly if I am trying to read any other registry value e.g.:</p> <pre><code> public static string UserOUPath() { try { if (UserDomain() == SystemInformation.ComputerName) return strUnavailableString; // Non-domain account //For consistant performance, grab OU from registry instead of AD. string userSID = WindowsIdentity.GetCurrent().User.ToString(); string ouPath = @"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\" + userSID; string ou = GetPowerShellOutputString("(Get-ItemProperty -Path '" + ouPath + "').'Distinguished-Name'"); ou = ou.Remove(0, ou.IndexOf(",", 0) + 1); // Drop leading CN stuff ou = ou.Remove(ou.IndexOf(",DC=", 0), ou.Length - ou.IndexOf(",DC=", 0)); // Drop trailing DC stuff ou = ou.Replace(",OU=", "/"); ou = ou.Replace("OU=", "/"); ou = FlipOU(ou); if (ou == null) throw new NullReferenceException(); return ou; } catch { return strUnavailableString; } } </code></pre> <p>For the first call (UserDisplayName()) when I did Debug mode, the <strong><em>results</em></strong> object is returning <strong><em>null</em></strong>. However if I run the same PowerShell Command in PowerShell window it is giving the value. </p> <p>I am stumbled upon this as I am not able to get why and what is happening?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload