- I need to built a scheduled task for posting once per day, a log about users session information (log in/log off/session lock/session unlock...). I have created a WCF service, a database, and functions for getting theses infos. The last step is to built a PowerShell script for posting to the service, this log, from users pc's.
- In my console application I'm using ConfigItem dataclass for my list and then I pass this List with the two otjers string arguments to service. I don't understand how I could do the same thing in the PS script, using the same arguments and post them to the service.
My service method:
public void RecordActivity(string user, string pc, List<ConfigItem> act)
{
var USR = new Users() { UserName = user };
var PC = new Pc() { PcName = pc };
act.ToList().ForEach(p =>
{
int id = (from t in context.Activity where t.ActivityName==p.Activity select t.ActivityId).SingleOrDefault();
var RF = new AuditActivity() { Date = p.Date,ActivityId=id};
USR.AuditActivity.Add(RF);
PC.AuditActivity.Add(RF);
context.Set<Pc>().Add(PC);
context.Set<Users>().Add(USR);
});
context.SaveChanges();
}
Data class in service :
[DataContract]
public class ConfigItem
{
[DataMember]
public DateTime Date { get; set; }
[DataMember]
public string Activity { get; set; }
}
Function to get users informations and post them in service (working in a console app)
AuditServiceTestClient sve = new AuditServiceTestClient();
string machine = Environment.MachineName;
string userName = WindowsIdentity.GetCurrent().Name;
List<ConfigItem> cfg = new List<ConfigItem>();
const char fieldSeparator = ';';
string filePath = @"h:\csv\Activity.csv";
using (StreamReader SR = new StreamReader(filePath))
{
while (!SR.EndOfStream)
{
var CSValues = SR.ReadLine().Split(fieldSeparator);
ConfigItem c = new ConfigItem();
c.Date = Convert.ToDateTime(CSValues[0]);
c.Activity = CSValues[1];
cfg.Add(c);
}
}
sve.RecordActivity(userName, machine, cfg);
My PowerShell script :
$WebSvcURL= “http://xxx/AuditServiceTest.svc”
#Create the Web Service Proxy Object
$proxy = New-WebServiceProxy -Uri $WebSvcURL -Namespace ExportTool -Class Program -UseDefaultCredential
$proxy
Write-host -ForegroundColor DarkBlue ===successfullyconnected to the WCF Web Service $proxy