0

Writing some code to do an insert from rest API into database. One of the fields needs to be limited to 8000 characters which I am having trouble doing.

Tried lots of googling.

$searchResultsResponse = Invoke-RestMethod -Method GET -Uri $searchResultsUri -ContentType application/json -Header $requestHeader
$dt = $searchResultsResponse.fields | Select -Property @{label="val";expression={$_.value.Substring(0,8000)}},displayName,name

Expecting the character length to be limited to so many characters but it returns a null value

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
Kez
  • 57
  • 1
  • 8
  • [1] if you leave off the `| Select-Object` do you have a property named `.Value`? [2] if you perform the `.SubString()` directly on the object, does it work? [3] do you get any error messages at all? – Lee_Dailey Feb 04 '19 at 05:06
  • Can you please share more details. what is $searchResultsUri and $requestHeader .I can try to generate an error on my machine – sarvesh shetty Feb 04 '19 at 05:15

1 Answers1

1

You will need to do an if statement as .substring(Start Index, Length) will throw an error if there are less than Length characters:

$(if($_.Length -gt 8000){$_.Substring(0,8000)}else{$_})

Check if Length is greater than (-gt) 8k, then use substring(), otherwise send as is:

$searchResultsResponse = Invoke-RestMethod -Method GET -Uri $searchResultsUri -ContentType application/json -Header $requestHeader 
$dt = $searchResultsResponse.fields | Select -Property @{label="val";expression={$(if($_.Length -gt 8000){$_.Substring(0,8000)}else{$_})}},displayName,name
Flinsch
  • 4,296
  • 1
  • 20
  • 29
ArcSet
  • 6,518
  • 1
  • 20
  • 34