RangerMSP Search for Accounts by Date

This first example uses a TimeSpan of 1 hour to determine the date one hour prior to the runtime of the code. The purpose is to identify the Accounts created during the last hour. 

Alternatively if it is preferred to use a fixed date, then on Line 47 change the value to be equal to a string. The syntax required of the date field in RangerMSP is in the comment on Line 47. 

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ServiceModel;

namespace DemoRangerMSPAccountByDate
{
    class Program
    {
        /*
         * Instructions
         * 
         * 1. Disable Authentication and SSL.
         * 2. Add a Service Reference to the RangerMSP Service, name the reference RangerMSPService.
         *    The uri will generally be http://localhost:11111/RangerMSP.
         * 3. With new release you may have to update the Service Reference.
         */
        static void Main(string[] args)
        {            
            RangerMSPService.AccountClient accountClient = new RangerMSPService.AccountClient();
            (accountClient.ChannelFactory.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue;
            (accountClient.ChannelFactory.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue;
            (accountClient.ChannelFactory.Endpoint.Binding as BasicHttpBinding).ReceiveTimeout = new TimeSpan(0, 10, 0);
            RangerMSPService.Select select = new RangerMSPService.Select
            {
                Link = "and",
                RecordLimit = 0,
                Criterias = new RangerMSPService.Criteria[1]
                {
                        new RangerMSPService.Criteria
                        {
                            Field = "",
                            Operation = "opGreaterThanOrEqual",
                            Value = ""
                        }
                }
            };
            
            DateTime currentTime = DateTime.Now;
            TimeSpan sinceLastRun = new TimeSpan(1, 0, 0);
            DateTime searchTime = currentTime.Subtract(sinceLastRun);
            Console.WriteLine(searchTime);
            select.Criterias[0].Field = "CreationDate";
            select.Criterias[0].Value = searchTime.ToString(); // "01/09/2018  11:40 AM";
            select.RecordLimit = 100;                    // Define max number of records to retrieve. 
            Console.WriteLine("CompanyName Contact CreationDate");
            RangerMSPService.AccountRecord[] accountRecord = accountClient.Select(select);
            foreach (RangerMSPService.AccountRecord record in accountRecord)
            {
                string companyName = record.CompanyName;
                if (companyName == "")
                {
                    companyName = "RESIDENTAL";
                }
                string creationDate = record.CreationDate;
                string contact = record.Contact;
                DateTime parsedDate = DateTime.Parse(creationDate);
                Console.WriteLine($"{companyName} {contact} {parsedDate}");
            }
            Console.WriteLine("Press enter to continue.");
            Console.ReadLine();
        }
    }
}

The Powershell example below merely demonstrates searching for Accounts created since a fixed date. 

You could extend this example to programatically check the datetime at script execution time, subtract an hour, and then use the resultant date in the body of the XML below to find the accounts created in the previous hour

Powershell
# Instructions
#
# 1. Disable Authentication and SSL

$Uri = 'http://localhost:11111/RangerMSP'

$Xml = @'
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">	
  <soap:Body>
    {0}
  </soap:Body>
</soap:Envelope>
'@

$Body = @'
<Select>
      <Select xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Criterias>
          <Criteria>
            <Field>CreationDate</Field>
            <Operation>opGreaterThanOrEqual</Operation>
            <Value>01/09/2019  1:00 AM</Value>
          </Criteria>
        </Criterias>
	<RecordLimit>10</RecordLimit>
	<Link>and</Link>
      </Select>
    </Select>
'@

$result = (Invoke-WebRequest -UseBasicParsing -Body ($Xml -f $Body) `
	-method POST –contentType "text/xml" -Uri $Uri -TimeoutSec 100 -Headers @{"SOAPAction"="urn:IAccount/Select"})

$StringWriter = New-Object System.IO.StringWriter 
$XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter 
$xmlWriter.Formatting = "indented"
$xmlWriter.Indentation = 4
([xml]($result.content)).WriteContentTo($XmlWriter)
$XmlWriter.Flush()
$StringWriter.Flush()
Write-Output $StringWriter.ToString()
Write-Output $result.ToString()