Share via


Powershell loop through to set variable from each line of text file

Question

Thursday, July 28, 2016 10:33 AM

I am trying to write a PowerShell script which calls an API which gets a list of machine names from the API. It then puts the machine names in a txt file where I then remove all the unwanted characters from the file just leaving me with lines of the server names. I then want to take each line, set it a variable and then use it to call another API function. I am having issues with selecting the line calling the function and then it continuing to loop through setting each line as the variable and running the API call.

Any help appreciated. 

# Mute  hosts 
#Variables
$api_key = 
$app_key = 
$query = 
$http_method = 
$url_signature = 
$ddhostfile = 
$specialCharacter = "["
$specialCharacter1 = "]"

#Call to DDOG
$url_base = "https://app.datadoghq.com/" 
$url = $url_base + $url_signature + "?api_key=$api_key" + "&" + "application_key=$app_key"+ "&" + "q=$query" 
$http_request = New-Object -ComObject Msxml2.XMLHTTP 
$http_request.open($http_method, $url, $false) 
$http_request.setRequestHeader("Content-type", "application/json")
$http_request.setRequestHeader("Connection", "close") 
$http_request.send($parameters) 
$http_request.status 
$http_request.responseText -split ", " | Out-File $ddhostfile

#Remove unwanted text from results
(gc $ddhostfile | select -Skip 1) | sc $ddhostfile
(gc $ddhostfile) | ForEach-Object { $_ -replace 'hosts' } > $ddhostfile
(gc $ddhostfile) | ForEach-Object { $_ -replace ':' } > $ddhostfile
(gc $ddhostfile) | ForEach-Object { $_ -replace [regex]::Escape($specialCharacter) } > $ddhostfile
(gc $ddhostfile) | ForEach-Object { $_ -replace '"' } > $ddhostfile
(gc $ddhostfile) | ForEach-Object { $_ -replace [regex]::Escape($specialCharacter1) } > $ddhostfile
(gc $ddhostfile) | ForEach-Object { $_ -replace '}' } > $ddhostfile
(gc $ddhostfile) | ForEach-Object { $_ -replace ' ' } > $ddhostfile

#loop through machine names and set as variable to call API function
foreach ($line in $ddhostfile)
{
$ddhost = (gc $ddhostfile) | select -Index 1
write-host $ddhost
$url = "https://app.datadoghq.com/api/v1/host/$ddhost/mute?api_key=&application_key=
write-host $url
Invoke-WebRequest -Method POST -Uri $url
}

Thanks

All replies (5)

Thursday, July 28, 2016 11:02 AM ✅Answered

help Invoke-WebRequest -full
help ConvertFron-Json -full
help ConvertTo-Json -full

YOU are working with old converted VB code that is much harder to manipulate correctly. Use PowerSHell instead. It will save you a lot of pain and confusion.

\(ツ)_/


Thursday, July 28, 2016 3:09 PM

The reason I am having to do the request that way is because the API I am using is not easy to work with using PowerShell see there documentation here: https://help.datadoghq.com/hc/en-us/articles/205564949-Powershell-api-examples.


Thursday, July 28, 2016 3:14 PM

The reason I am having to do the request that way is because the API I am using is not easy to work with using PowerShell see there documentation here: https://help.datadoghq.com/hc/en-us/articles/205564949-Powershell-api-examples,  also the results I get are not in a Valid JSON format.

What makes you think that that is true.  Json objects are returned as strings that have to be converted.

\(ツ)_/


Thursday, July 28, 2016 3:16 PM

I have now managed to work it out using the convertfrom-json

Thanks for the Tip.


Thursday, July 28, 2016 3:22 PM

Good.  It saves a lot of time and trouble as generated json may not be consistent.  The conversion may also be wrapped so it may need you to extract a sub object. It depends on how the Json object is being generated.  It is still all Json.

\(ツ)_/