WMI, Where did i go wrong

funkel1989

New member
Joined
May 23, 2017
Messages
2
Programming Experience
Beginner
I'm trying to use WMI to connect to remote PC's and run patch scripts. When I run my program I receive no errors or exceptions. When I remote the authentication and run on localhost it works. When I add the authentication back in having changed nothing it will never start the batch script on the remote PC.
I've verified the account I'm using has access and WMI is enabled on our network.
I'm not sure what I'm doing wrong. Any assistance to review my code and make sure it is correct would be great. If you know what I'm doing wrong it would be even better!
sorry for code formatting, not sure how to format code on reddit.
Remote Access Class:
C#:
public void RunOnRemotePC(string Application, string RemotePC, string Username, string Password)
        {
            try
            {
                var processToRun = new[] { Application };
                var connection = new ConnectionOptions();
                connection.Authentication = AuthenticationLevel.PacketPrivacy;
                connection.Username = Username;
                connection.Password = Password;
                var wmiScope = new ManagementScope(String.Format(@"\\{0}\root\cimv2", RemotePC), 
connection);
                var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new 
ObjectGetOptions());
                wmiProcess.InvokeMethod("Create", processToRun);
            }
            catch (ThreadAbortException tAe)
            {

                MessageBox.Show(tAe.ToString());
            }

            catch (Exception e)
             {

                 MessageBox.Show(e.ToString());
             }

        }`
Program that is running:
C#:
public partial class RemoteShortyWindow : Window
    {
        public string Application;
        public string RemotePC;
        public string Username;
        public string Password;
        public bool handle = true;

    public RemoteShortyWindow()
    {
        InitializeComponent();

        string value = "";
        if (Tmp.InputBox("Username", "Enter your dash account Username:", ref value) == System.Windows.Forms.DialogResult.OK)
        {
            Username = value;
        }

        string value2 = "";
        if (Tmp.InputBox("Password", "Enter your dash account Password:", ref value2) == System.Windows.Forms.DialogResult.OK)
        {
            Password = value2;
        }
    }        

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    private void GoButton_Click(object sender, RoutedEventArgs e)
    {
        RemotePCAccess remotepc = new RemotePCAccess();
        remotepc.RunOnRemotePC(Application, RemotePC, Username, Password);
    }

    private void ComboBox_DropDownClosed(object sender, EventArgs e)
    {
        if (handle) Handle();
        handle = true;
    }

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ComboBox cmb = sender as ComboBox;
        handle = !cmb.IsDropDownOpen;
        Handle();
    }

    private void Handle()
    {
        switch (ActionSelector.SelectedItem.ToString().Split(new string[] { ": " }, StringSplitOptions.None).Last())
        {
            case "ClearIe":
                Application = @"\\mi\dfs\Apps\Shorty\clearie-v1\clearie.cmd";
                break;
            case "ResetCTI":
                //Handle for the second combobox
                break;
            case "Reboot":
                Application = @"\\mi\dfs\Apps\Shorty\reboot.cmd";
                break;
        }
    }

    private void ComputerNameBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        RemotePC = ComputerNameBox.Text;
    }
}`
 
Back
Top Bottom