private static ManagementScope scopeMgmt(bool test, Model.Server s) { ManagementPath mp; ManagementScope ms = null; try { ConnectionOptions co = new ConnectionOptions(); co.Impersonation = ImpersonationLevel.Impersonate; co.Authentication = AuthenticationLevel.Packet; co.Timeout = new TimeSpan(0, 0, 30); co.EnablePrivileges = true; co.Username = s.USUARIO; co.Password = s.SENHA; mp = new ManagementPath(); mp.NamespacePath = @"\root\cimv2"; mp.Server = s.IPHOST; ms = new ManagementScope(mp, co); //Se ocorrer erro com a conexão remota acima, tenta a local... ms.Connect(); } catch (ManagementException me) { if (me.ErrorCode.ToString() != "LocalCredentials") { if ((test) && (s.IPHOST.ToUpper() != "LOCALHOST")) { Console.WriteLine("|Info| Ocorreu um Erro de Gerenciamento - " + me.Message); return(null); } } else { mp = new ManagementPath(); mp.NamespacePath = @"\root\cimv2"; mp.Server = s.IPHOST; ms = new ManagementScope(mp); ms.Connect(); } } catch (Exception eg) { Console.WriteLine("|Info| Ocorreu um Erro Geral - " + eg.Message); return(null); } return(ms); }
public static string ExecProcesso(Model.Server s, ConnectionOptions oConn, string localSrv, string commandLine) { ManagementPath path2 = wmInstPath(s.IPHOST, "Win32_Process", "root\\CIMV2"); ManagementScope scopeProcess = wmInstScope(s.USUARIO, path2, wmInstConOpt(s.USUARIO, s.SENHA)); using (ManagementClass process = new ManagementClass(scopeProcess, path2, null)) { using (ManagementBaseObject inParams = process.GetMethodParameters("Create")) { inParams["CommandLine"] = commandLine; inParams["CurrentDirectory"] = localSrv;//DriveLetter + @":\\"; inParams["ProcessStartupInformation"] = null; using (ManagementBaseObject outParams = process.InvokeMethod("Create", inParams, null)) { int retVal = Convert.ToInt32(outParams.Properties["ReturnValue"].Value); return(retVal.ToString()); } } } }
public static string CriaPastaSite(string localSrv, string remoteDir, Model.Server s) { string TempName = remoteDir; int Index = TempName.IndexOf(":"); string DriveLetter = "C"; if (Index != -1) { string[] arr = TempName.Split(new char[] { ':' }); DriveLetter = arr[0]; TempName = TempName.Substring(Index + 2); } try { ManagementPath myPath = wmInstPath(s.IPHOST, "", "root\\CIMV2"); ConnectionOptions oConn = wmInstConOpt(s.USUARIO, s.SENHA); ManagementScope scope = wmInstScope(s.USUARIO, myPath, oConn); scope.Connect(); //without next strange manipulation, the os.Get().Count will throw the "Invalid query" exception remoteDir = remoteDir.Replace("\\", "\\\\"); ObjectQuery oq = new ObjectQuery("select Name from Win32_Directory where Name = '" + remoteDir + "'"); using (ManagementObjectSearcher os = new ManagementObjectSearcher(scope, oq)) { if (os.Get().Count == 0) //It don't exist, so create it! { string commandLine = String.Format(@"cmd /C mkdir {0} ", TempName); return(ExecProcesso(s, oConn, localSrv, commandLine)); } else { return("O usuário já possui um perfil neste Servidor!"); } } } catch (Exception ex) { return("Ocorreu um erro: " + ex.Source + "\nDetail: " + ex.Message); } } //OK MPS - 09/10/2014