2017-06-01 3 views
0

Remote-Shutdown-Befehl funktioniert nicht für Windows Embedded-Computer. Es funktioniert gut für normale Windows-Computer. Gibt es etwas Besonderes, was wir für Windows Embedded tun müssen? Ich versuche, folgenden Befehl aus meinem C# -Programm zu senden. Auch über Kommandozeile versucht.Remote-Shutdown-Befehl funktioniert nicht für Windows Embedded-Computer

shutdown /s /f /m \\192.168.100.2 /t 5 /d u:0:0 /c "The Computer is shutting down" 

-Code sieht wie folgt aus

Process proc = new Process(); 
proc.EnableRaisingEvents = false; 
proc.StartInfo.UseShellExecute = false; 
proc.StartInfo.CreateNoWindow = true; 
proc.StartInfo.FileName = "shutdown.exe"; 
proc.StartInfo.UserName = adminName; 
proc.StartInfo.Password = adminPassword; 
proc.StartInfo.Domain = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName; 

proc.StartInfo.Arguments = string.Format(@" /s /f /m \\{0} /t 5 /d u:0:0 /c ""The computer is shutting down"" ", ipAddress); 
try 
{ 
    proc.Start(); 
} 
catch(Exception ex) 
{ 
     // log 
} 
+0

Sagt Ihr Protokoll irgendetwas? –

+0

Erfassen Sie die Ausgabe dieses Befehls, damit Sie sehen können, ob eine Fehlermeldung ausgegeben wird und nicht nur "es hat nicht funktioniert"? –

+0

Funktioniert es, wenn Sie den Befehl shutdown über die Eingabeaufforderung ausführen und C# -Code überhaupt nicht enthält? Der Grund, den ich stelle, ist, dass, wenn es nicht von der Eingabeaufforderung aus funktioniert, dies eine Frage für [su] ist, da das Umbrechen eines Problems in C# -Code es nicht zu einem Programmierproblem macht. –

Antwort

0

Kommandozeilen-Tools nicht funktioniert hat und ich am Ende schriftlich Shutdown Computer folgenden Code remote. Der Code funktioniert jedoch auch für den lokalen Computer.

public void ShutdownRemotePC(string ipAddress, string adminName, string adminPassword) 
     { 
      try 
      { 
       var query = new SelectQuery("Win32_OperatingSystem"); 

       // create always a new management scope 
       // create a default one and get immediate info if connection is be possible 
       ConnectionOptions connectionOptions = new ConnectionOptions 
       { 
        Impersonation = ImpersonationLevel.Impersonate, 
        EnablePrivileges = true, 
        //changed to packet privacy as some service requires it 
        Authentication = AuthenticationLevel.PacketPrivacy, 
        Username = ipAddress + @"\" + adminName, 
        Password = adminPassword, 
        Timeout = TimeSpan.FromMilliseconds(5000) 
       }; 
       string name = @"\\" + ipAddress + @"\root\cimv2"; 
       ManagementScope managementScope = new ManagementScope(name, connectionOptions); 

       // if already connected is checked inside connect 
       managementScope.Connect(); 
       if(!managementScope.IsConnected) 
       { 
        //Shutdown Failed. Managment Scope could not be connected. 
        return; 
       } 

       using(var searcher = new ManagementObjectSearcher(managementScope, query)) 
       { 
        // impersonate the searcher if not allready done 
        searcher.Scope.Options.Impersonation = System.Management.ImpersonationLevel.Impersonate; 
        searcher.Scope.Options.EnablePrivileges = true; 

        using(ManagementObjectCollection found = searcher.Get()) 
        { 
         foreach(ManagementObject os in found) 
         { 
          // Obtain in-parameters for the method 
          using(ManagementBaseObject inParams = os.GetMethodParameters("Win32Shutdown")) 
          { 

           // Add the input parameters. 
           inParams["Flags"] = 12; 

           // Execute the method and obtain the return values. 
           using(ManagementBaseObject outParams = os.InvokeMethod("Win32Shutdown", inParams, null)) 
           { 
            if(outParams != null) 
            { 
             var result = Convert.ToInt32(outParams["returnValue"]); 
             if(result == 0) 
             { 
              Logger.LogError("Shutdown successfully."); 
             } 

            } 
           } 
          } 
         } 
        } 
       } 
      } 
      catch(Exception ex) 
      { 
       // Shutdown PC=failed. 
      } 
     } 
Verwandte Themen