Esempio n. 1
0
 public WMIUtility()
 {
     _settings = new RDPSettings();
     _scope    = new ManagementScope($@"\\{_settings.WMIServer}\root\sms\site_{_settings.SiteCode}");
 }
Esempio n. 2
0
        public string WOL(int resourceId, bool alwaysWake = false)
        {
            string returnMessage  = string.Empty;
            var    settings       = new RDPSettings();
            bool   deviceIsOnline = DeviceOnline(resourceId);

            if (deviceIsOnline && alwaysWake == false)
            {
                Log.Information("Device found to be online. Will not wake up.");
                return("Device is online and ready for RDP. Please run the downloaded file called Default.rdp");
            }

            var peerList = GetDevicePeers(resourceId);

            if (peerList.Count == 0)
            {
                Log.Information("Device not online and no peers found.");
                return("Device does not show online and no peers found. You can try running the Default.rdp file but the device probably can't be turned on.");
            }

            var MACList = GetDeviceMACs(resourceId);

            if (MACList.Count == 0)
            {
                Log.Information("MAC Address for device not found. This is required for WOL");
                return("Device does not show online and no MAC address was found. You can try running the Default.rdp file but the device probably can't be turned on.");
            }


            string macs = String.Join(",", MACList.ToArray());

            Dictionary <string, object> scriptInformation = null;

            var SqlParams = new Dictionary <string, object>()
            {
                { "@ScriptName", settings.CMScriptName }
            };

            using (var _sql = new SQL())
            {
                var results = _sql.Invoke(GetScriptQuery, SqlParams);
                foreach (var result in results)
                {
                    scriptInformation = result;
                }
            }

            if (scriptInformation == null)
            {
                Log.Information("Could not find WOL script");
                return("Device does not show online and no script was found to turn it on. You can try running the Default.rdp file but the device probably can't be turned on.");
            }
            string scriptParameters = $"<ScriptParameters><ScriptParameter ParameterGroupGuid=\"\" ParameterGroupName=\"PG_\" ParameterName=\"Macs\" ParameterDataType=\"System.String\" ParameterVisibility=\"0\" ParameterType=\"0\" ParameterValue=\"{macs}\"/></ScriptParameters>";
            string shaHash          = "";

            byte[] bytes = Encoding.Unicode.GetBytes(scriptParameters);
            using (SHA256 sHA = new SHA256Cng())
            {
                byte[]        a             = sHA.ComputeHash(bytes);
                StringBuilder stringBuilder = new StringBuilder();
                byte[]        a2            = a;
                foreach (byte b in a2)
                {
                    stringBuilder.Append(b.ToString("x2"));
                }
                shaHash = stringBuilder.ToString();
            }
            string scriptXML       = string.Format(CultureInfo.InvariantCulture, "<ScriptContent ScriptGuid='{0}'><ScriptVersion>{1}</ScriptVersion><ScriptType>{2}</ScriptType><ScriptHash ScriptHashAlg='SHA256'>{3}</ScriptHash>{4}<ParameterGroupHash ParameterHashAlg='SHA256'>{5}</ParameterGroupHash></ScriptContent>", scriptInformation["ScriptGuid"].ToString(), scriptInformation["ScriptVersion"].ToString(), scriptInformation["ScriptType"].ToString(), scriptInformation["ScriptHash"].ToString(), scriptParameters, shaHash);
            var    scriptXMLBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(scriptXML));
            var    wmiUtility      = new WMIUtility();
            var    wmiDictionary   = new Dictionary <string, object>();

            wmiDictionary.Add("Param", scriptXMLBase64);
            uint scriptType = 135;

            wmiDictionary.Add("Type", scriptType);
            List <uint> uintResourceIds = new List <uint>();

            foreach (var r in peerList)
            {
                uintResourceIds.Add(Convert.ToUInt32(r));
            }
            wmiDictionary.Add("TargetResourceIDs", uintResourceIds.ToArray());
            Log.Information("Attempting to run script on these resourceIds {uintResourceIds}", uintResourceIds);
            var i = wmiUtility.InvokeClassMethod("SMS_ClientOperation", "InitiateClientOperationEx", wmiDictionary);

            Log.Information("Successfully sent request to start script");
            return("Device does not show online so we are attempting to turn it on. You can run the Default.rdp file that was downloaded in 2-3 minutes to log into your computer. Thank you.");
        }
Esempio n. 3
0
 public HomeController()
 {
     _settings = new RDPSettings();
 }
 public AdminController()
 {
     _settings = new RDPSettings();
 }