private DataTable GetUpdateTable(string selectQuery)
        {
            var tmpTable = controlParser.ReturnUpdateTable(selectQuery);
            var row      = tmpTable.Rows[0];

            //Add Add'l info
            if (MunisUser.Number != null && MunisUser.Number != string.Empty)
            {
                row[DevicesCols.CurrentUser] = MunisUser.Name;
                row[DevicesCols.MunisEmpNum] = MunisUser.Number;
            }
            else
            {
                if (currentViewDevice.CurrentUser != CurrentUserTextBox.Text.Trim())
                {
                    row[DevicesCols.CurrentUser] = CurrentUserTextBox.Text.Trim();
                    row[DevicesCols.MunisEmpNum] = DBNull.Value;
                }
                else
                {
                    row[DevicesCols.CurrentUser] = currentViewDevice.CurrentUser;
                    row[DevicesCols.MunisEmpNum] = currentViewDevice.CurrentUserEmpNum;
                }
            }
            row[DevicesCols.SibiLinkGuid] = DataConsistency.CleanDBValue(currentViewDevice.SibiLink);
            row[DevicesCols.LastModUser]  = NetworkInfo.LocalDomainUser;
            row[DevicesCols.LastModDate]  = DateTime.Now;
            return(tmpTable);
        }
Beispiel #2
0
        public string InvokeRemotePSCommand(NetworkCredential credentials, Command pScommand)
        {
            try
            {
                var psCreds = new PSCredential(credentials.UserName, credentials.SecurePassword);

                string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
                WSManConnectionInfo connInfo = new WSManConnectionInfo(false, targetHostname, 5985, "/wsman", shellUri, psCreds);

                using (Runspace remoteRunSpace = RunspaceFactory.CreateRunspace(connInfo))
                {
                    remoteRunSpace.Open();
                    remoteRunSpace.SessionStateProxy.SetVariable("cred", psCreds);

                    using (var powerSh = PowerShell.Create())
                    {
                        powerSh.Runspace = remoteRunSpace;
                        powerSh.InvocationStateChanged -= Powershell_InvocationStateChanged;
                        powerSh.InvocationStateChanged += Powershell_InvocationStateChanged;
                        powerSh.Commands.AddCommand(pScommand);
                        CurrentPowerShellObject = powerSh;
                        Collection <PSObject> results = powerSh.Invoke();

                        StringBuilder stringBuilder = new StringBuilder();

                        foreach (var obj in results)
                        {
                            stringBuilder.AppendLine(obj.ToString());
                        }

                        remoteRunSpace.Close();
                        powerSh.Stop();
                        return(DataConsistency.CleanDBValue((stringBuilder.ToString())).ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                //Check for incorrect username/password error and rethrow a Win32Exception to be caught in the error handler.
                //Makes sure that the global admin credentials variable is cleared so that a new prompt will be shown on the next attempt. See: VerifyAdminCreds method.
                if (ex is PSRemotingTransportException)
                {
                    var transportEx = (PSRemotingTransportException)ex;
                    if (transportEx.ErrorCode == 1326)
                    {
                        throw new Win32Exception(1326);
                    }
                }
                return(ex.Message);
            }
        }
Beispiel #3
0
        public async Task <bool> InvokePowerShellSession(PowerShell session)
        {
            CurrentPowerShellObject         = session;
            session.InvocationStateChanged -= Powershell_InvocationStateChanged;
            session.InvocationStateChanged += Powershell_InvocationStateChanged;

            try
            {
                var psResults = await Task.Run(() =>
                {
                    Collection <PSObject> results = session.Invoke();
                    StringBuilder stringBuilder   = new StringBuilder();


                    foreach (var obj in results)
                    {
                        stringBuilder.AppendLine(obj.ToString());
                    }
                    return(DataConsistency.CleanDBValue((stringBuilder.ToString())).ToString());
                });

                if (!string.IsNullOrEmpty(psResults))
                {
                    if (psResults.Contains("success"))
                    {
                        return(true);
                    }
                    else
                    {
                        OtherFunctions.Message(psResults, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, "Error Running Script");
                        return(false);
                    }
                }
                else
                {
                    return(true);
                }
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                session.Runspace.Close();
                session.Runspace.Dispose();
                session.Dispose();
            }
        }
Beispiel #4
0
 public object GetDBControlValue(Control control)
 {
     if (control is TextBox)
     {
         var dbTxt = (TextBox)control;
         return(DataConsistency.CleanDBValue(dbTxt.Text));
     }
     else if (control is MaskedTextBox)
     {
         var dbMaskTxt = (MaskedTextBox)control;
         return(DataConsistency.CleanDBValue(dbMaskTxt.Text));
     }
     else if (control is DateTimePicker)
     {
         var dbDtPick = (DateTimePicker)control;
         return(dbDtPick.Value);
     }
     else if (control is ComboBox)
     {
         var dbCmb = (ComboBox)control;
         if (dbCmb.SelectedIndex > -1)
         {
             return(dbCmb.SelectedValue.ToString());
         }
         else
         {
             return(dbCmb.Text);
         }
     }
     else if (control is CheckBox)
     {
         var dbChk = (CheckBox)control;
         return(dbChk.Checked);
     }
     else
     {
         throw new Exception("Unexpected type.");
         //return null;
     }
 }