Custom Object for API

This topic contains 13 replies, has 2 voices, and was last updated by  Jason 2 weeks, 6 days ago.

  • Author
    Posts
  • #92188

    Jason
    Participant

    I am working with an API, and this one is requiring the first parameter passed to it be an object and not a string. I think I need to create an object and set the “.ID” string field to the value I am pulling from SQL.

    $OSSNAWS = New-WebServiceProxy -Uri "http://localhost/websiteA/Web.asmx?wsdl" -Namespace WebServiceProxy -Credential $Credential 
    
    $applicationUri = "http://websiteB.com/Apps/SMessages/MPage.aspx" 
    
    $extDeviceID = '00AA11BB22CC' 
    
    $devID = New-Object -TypeName PSObject 
    
    Add-Member -InputObject $devID -MemberType NoteProperty -Name Id -Value $extDeviceID 
    
    $devID.Id = $extDeviceID 
    
    $OSSNBWS.LaunchClientApplication($devID,$applicationUri) 
    

    The API is the “LaunchClientApplication” and the parameter that needs to be an object is the $devID.

    When I run the above, this is the error I get back.

    Cannot convert argument "0", with value: "@{Id=00AA11BB22CC}", for "LaunchClientApplication" to type "WebServiceProxy.ExternalId": "Cannot convert the "@{Id=00AA11BB22CC}" value of type "System.Management.Automation
    .PSCustomObject" to type "WebServiceProxy.ExternalId"."
    At C:\PowerShellScripts\Test-.ps1:23 char:38
    + $OSSNBWS.LaunchClientApplication < <<< ($devID,$applicationUri) + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

    I have been playing around and researching this and cannot seem to get that parameter right. I feel that there is a rather simple solution that is just out of my reach.

    Thanks.

  • #92339

    Matt Bloomfield
    Participant

    This is tricky to answer because we don't have access to the API you're using which is probably why you've not had a response to your post.

    The error message is given because the LaunchClientApplication method is unable to cast (convert) your PSCustomObject to the object of type ExternalID that it is expecting.

    In the first instance, I'd be inclined just to pass it a string and see if you get a similar error.

  • #92351

    Jason
    Participant

    Yes, I figured that would be the case, so I hoped at least someone would ask for more information.

    I tried passing just the $extDeviceID variable as a string and received a similar error. I think my problem is that I do not know how to make an object as a type "WebServiceProxy" with the method ".ID". I found some help in another forum with someone that knows C# but not PowerShell and they posted this:

    "You need to create the object and set the “.Id” string field to the value of the external device Id. Here is some C# code fragment.

            String appURL = “https://myServerURL.com/WebFolder/WebApp.aspx”;
            String extDeviceId = “MySTB_External_Device_ID”;
            UINotificationsWS.DeviceExternalId devID = new UINotificationsWS.DeviceExternalId();
            devID.Id = extDeviceId;
            uinws.LaunchClientApplication(devID, appURL);
    

    "

    I do not know C#. Hoping someone can help me translate.

    Thanks for the response.

  • #92369

    Matt Bloomfield
    Participant

    Can you pipe $OSSNAWS to Get-Member and post the results.

  • #92371

    Jason
    Participant

    Yes, trying a cut and paste.

    PS C:\> $OSSNAWS | Get-Member
    
    
       TypeName: WebServiceProxy.UINotificationsWS
    
    Name                                 MemberType Definition                                                                                                                                                           
    ----                                 ---------- ----------                                                                                                                                                           
    BroadcastMessageCompleted            Event      WebServiceProxy.BroadcastMessageCompletedEventHandler BroadcastMessageCompleted(System.Object, System.ComponentModel.AsyncCompletedEventArgs)                        
    BroadcastMessageToGroupsCompleted    Event      WebServiceProxy.BroadcastMessageToGroupsCompletedEventHandler BroadcastMessageToGroupsCompleted(System.Object, System.ComponentModel.AsyncCompletedEventArgs)        
    Disposed                             Event      System.EventHandler Disposed(System.Object, System.EventArgs)                                                                                                        
    LaunchClientApplicationCompleted     Event      WebServiceProxy.LaunchClientApplicationCompletedEventHandler LaunchClientApplicationCompleted(System.Object, System.ComponentModel.AsyncCompletedEventArgs)          
    SendMessageToAccountCompleted        Event      WebServiceProxy.SendMessageToAccountCompletedEventHandler SendMessageToAccountCompleted(System.Object, System.ComponentModel.AsyncCompletedEventArgs)                
    SendMessageToDevicesCompleted        Event      WebServiceProxy.SendMessageToDevicesCompletedEventHandler SendMessageToDevicesCompleted(System.Object, System.ComponentModel.AsyncCompletedEventArgs)                
    Abort                                Method     System.Void Abort()                                                                                                                                                  
    BeginBroadcastMessage                Method     System.IAsyncResult BeginBroadcastMessage(string notificationData, System.DateTime deliveryTime, System.DateTime expirationTime, System.AsyncCallback callback, Sy...
    BeginBroadcastMessageToGroups        Method     System.IAsyncResult BeginBroadcastMessageToGroups(string notificationData, System.DateTime deliveryTime, System.DateTime expirationTime, string[] grpExternalIds, ...
    BeginLaunchClientApplication         Method     System.IAsyncResult BeginLaunchClientApplication(WebServiceProxy.ExternalId externalId, string applicationUri, System.AsyncCallback callback, System.Object asyncS...
    BeginSendMessageToAccount            Method     System.IAsyncResult BeginSendMessageToAccount(string nativeId, System.DateTime deliveryTime, System.DateTime expirationTime, string notificationData, System.Async...
    BeginSendMessageToDevices            Method     System.IAsyncResult BeginSendMessageToDevices(System.Guid[] devices, System.DateTime deliveryTime, System.DateTime expirationTime, string notificationData, System...
    BroadcastMessage                     Method     System.Void BroadcastMessage(string notificationData, System.DateTime deliveryTime, System.DateTime expirationTime)                                                  
    BroadcastMessageAsync                Method     System.Void BroadcastMessageAsync(string notificationData, System.DateTime deliveryTime, System.DateTime expirationTime), System.Void BroadcastMessageAsync(string...
    BroadcastMessageToGroups             Method     System.Void BroadcastMessageToGroups(string notificationData, System.DateTime deliveryTime, System.DateTime expirationTime, string[] grpExternalIds)                 
    BroadcastMessageToGroupsAsync        Method     System.Void BroadcastMessageToGroupsAsync(string notificationData, System.DateTime deliveryTime, System.DateTime expirationTime, string[] grpExternalIds), System....
    CancelAsync                          Method     System.Void CancelAsync(System.Object userState)                                                                                                                     
    CreateObjRef                         Method     System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)                                                                                                      
    Discover                             Method     System.Void Discover()                                                                                                                                               
    Dispose                              Method     System.Void Dispose()                                                                                                                                                
    EndBroadcastMessage                  Method     System.Void EndBroadcastMessage(System.IAsyncResult asyncResult)                                                                                                     
    EndBroadcastMessageToGroups          Method     System.Void EndBroadcastMessageToGroups(System.IAsyncResult asyncResult)                                                                                             
    EndLaunchClientApplication           Method     System.Void EndLaunchClientApplication(System.IAsyncResult asyncResult)                                                                                              
    EndSendMessageToAccount              Method     System.Void EndSendMessageToAccount(System.IAsyncResult asyncResult)                                                                                                 
    EndSendMessageToDevices              Method     System.Void EndSendMessageToDevices(System.IAsyncResult asyncResult)                                                                                                 
    Equals                               Method     bool Equals(System.Object obj)                                                                                                                                       
    GetHashCode                          Method     int GetHashCode()                                                                                                                                                    
    GetLifetimeService                   Method     System.Object GetLifetimeService()                                                                                                                                   
    GetType                              Method     type GetType()                                                                                                                                                       
    InitializeLifetimeService            Method     System.Object InitializeLifetimeService()                                                                                                                            
    LaunchClientApplication              Method     System.Void LaunchClientApplication(WebServiceProxy.ExternalId externalId, string applicationUri)                                                                    
    LaunchClientApplicationAsync         Method     System.Void LaunchClientApplicationAsync(WebServiceProxy.ExternalId externalId, string applicationUri), System.Void LaunchClientApplicationAsync(WebServiceProxy.E...
    SendMessageToAccount                 Method     System.Void SendMessageToAccount(string nativeId, System.DateTime deliveryTime, System.DateTime expirationTime, string notificationData)                             
    SendMessageToAccountAsync            Method     System.Void SendMessageToAccountAsync(string nativeId, System.DateTime deliveryTime, System.DateTime expirationTime, string notificationData), System.Void SendMes...
    SendMessageToDevices                 Method     System.Void SendMessageToDevices(System.Guid[] devices, System.DateTime deliveryTime, System.DateTime expirationTime, string notificationData)                       
    SendMessageToDevicesAsync            Method     System.Void SendMessageToDevicesAsync(System.Guid[] devices, System.DateTime deliveryTime, System.DateTime expirationTime, string notificationData), System.Void S...
    ToString                             Method     string ToString()                                                                                                                                                    
    AllowAutoRedirect                    Property   System.Boolean AllowAutoRedirect {get;set;}                                                                                                                          
    ClientCertificates                   Property   System.Security.Cryptography.X509Certificates.X509CertificateCollection ClientCertificates {get;}                                                                    
    ConnectionGroupName                  Property   System.String ConnectionGroupName {get;set;}                                                                                                                         
    Container                            Property   System.ComponentModel.IContainer Container {get;}                                                                                                                    
    CookieContainer                      Property   System.Net.CookieContainer CookieContainer {get;set;}                                                                                                                
    Credentials                          Property   System.Net.ICredentials Credentials {get;set;}                                                                                                                       
    EnableDecompression                  Property   System.Boolean EnableDecompression {get;set;}                                                                                                                        
    PreAuthenticate                      Property   System.Boolean PreAuthenticate {get;set;}                                                                                                                            
    Proxy                                Property   System.Net.IWebProxy Proxy {get;set;}                                                                                                                                
    RequestEncoding                      Property   System.Text.Encoding RequestEncoding {get;set;}                                                                                                                      
    Site                                 Property   System.ComponentModel.ISite Site {get;set;}                                                                                                                          
    SoapVersion                          Property   System.Web.Services.Protocols.SoapProtocolVersion SoapVersion {get;set;}                                                                                             
    Timeout                              Property   System.Int32 Timeout {get;set;}                                                                                                                                      
    UnsafeAuthenticatedConnectionSharing Property   System.Boolean UnsafeAuthenticatedConnectionSharing {get;set;}                                                                                                       
    Url                                  Property   System.String Url {get;set;}                                                                                                                                         
    UseDefaultCredentials                Property   System.Boolean UseDefaultCredentials {get;set;}                                                                                                                      
    UserAgent                            Property   System.String UserAgent {get;set;}                                                                                                                       
  • #92374

    Matt Bloomfield
    Participant

    I was hoping to see a more obvious method for creating it but based on the C# example, I'm thinking something like this:

    $OSSNAWS = New-WebServiceProxy -Uri "http://localhost/websiteA/Web.asmx?wsdl" -Namespace WebServiceProxy -Credential $Credential 
    
    $applicationUri = "http://websiteB.com/Apps/SMessages/MPage.aspx" 
    
    $extDeviceID = '00AA11BB22CC' 
    
    $devID = New-Object WebServiceProxy.UINotificationsWS.DeviceExternalId
    
    $devID.Id = $extDeviceID
    
    $OSSNBWS.LaunchClientApplication($devID,$applicationUri) 
    
    
  • #92375

    Jason
    Participant

    Much thanks. I just tried that but I am still getting errors.

    New-Object : Cannot find type [WebServiceProxy.UINotificationsWS.DeviceExternalId]: make sure the assembly containing this type is loaded.
    At C:\PowerShellScripts\Test.ps1:18 char:20
    + $devID = New-Object < <<<  WebServiceProxy.UINotificationsWS.DeviceExternalId
        + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
        + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
     
    Property 'Id' cannot be found on this object; make sure it exists and is settable.
    At C:\PowerShellScripts\Test.ps1:20 char:8
    + $devID. < <<< Id = $extDeviceID
        + CategoryInfo          : InvalidOperation: (Id:String) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFound
    
    Exception calling "LaunchClientApplication" with "2" argument(s): "Server was unable to process request. ---> externalId must be a DeviceExternalId"
    At C:\PowerShellScripts\Test.ps1:22 char:38
    + $OSSNBWS.LaunchClientApplication < <<< ($devID,$applicationUri)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException
    
    
  • #92378

    Matt Bloomfield
    Participant

    Ok, I based it off the example but the object type in the method definition is ExternalID not DeviceExternalID.

    So you could try

    $devID = New-Object WebServiceProxy.UINotificationsWS.ExternalId
    

    or

    $devID = New-Object WebServiceProxy.ExternalId
    

    If those don't work I think we're clutching at straws without access to the documentation for the API.

  • #92390

    Jason
    Participant

    I found the page where it shows the SOAP code for the API, but cannot seem to get it to display here, in this message. ???

    I will look for more in the morning (EST). Much thanks for all the help.

    Also;

    PS C:\> $ossnbWS.LaunchClientApplication | get-member
    
    
       TypeName: System.Management.Automation.PSMethod
    
    Name                MemberType Definition                                                                                                                                                          
    ----                ---------- ----------                                                                                                                                                          
    Copy                Method     System.Management.Automation.PSMemberInfo Copy()                                                                                                                    
    Equals              Method     bool Equals(System.Object obj)                                                                                                                                      
    GetHashCode         Method     int GetHashCode()                                                                                                                                                   
    GetType             Method     type GetType()                                                                                                                                                      
    Invoke              Method     System.Object Invoke(Params System.Object[] arguments)                                                                                                              
    ToString            Method     string ToString()                                                                                                                                                   
    IsInstance          Property   System.Boolean IsInstance {get;}                                                                                                                                    
    MemberType          Property   System.Management.Automation.PSMemberTypes MemberType {get;}                                                                                                        
    Name                Property   System.String Name {get;}                                                                                                                                           
    OverloadDefinitions Property   System.Collections.ObjectModel.Collection`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] OverloadDefinitions {get;}
    TypeNameOfValue     Property   System.String TypeNameOfValue {get;}                                                                                                                                
    Value               Property   System.Object Value {get;set;}                                                                                                                                      
    
  • #92407

    Jason
    Participant

    Trying to post the SOAP code....

    SOAP Code-01

    SOAP Code-02

  • #92426

    Matt Bloomfield
    Participant

    After creating the namespace can you see if the class's static methods offer any clues. The C# example suggests the class has a method for creating the object:

    WebServiceProxy.UINotificationsWS | Get-Member -Static
  • #92429

    Jason
    Participant

    Do you mean this?

    PS C:\> $OSSNBWS | Get-Member -static
    
    
       TypeName: WebServiceProxy.UINotificationsWS
    
    Name                MemberType Definition                                                                                                                                                                  
    ----                ---------- ----------                                                                                                                                                                  
    Equals              Method     static bool Equals(System.Object objA, System.Object objB)                                                                                                                  
    GenerateXmlMappings Method     static bool GenerateXmlMappings(type type, System.Collections.ArrayList mappings), static hashtable GenerateXmlMappings(type[] types, System.Collections.ArrayList mappings)
    ReferenceEquals     Method     static bool ReferenceEquals(System.Object objA, System.Object objB)                                                                                                         
    

    Or this?...

    PS C:\> $OSSNBWS.LaunchClientApplication | Get-Member -static
    
    
    
       TypeName: System.Management.Automation.PSMethod
    
    Name            MemberType Definition                                                         
    ----            ---------- ----------                                                         
    Equals          Method     static bool Equals(System.Object objA, System.Object objB)         
    ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, System.Object objB)
    
    
  • #92431

    Matt Bloomfield
    Participant

    Yes that's what I was after but it doesn't help 🙁 One last thing that might be worth a try, I found an old Don Jones article that shows how to give your object a custom type name. I don't know if that will fool it into thinking that's what's being passed.

    https://technet.microsoft.com/en-us/library/hh750381.aspx

    $OSSNAWS = New-WebServiceProxy -Uri "http://localhost/websiteA/Web.asmx?wsdl" -Namespace WebServiceProxy -Credential $Credential 
    
    $applicationUri = "http://websiteB.com/Apps/SMessages/MPage.aspx" 
    
    $extDeviceID = '00AA11BB22CC' 
    
    $devID = New-Object -TypeName PSObject 
    
    Add-Member -InputObject $devID -MemberType NoteProperty -Name Id -Value $extDeviceID 
    
    $devID.PSObject.TypeNames.Insert(0,'WebServiceProxy.ExternalId')
    
    $OSSNBWS.LaunchClientApplication($devID,$applicationUri) 
    

    If you pipe $devID to Get-Member you'll now see the typename reported as WebServiceProxy.ExternalID instead of System.Management.Automation.PSCustomObject

    • #92495

      Jason
      Participant

      Much thanks. I will certainly check out that article!

      Yeah, this is kicking my butt. Tried that code and received the following error.

      PS C:\> C:\PowerShellScripts\Test-Crawler-02.ps1
      Cannot convert argument "0", with value: "@{Id=00AA11BB22CC}", for "LaunchClientApplication" to type "WebServiceProxy.ExternalId": "Cannot convert the "@{Id=00AA11BB22CC}" value of type "WebServiceProxy.ExternalId
      " to type "WebServiceProxy.ExternalId"."
      At C:\PowerShellScripts\Test-Crawler-02.ps1:23 char:38
      + $OSSNBWS.LaunchClientApplication < <<< ($devID,$applicationUri)
          + CategoryInfo          : NotSpecified: (:) [], MethodException
          + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
      

      You are right and that code does appear to change the type of $devID, but the API still did not like it.

      PS C:\> $devid | Get-Member
      
      
         TypeName: WebServiceProxy.ExternalId
      
      Name        MemberType   Definition                    
      ----        ----------   ----------                    
      Equals      Method       bool Equals(System.Object obj)
      GetHashCode Method       int GetHashCode()             
      GetType     Method       type GetType()                
      ToString    Method       string ToString()             
      Id          NoteProperty System.String Id=00AA11BB22CC 
      

      I have reached out (again) to someone that might know how to work with this API. Fingers crossed.

      Thanks so much for all the help. And if you have any other ideas, please let me know. And I will do the same. Believe it or not, this was suppose to be the easy part of the script I am trying to make. Little did I know...

You must be logged in to reply to this topic.