0
  • 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 
mrplume
  • 183
  • 1
  • 3
  • 18

1 Answers1

2

I'm not sure where your ConfigItem comes from, so New-Object's invocations may differ:

$WebSvcURL = 'http://xxx/AuditServiceTest.svc'

#Create the Web Service Proxy Object
$proxy = New-WebServiceProxy -Uri $WebSvcURL -Namespace ExportTool  -Class Program -UseDefaultCredential

# Get machine and user name
$userName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$machine = [System.Environment]::MachineName

# Create new list of ConfigItems
$cfg = New-Object -TypeName System.Collections.Generic.List``1[ConfigItem]

# Import CSV, add values to list of ConfigItems
Import-Csv -Path 'h:\csv\Activity.csv' | ForEach-Object {
    # Create new ConfigItem
    $item = New-Object -TypeName ConfigItem

    # Set properties
    $item.Date = $_.Date
    $item.Activity = $_.Activity

    # add item to list
    $cfg.Add($item)
}

# Call service method
$proxy.RecordActivity($userName, $machine, $cfg)

References

Community
  • 1
  • 1
beatcracker
  • 6,714
  • 1
  • 18
  • 41
  • ConfigItem is a class created in my WCF service. WCF service method is called, but list is empty – mrplume Dec 21 '15 at 14:34
  • @mrplume Could you post returned serialized objects from console app and PS script? Example: https://stackoverflow.com/questions/32611187/using-complex-objects-via-a-web-service-from-powershell – beatcracker Dec 21 '15 at 14:50
  • Hum... I'n not returning serialized objects from console, I post only values via the service. For return, I'm using a MVC application – mrplume Dec 21 '15 at 15:14
  • With the creation of a custom class "ConfigItem" I get an error. This error helps me to get correct adress to call my ConfigItem on the service. So with 'code'System.Collections.Generic.List``1[Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1ost_xxx_AuditServiceTest_svc.ConfigItem] it's working. Thanks. – mrplume Dec 22 '15 at 09:52