Home > other >  Powershell - Loop through folder, Get Contents and post to SOAP
Powershell - Loop through folder, Get Contents and post to SOAP

Time:11-09

I am trying to loop through a folder, grab all files, read their contents then post each file content individually to SOAP.

This is how I would do it, but PowerShell returns an error.

Invoke-Webrequest : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

Below is my code:

$dataAPI = Get-ChildItem 'C:\Users\..\Output'
$uri = 'http://server-name.com:8080/name/name2'

ForEach ($Item in $dataAPI) {
    Get-Content $Item.FullName | Invoke-Webrequest -Headers @{"Content-Type" = "text/xml;charset=UTF-8"; "SOAPAction" = "http://server-name.com:8080/name/name2"} -Method 'POST' -Body $dataAPI -Uri $uri -UseDefaultCredential 

}

I am not really sure where I should place the Invoke-WebRequest...

Any help would be appreciated. Thanks.

CodePudding user response:

Continuing from my comments,

  • Add switch -Raw to the Get-Content call to receive a single multiline string instead of an array of lines
  • Add switch -File to the Get-ChildItem call to ensure you will only deal with files in the loop, not directories too

Try

# if all files you need have a common extension, add `-Filter '*.xml'` to below line
# '*.xml' is just an example here..
$files  = Get-ChildItem -Path 'C:\Users\Gabriel\Output' -File
$uri    = 'http://server-name.com:8080/name/name2'
$header = @{"Content-Type" = "text/xml;charset=UTF-8"; "SOAPAction" = "http://server-name.com:8080/name/name2"}

foreach ($Item in $files) {
    $content = Get-Content $Item.FullName -Raw
    Invoke-Webrequest -Headers $header -Method 'POST' -Body $content -Uri $uri -UseDefaultCredential 
}
  • Related