Example #1
0
        /// <summary>
        /// Handles OrgUnit.AwaitableGetOrgUnitFromSelector.
        /// Returns an empty string if the user didn't pick anything.
        /// </summary>
        /// <param name="orgUnits">Data returned from OrgUnit.GetOrgUnitFromSelector</param>
        /// <returns></returns>
        public static string HandleAwaitableGetOrgUnitFromSelector(List <OrgUnit> orgUnits)
        {
            List <string> orgSelection = GetInput.GetDataGridSelection("Pick an org!", "Click on an row to select it, or paste the full path here and press submit...", "Organizational Unit Selector", orgUnits);
            string        orgPath      = null;

            foreach (string item in orgSelection)
            {
                if (item.Contains("/"))
                {
                    orgPath = item;
                    break;
                }
            }
            if (orgPath == null || orgSelection.Contains("Click on an row to select it, or paste the full path here and press submit..."))
            {
                return("");
            }

            return(orgPath);
        }
        /// <summary>
        /// Easily get an org path. Handles all of the logic of opening the data selector window and finding which is the path.
        /// </summary>
        /// <returns>A string containing the full path to the organizational unit.</returns>
        public static string GetOrgUnitFromSelector()
        {
            List <List <string> > fixedOrgs = GAM.RunGAMCommasFixed("print orgs allfields");

            List <OrgUnit> orgUnits = new List <OrgUnit>();

            foreach (List <string> org in fixedOrgs)
            {
                if (org[0] == "orgUnitPath")
                {
                    continue;
                }

                orgUnits.Add(new OrgUnit()
                {
                    OrgUnitPath        = !String.IsNullOrEmpty(org[0]) ? org[0] : null,
                    OrgUnitName        = !String.IsNullOrEmpty(org[2]) ? (org[2].StartsWith("id:") ? "(no description provided)" : org[2]) : null,
                    OrgUnitDescription = !String.IsNullOrEmpty(org[3]) ? (org[3].StartsWith("id:") ? "(no description provided)" : org[3]) : null
                });
            }
            if (orgUnits.Count < 2)
            {
                return("There was an error getting your org units. You don't seem to have any.");
            }
            List <string> orgSelection = GetInput.GetDataGridSelection("Pick an org!", "Click on an row to select it, or paste the full path here and press submit...", "Organizational Unit Selector", orgUnits);
            string        orgPath      = null;

            foreach (string item in orgSelection)
            {
                if (item.Contains("/"))
                {
                    orgPath = item;
                }
            }
            if (orgPath == null | orgSelection.Contains("Click on an row to select it, or paste the full path here and press submit..."))
            {
                return("Either you didn't enter anything or there was an error. Nothing has been changed.");
            }

            return(orgPath);
        }
Example #3
0
        /// <summary>
        /// Takes in a GAM query, and returns a single device.
        /// </summary>
        /// <param name="query">The gam query to search for: like "user:myUser"</param>
        /// <returns></returns>
        public static BasicDeviceInfo GetDeviceByGAMQuery(string query)
        {
            List <string>            gamResults = RunGAM("print cros query \"" + query + "\" fields deviceId,lastSync,serialNumber,status,user,location,assetId,notes");
            Dictionary <string, int> fieldOrder = new Dictionary <string, int>();

            if (gamResults[0].StartsWith("deviceId"))
            {
                string[] gamFieldOrder = gamResults[0].Split(',');
                for (int i = 0; i < gamResults[0].Split(',').Length; i++)
                {
                    fieldOrder.Add(gamFieldOrder[i], i);
                }
                gamResults.RemoveAt(0);
            }
            if (gamResults.Count == 0)
            {
                return new BasicDeviceInfo()
                       {
                           Error     = true,
                           ErrorText = "Invalid query string or no results for that query string.",
                       }
            }
            ;
            List <List <string> > deviceInfos = FixCSVCommas.FixCommas(gamResults);

            if (deviceInfos.Count == 1)
            {
                if (deviceInfos[0].Count > 1)                         // using 2 if statements here because I can't check for [0] if it doesn't exist
                {
                    return(new BasicDeviceInfo()
                    {
                        DeviceId = deviceInfos[0][fieldOrder["deviceId"]],
                        LastSync = deviceInfos[0][fieldOrder["lastSync"]],
                        SerialNumber = deviceInfos[0][fieldOrder["serialNumber"]],
                        Status = deviceInfos[0][fieldOrder["status"]],
                        User = deviceInfos[0][fieldOrder["annotatedUser"]],
                        Error = false
                    });
                }
            }
            // there is more than one result
            Console.WriteLine(gamResults[0]);
            List <BasicDeviceInfo> infoObjects = new List <BasicDeviceInfo>();

            for (int i = 0; i < deviceInfos.Count; i++)
            {
                infoObjects.Add(new BasicDeviceInfo()
                {
                    DeviceId     = deviceInfos[i][fieldOrder["deviceId"]],
                    LastSync     = deviceInfos[i][fieldOrder["lastSync"]],
                    SerialNumber = deviceInfos[i][fieldOrder["serialNumber"]],
                    Status       = deviceInfos[i][fieldOrder["status"]],
                    User         = deviceInfos[i][fieldOrder["annotatedUser"]],
                    Error        = false
                });
            }
            List <string> selection = GetInput.GetDataGridSelection("Which device would you like to select?", "Click on a row or enter a Device ID or Serial Number.", "Device Selector", infoObjects);
            string        deviceId  = null;

            foreach (string item in selection)
            {
                if (IsDeviceId(item))
                {
                    deviceId = item;
                }
                break;
            }
            if (deviceId == null)
            {
                return(new BasicDeviceInfo
                {
                    ErrorText = "There was an error. You didn't pick anything.",
                    Error = true
                });
            }
            else
            {
                for (int i = 1; i < deviceInfos.Count; i++)
                {
                    if (deviceId == deviceInfos[i][0])
                    {
                        return(new BasicDeviceInfo
                        {
                            DeviceId = deviceInfos[i][fieldOrder["deviceId"]],
                            LastSync = deviceInfos[i][fieldOrder["lastSync"]],
                            SerialNumber = deviceInfos[i][fieldOrder["serialNumber"]],
                            Status = deviceInfos[i][fieldOrder["status"]],
                            User = deviceInfos[i][fieldOrder["annotatedUser"]],
                            Error = false
                        });
                    }
                }
                // we have not found a match for the device id
                return(new BasicDeviceInfo
                {
                    DeviceId = deviceId,
                    Error = true,
                    ErrorText = "No devices found for that query. (404)"
                });
            }
        }
        private void changeOuButton_Click(object sender, RoutedEventArgs e)
        {
            if (Globals.DeviceIdExists() == false)
            {
                outputField.Text = "No device ID currently in memory. Press " + submitDeviceId.Content + " then try again.";
                return;
            }
            outputField.Text = "You should see the org selector in a second...";
            //return;

            List <string>         allOrgs   = GAM.RunGAM("print orgs allfields");
            List <List <string> > fixedOrgs = FixCSVCommas.FixCommas(allOrgs);

            List <OrgUnit> orgUnits = new List <OrgUnit>();

            foreach (List <string> org in fixedOrgs)
            {
                if (org[0] == "orgUnitPath")
                {
                    continue;
                }

                orgUnits.Add(new OrgUnit()
                {
                    OrgUnitPath        = !String.IsNullOrEmpty(org[0]) ? org[0] : null,
                    OrgUnitName        = !String.IsNullOrEmpty(org[2]) ? (org[2].StartsWith("id:") ? "(no description provided)" : org[2]) : null,
                    OrgUnitDescription = !String.IsNullOrEmpty(org[3]) ? (org[3].StartsWith("id:") ? "(no description provided)" : org[3]) : null
                });
            }
            if (orgUnits.Count < 2)
            {
                outputField.Text = "There was an error getting your org units. You don't seem to have any.";
                return;
            }
            List <string> orgSelection = GetInput.GetDataGridSelection("Pick an org!", "Click on an row to select it, or paste the full path here and press submit...", "Organizational Unit Selector", orgUnits);
            string        orgPath      = null;

            foreach (string item in orgSelection)
            {
                if (item.Contains("/"))
                {
                    orgPath = item;
                }
            }
            if (orgPath == null | orgSelection.Contains("Click on an row to select it, or paste the full path here and press submit..."))
            {
                outputField.Text = "Either you didn't enter anything or there was an error. Nothing has been changed.";
                return;
            }

            string gamResult = null;

            if (Globals.DeviceId == "csv")
            {
                gamResult = GAM.RunGAMFormatted(GAM.GetGAMCSVCommand(Globals.CsvLocation, "update cros", "ou \"" + orgPath + "\""));
            }
            else
            {
                gamResult = GAM.RunGAMFormatted("update cros " + Globals.DeviceId + " ou \"" + orgPath + "\"");
            }
            outputField.Text = "Done! Your OU has been changed.";
        }