/// <summary> /// Get Object from PowerShell Command /// </summary> /// <param name="PSCode">PowerShell code</param> /// <param name="Reload">enforce reload</param> /// <param name="tCacheTime">custom cache time</param> /// <returns>Command results as list of PSObjects.</returns> /// <example><code>List<PSObject> lResult = base.GetObjectsFromPS("(Get-ItemProperty(\"HKLM:\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\")).$(\"PendingFileRenameOperations\")", True, new TimeSpan(0,0,30));</code></example> public List <PSObject> GetObjectsFromPS(string PSCode, bool Reload, TimeSpan tCacheTime) { List <PSObject> lResult = new List <PSObject>(); if (!bShowPSCodeOnly) { string sHash = CreateHash(PSCode); if ((Cache.Get(sHash) != null) & !Reload) { lResult = Cache.Get(sHash) as List <PSObject>; } else { foreach (PSObject obj in WSMan.RunPSScript(PSCode, remoteRunspace)) { try { lResult.Add(obj); } catch (Exception ex) { Trace.WriteLineIf(debugLevel.TraceError, ex.Message); } } Cache.Add(sHash, lResult, DateTime.Now + tCacheTime); } } //Trace the PowerShell Command tsPSCode.TraceInformation(PSCode); return(lResult); }
/// <summary> /// Gets a list of PSObjects from cache(if Reload==False) or from a given WMI namespace using a given WQL query /// </summary> /// <param name="WMINamespace">The WMI namespace.</param> /// <param name="WQLQuery">The WQL query.</param> /// <param name="Reload">Enforce reload. i.e. don't use cached results.</param> /// <param name="tCacheTime">Custom cache time.</param> /// <returns>Command results as a list of PSObjects.</returns> /// <example><code>List<PSObject> lResult = base.GetObjects(@"ROOT\CCM", "SELECT * FROM SMS_MPProxyInformation Where State = 'Active'", True, new TimeSpan(0,0,30));</code></example> public List <PSObject> GetObjects(string WMINamespace, string WQLQuery, bool Reload, TimeSpan tCacheTime) { //get-wmiobject -query "SELECT * FROM CacheInfoEx" -namespace "root\ccm\SoftMgmtAgent" List <PSObject> lResult = new List <PSObject>(); string sPSCode = string.Format("get-wmiobject -query \"{0}\" -namespace \"{1}\"", WQLQuery, WMINamespace); if (!bShowPSCodeOnly) { string sHash = CreateHash(WMINamespace + WQLQuery); if ((Cache.Get(sHash) != null) & !Reload) { lResult = Cache.Get(sHash) as List <PSObject>; } else { foreach (PSObject obj in WSMan.RunPSScript(sPSCode, remoteRunspace)) { try { lResult.Add(obj); } catch (Exception ex) { Trace.WriteLineIf(debugLevel.TraceError, ex.Message); } } Cache.Add(sHash, lResult, DateTime.Now + cacheTime); } } //Trace the PowerShell Command tsPSCode.TraceInformation(sPSCode); return(lResult); }
/// <summary> /// Gets a string from cache(if Reload==False) or from a WMI property. /// </summary> /// <param name="WMIPath">The WMI path.</param> /// <param name="ResultProperty">The name of the property you are trying to retrieve.</param> /// <param name="Reload">Enforce reload. i.e. don't use cached results.</param> /// <returns>Command results as a string.</returns> /// <example><code>string siteCode = base.GetStringFromClassMethod(@"ROOT\ccm:CCM_Client=@", "ClientVersion", True);</code></example> public string GetProperty(string WMIPath, string ResultProperty, bool Reload) { //(Get-Wmiobject -class CCM_Client -namespace 'ROOT\CCM').ClientIDChangeDate //$a=([wmi]"ROOT\ccm:SMS_Client=@").ClientVersion if (!ResultProperty.StartsWith(".")) { ResultProperty = "." + ResultProperty; } string sResult = ""; string sPSCode = string.Format("([wmi]\"{0}\"){1}", WMIPath, ResultProperty); if (!bShowPSCodeOnly) { string sHash = CreateHash(WMIPath + ResultProperty); if ((Cache.Get(sHash) != null) & !Reload) { sResult = Cache.Get(sHash) as string; } else { foreach (PSObject obj in WSMan.RunPSScript(sPSCode, remoteRunspace)) { try { sResult = obj.BaseObject.ToString(); if (obj.BaseObject.GetType() == typeof(ErrorRecord)) { Trace.TraceError(obj.ToString()); Trace.WriteLineIf(debugLevel.TraceError, obj.ToString()); tsPSCode.TraceInformation("#ERROR:" + obj.ToString()); sResult = ""; } else { Cache.Add(sHash, sResult, DateTime.Now + cacheTime); } break; } catch (Exception ex) { Trace.WriteLineIf(debugLevel.TraceError, ex.Message); } } } } //Trace the PowerShell Command tsPSCode.TraceInformation(sPSCode); return(sResult); }
/// <summary> /// Gets a string from cache(if Reload==False) or from a WMI method. /// </summary> /// <param name="WMIPath">The WMI path.</param> /// <param name="WMIMethod">The WMI method.</param> /// <param name="ResultProperty">The name of the property you are trying to retrieve.</param> /// <param name="Reload">Enforce reload. i.e. don't use cached results.</param> /// <returns>Command results as a string.</returns> /// <example><code>bool multiUser = Boolean.Parse(GetStringFromMethod(@"ROOT\ccm\ClientSDK:CCM_ClientInternalUtilities=@", "AreMultiUsersLoggedOn", "MultiUsersLoggedOn", True));</code></example> public string GetStringFromMethod(string WMIPath, string WMIMethod, string ResultProperty, bool Reload) { if (!ResultProperty.StartsWith("(")) { ResultProperty = "(" + ResultProperty + ")"; } string sResult = ""; string sPSCode = string.Format("([wmi]\"{0}\").{1}{2}", WMIPath, WMIMethod, ResultProperty); if (!bShowPSCodeOnly) { string sHash = CreateHash(WMIPath + WMIMethod + ResultProperty); if ((Cache.Get(sHash) != null) & !Reload) { sResult = Cache.Get(sHash) as string; } else { foreach (PSObject obj in WSMan.RunPSScript(sPSCode, remoteRunspace)) { try { sResult = obj.BaseObject.ToString(); if (obj.BaseObject.GetType() == typeof(ErrorRecord)) { Trace.TraceError(obj.ToString()); Trace.WriteLineIf(debugLevel.TraceError, obj.ToString()); tsPSCode.TraceInformation("#ERROR:" + obj.ToString()); sResult = ""; } else { Cache.Add(sHash, sResult, DateTime.Now + cacheTime); } break; } catch (Exception ex) { Trace.WriteLineIf(debugLevel.TraceError, ex.Message); } } } } //Trace the PowerShell Command tsPSCode.TraceInformation(sPSCode); return(sResult); }
/// <summary> /// Gets a string from cache(if Reload==False) or from a PowerShell command. /// </summary> /// <param name="PSCode">The ps code.</param> /// <param name="Reload">Enforce reload. i.e. don't use cached results.</param> /// <returns>Command results as a string.</returns> /// <example><code>string sPort = base.GetStringFromPS("(Get-ItemProperty(\"HKLM:\\SOFTWARE\\Microsoft\\CCM\")).$(\"HttpPort\")", True);</code></example> public string GetStringFromPS(string PSCode, bool Reload) { string sResult = ""; if (!bShowPSCodeOnly) { string sHash = CreateHash(PSCode); if ((Cache.Get(sHash) != null) & !Reload) { sResult = Cache.Get(sHash) as string; } else { foreach (PSObject obj in WSMan.RunPSScript(PSCode, remoteRunspace)) { try { //sResult = obj.BaseObject.ToString(); sResult = obj.ToString(); if (obj.BaseObject.GetType() == typeof(ErrorRecord)) { Trace.TraceError(obj.ToString()); Trace.WriteLineIf(debugLevel.TraceError, obj.ToString()); tsPSCode.TraceInformation("#ERROR:" + obj.ToString()); sResult = ""; } else { Cache.Add(sHash, sResult, DateTime.Now + cacheTime); } break; } catch (Exception ex) { Trace.WriteLineIf(debugLevel.TraceError, ex.Message); } } } } //Trace the PowerShell Command tsPSCode.TraceInformation(PSCode); return(sResult); }
/// <summary> /// Gets a list of PSObjects from cache(if Reload==False) or from a WMI property. /// </summary> /// <param name="WMIPath">The WMI path.</param> /// <param name="ResultProperty">The name of the property you are trying to retrieve.</param> /// <param name="Reload">Enforce reload. i.e. don't use cached results.</param> /// <returns>Command results as a list of PSObjects.</returns> /// <example><code>List<PSObject> lPSAppDts = base.GetProperties(@"ROOT\ccm\clientsdk:CCM_Application", "AppDTs", True);</code></example> public List <PSObject> GetProperties(string WMIPath, string ResultProperty, bool Reload) { //$a=([wmi]"ROOT\ccm:SMS_Client=@").ClientVersion if (!ResultProperty.StartsWith(".")) { ResultProperty = "." + ResultProperty; } List <PSObject> lResult = new List <PSObject>(); string sPSCode = string.Format("([wmi]'{0}'){1}", WMIPath, ResultProperty); if (!bShowPSCodeOnly) { string sHash = CreateHash(WMIPath + ResultProperty); if ((Cache.Get(sHash) != null) & !Reload) { lResult = Cache.Get(sHash) as List <PSObject>; } else { foreach (PSObject obj in WSMan.RunPSScript(sPSCode, remoteRunspace)) { try { lResult.Add(obj); } catch (Exception ex) { Trace.WriteLineIf(debugLevel.TraceError, ex.Message); } } Cache.Add(sHash, lResult, DateTime.Now + cacheTime); } } //Trace the PowerShell Command tsPSCode.TraceInformation(sPSCode); return(lResult); }
/// <summary> /// Gets a PSObject from cache(if Reload==False) or from a WMI class method. /// </summary> /// <param name="WMIPath">The WMI path.</param> /// <param name="WMIMethod">The WMI method.</param> /// <param name="MethodParams">The method parameters.</param> /// <param name="Reload">Enforce reload. i.e. don't use cached results.</param> /// <returns>Command results as a PSObject.</returns> /// <example><code>base.CallClassMethod(@"ROOT\ccm:SMS_Client", "TriggerSchedule", "'{00000000-0000-0000-0000-000000000001}'", True);</code></example> public PSObject CallClassMethod(string WMIPath, string WMIMethod, string MethodParams, bool Reload) { if (!MethodParams.StartsWith("(")) { MethodParams = "(" + MethodParams + ")"; } PSObject pResult = null; string sPSCode = string.Format("([wmiclass]'{0}').{1}{2}", WMIPath, WMIMethod, MethodParams); if (!bShowPSCodeOnly) { string sHash = CreateHash(WMIPath + WMIMethod + MethodParams); if ((Cache.Get(sHash) != null) & !Reload) { pResult = Cache.Get(sHash) as PSObject; } else { foreach (PSObject obj in WSMan.RunPSScript(sPSCode, remoteRunspace)) { try { pResult = obj; Cache.Add(sHash, pResult, DateTime.Now + cacheTime); break; } catch (Exception ex) { Trace.WriteLineIf(debugLevel.TraceError, ex.Message); } } } } //Trace the PowerShell Command tsPSCode.TraceInformation(sPSCode); return(pResult); }
/// <summary> /// Connects this instance. /// </summary> /// <exception cref="System.Exception">Unable to connect</exception> public void connect() { Runspace RemoteRunspace = null; if (Client != null) { Client.Dispose(); } //suppress exceptions try { WSMan.openRunspace(connectionInfo, ref RemoteRunspace); } catch (Exception ex) { Console.WriteLine(ex.Message); PSCode.TraceInformation(ex.Message); } //Check if connection was successful... if (RemoteRunspace.RunspaceStateInfo.State == RunspaceState.Opened) { //Yes, return Runspace remoteRunspace = RemoteRunspace; Client = new ccm(remoteRunspace, PSCode); //oAgentProperties = new agentProperties(remoteRunspace, PSCode); //oSoftwareDistribution = new softwareDistribution(remoteRunspace, PSCode); } else { //No, throw an execption... throw new Exception("Unable to connect"); } }
/// <summary> /// Sets a WMI property. /// </summary> /// <param name="WMIPath">The WMI path.</param> /// <param name="Property">The property.</param> /// <param name="Value">The value.</param> /// <example><code>base.SetProperty(@"ROOT\ccm:SMS_Client=@", "EnableAutoAssignment", "$True");</code></example> public void SetProperty(string WMIPath, string Property, string Value) { //$a=([wmi]"ROOT\ccm:SMS_Client=@");$a.AllowLocalAdminOverride=$false;$a.Put() string sPSCode = string.Format("$a=([wmi]\"{0}\");$a.{1}={2};$a.Put()", WMIPath, Property, Value); tsPSCode.TraceInformation(sPSCode); if (!bShowPSCodeOnly) { string sHash = CreateHash(WMIPath + "." + Property); if (Value.StartsWith("$")) { Value = Value.Remove(0, 1); } Cache.Add(sHash, Value, DateTime.Now + cacheTime); foreach (PSObject obj in WSMan.RunPSScript(sPSCode, remoteRunspace)) { try { string sResult = obj.BaseObject.ToString(); break; } catch (Exception ex) { Trace.WriteLineIf(debugLevel.TraceError, ex.Message); } } } //Trace the PowerShell Command tsPSCode.TraceInformation(sPSCode); }