Getting Data In

How to format output of REST API using .Net.WebClient in Powershell?

williamberrysr
Engager

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?

NOTE: I cannot use curl as I have to get the credentials from the end user prior to connection.

----- SCRIPT ------

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") > $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") 
0 Karma
1 Solution

williamberrysr
Engager

Disregard.... I found the answer. I have to use the & sign and then the output format.

$wc.UploadString($URL, "POST", "search=search EventCode=4740 user=* daysago=1 |Table Caller_Computer_Name,_time&output_mode=csv")

View solution in original post

williamberrysr
Engager

Disregard.... I found the answer. I have to use the & sign and then the output format.

$wc.UploadString($URL, "POST", "search=search EventCode=4740 user=* daysago=1 |Table Caller_Computer_Name,_time&output_mode=csv")
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...