<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to format output of REST API using .Net.WebClient in Powershell? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/How-to-format-output-of-REST-API-using-Net-WebClient-in/m-p/214440#M42186</link>
    <description>&lt;P&gt;I have the following script. I am trying to format the output to CSV format, not HTML. Unfortunately the Output_Mode=csv does not work with the connection string. Any suggestions?&lt;/P&gt;

&lt;P&gt;NOTE: I cannot use curl as I have to get the credentials from the end user prior to connection.&lt;/P&gt;

&lt;P&gt;----- SCRIPT ------&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;function New-TrustAllWebClient {
    # Create a compilation environment
    $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
    $Compiler=$Provider.CreateCompiler()
    $Params=New-Object System.CodeDom.Compiler.CompilerParameters
    $Params.GenerateExecutable=$False
    $Params.GenerateInMemory=$True
    $Params.IncludeDebugInformation=$False
    $Params.ReferencedAssemblies.Add("System.DLL") &amp;gt; $null
    $TASource=@'
      namespace Local.ToolkitExtensions.Net.CertificatePolicy {
        public class TrustAll : System.Net.ICertificatePolicy {
          public TrustAll() { 
          }
          public bool CheckValidationResult(System.Net.ServicePoint sp,
            System.Security.Cryptography.X509Certificates.X509Certificate cert, 
            System.Net.WebRequest req, int problem) {
            return true;
          }
        }
      }
'@ 
    $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
    $TAAssembly=$TAResults.CompiledAssembly
    ## We now create an instance of the TrustAll and attach it to the ServicePointManager
    $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
    [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
    ## The ESX Upload requires the Preauthenticate value to be true which is not the default
    ## for the System.Net.WebClient class which has very simple-to-use downloadFile and uploadfile
    ## methods.  We create an override class which simply sets that Preauthenticate value.
    ## After creating an instance of the Local.ToolkitExtensions.Net.WebClient class, we use it just
    ## like the standard WebClient class.
    $WCSource=@'
      namespace Local.ToolkitExtensions.Net {
        class WebClient : System.Net.WebClient {
          protected override System.Net.WebRequest GetWebRequest(System.Uri uri) {
            System.Net.WebRequest webRequest = base.GetWebRequest(uri);
            webRequest.PreAuthenticate = true;
            webRequest.Timeout = 10000;
            return webRequest;
          }
        }
      }
'@
    $WCResults=$Provider.CompileAssemblyFromSource($Params,$WCSource)
    $WCAssembly=$WCResults.CompiledAssembly
    ## Now return the custom WebClient. It behaves almost like a normal WebClient.
    $WebClient=$WCAssembly.CreateInstance("Local.ToolkitExtensions.Net.WebClient")
    return $WebClient
}
# Example of using this function to upload a file over SSL.
# Notice that the object you get back from New-TrustAllWebClient is almost identical
# to what you would get from new-object system.net.webclient.
 $loc=get-location 
 $wc = New-TrustAllWebClient
 $credential = get-credential
 $wc.set_Credentials($credential.GetNetworkCredential())
 $URL = "https://iosplunkprd-v5:8089/services/search/jobs/export"
 $wc.UploadString($URL, "POST", "search=search EventCode=4740 user=* daysago=1 |Table Caller_Computer_Name,_time") 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 04 Sep 2015 14:42:47 GMT</pubDate>
    <dc:creator>williamberrysr</dc:creator>
    <dc:date>2015-09-04T14:42:47Z</dc:date>
    <item>
      <title>How to format output of REST API using .Net.WebClient in Powershell?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-format-output-of-REST-API-using-Net-WebClient-in/m-p/214440#M42186</link>
      <description>&lt;P&gt;I have the following script. I am trying to format the output to CSV format, not HTML. Unfortunately the Output_Mode=csv does not work with the connection string. Any suggestions?&lt;/P&gt;

&lt;P&gt;NOTE: I cannot use curl as I have to get the credentials from the end user prior to connection.&lt;/P&gt;

&lt;P&gt;----- SCRIPT ------&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;function New-TrustAllWebClient {
    # Create a compilation environment
    $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
    $Compiler=$Provider.CreateCompiler()
    $Params=New-Object System.CodeDom.Compiler.CompilerParameters
    $Params.GenerateExecutable=$False
    $Params.GenerateInMemory=$True
    $Params.IncludeDebugInformation=$False
    $Params.ReferencedAssemblies.Add("System.DLL") &amp;gt; $null
    $TASource=@'
      namespace Local.ToolkitExtensions.Net.CertificatePolicy {
        public class TrustAll : System.Net.ICertificatePolicy {
          public TrustAll() { 
          }
          public bool CheckValidationResult(System.Net.ServicePoint sp,
            System.Security.Cryptography.X509Certificates.X509Certificate cert, 
            System.Net.WebRequest req, int problem) {
            return true;
          }
        }
      }
'@ 
    $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
    $TAAssembly=$TAResults.CompiledAssembly
    ## We now create an instance of the TrustAll and attach it to the ServicePointManager
    $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
    [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
    ## The ESX Upload requires the Preauthenticate value to be true which is not the default
    ## for the System.Net.WebClient class which has very simple-to-use downloadFile and uploadfile
    ## methods.  We create an override class which simply sets that Preauthenticate value.
    ## After creating an instance of the Local.ToolkitExtensions.Net.WebClient class, we use it just
    ## like the standard WebClient class.
    $WCSource=@'
      namespace Local.ToolkitExtensions.Net {
        class WebClient : System.Net.WebClient {
          protected override System.Net.WebRequest GetWebRequest(System.Uri uri) {
            System.Net.WebRequest webRequest = base.GetWebRequest(uri);
            webRequest.PreAuthenticate = true;
            webRequest.Timeout = 10000;
            return webRequest;
          }
        }
      }
'@
    $WCResults=$Provider.CompileAssemblyFromSource($Params,$WCSource)
    $WCAssembly=$WCResults.CompiledAssembly
    ## Now return the custom WebClient. It behaves almost like a normal WebClient.
    $WebClient=$WCAssembly.CreateInstance("Local.ToolkitExtensions.Net.WebClient")
    return $WebClient
}
# Example of using this function to upload a file over SSL.
# Notice that the object you get back from New-TrustAllWebClient is almost identical
# to what you would get from new-object system.net.webclient.
 $loc=get-location 
 $wc = New-TrustAllWebClient
 $credential = get-credential
 $wc.set_Credentials($credential.GetNetworkCredential())
 $URL = "https://iosplunkprd-v5:8089/services/search/jobs/export"
 $wc.UploadString($URL, "POST", "search=search EventCode=4740 user=* daysago=1 |Table Caller_Computer_Name,_time") 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Sep 2015 14:42:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-format-output-of-REST-API-using-Net-WebClient-in/m-p/214440#M42186</guid>
      <dc:creator>williamberrysr</dc:creator>
      <dc:date>2015-09-04T14:42:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to format output of REST API using .Net.WebClient in Powershell?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-format-output-of-REST-API-using-Net-WebClient-in/m-p/214441#M42187</link>
      <description>&lt;P&gt;Disregard.... I found the answer. I have to use the &amp;amp; sign and then the output format. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;$wc.UploadString($URL, "POST", "search=search EventCode=4740 user=* daysago=1 |Table Caller_Computer_Name,_time&amp;amp;output_mode=csv")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Sep 2015 15:13:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-format-output-of-REST-API-using-Net-WebClient-in/m-p/214441#M42187</guid>
      <dc:creator>williamberrysr</dc:creator>
      <dc:date>2015-09-04T15:13:42Z</dc:date>
    </item>
  </channel>
</rss>

