Power shell script

Benny Caspi
Benny Caspi used Ask the Experts™
on
Need to write a script which:
1. Download the json file (curl request)
2. Rename the file to  YYYYMMDDHHMM_lng.xml
3. Push it to a shared folder (for example //192.168.1.1/log)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Try this one (It's untested, make sure to do a dry run first)

#Generate TimeStamp
$tStamp=Get-Date
$Day=$($tStamp).Day
$Month=$($tStamp).Month
$Year=$($tStamp).Year
$Hour=$($tStamp).Hour
$Minute=$($tStamp).Minute


#Set Download Location
$URL = "https://domain.tld/path/to/file.xml"

#Set downloaded file name
$FNAME = "$YEAR"+"$Month"+"$Day"+"$Hour"+"$Minute"+"_lng.xml"
$OUTPATH = "\\network.path.address\shared\folder"
$OUTFILE = "$OUTPATH"+"\"+"$FNAME"

#invoke the command:
Invoke-WebRequest -Uri $URL -OutFile $OUTFILE

Open in new window

Technical Specialist
Awarded 2017
Distinguished Expert 2018
Commented:
1) If it is just a Get you can just download
2) No need to rename, just download with the correct name
3) No need to copy, download to correct location

$newFileName = "$(get-date -f yyyyMMddHHmm)_lng.xml"
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile("https://www.somewhere.com/file.xml","C:\path\$($newFileName)")

Open in new window

Top Expert 2014

Commented:
why are you renaming a json file as .XML?
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Benny CaspiIntegration

Author

Commented:
10xs for quick answer.
I get my 'JSON' by running curl to https://poeditor.com/api/:

curl.exe -k -X POST https://poeditor.com/api/ -H Cache-Control=no-cache -H Content-Type="application/json" -F api_token="ttttssssaaaaqqqqzzzzwwwwyyyyxxxx" -F action="export" -F id="123456" -F language=en -F type="key_value_json"

response is:
{"response":{"status":"success","code":"200","message":"OK"},"item":"https:\/\/poeditor.com\/api\/download\/file\/10b60ec9fd13ff1dfe146590f48388f0"}

need to take "https:\/\/poeditor.com\/api\/download\/file\/10b60ec9fd13ff1dfe146590f48388f0" and save it as yyyyMMddHHmm_lng.xml

please assist to write it true
Top Expert 2014
Commented:
$curlresponse='{"response":{"status":"success","code":"200","message":"OK"},"item":"https:\/\/poeditor.com\/api\/download\/file\/10b60ec9fd13ff1dfe146590f48388f0"}'
$b=[pscustomobject]@{name="item";value=($curlresponse | convertfrom-json).item} | convertto-xml

Open in new window

Benny CaspiIntegration

Author

Commented:
explanation to me request:
I get next Output from Invoke-WebRequest -uri 'https://poeditor.com/api/' -Method POST -body $postparams

StatusCode        : 200
StatusDescription : OK
Content           : {"response":{"status":"success","code":"200","message":"OK"},"item":"https:\/\/poeditor.com\/api\/d
                    ownload\/file\/ac9dafc0ad7175387ce78668e746ef43"}
RawContent        : HTTP/1.1 200 OK

each time the url is different

I want to copy the URL in 'normal form https:\\poeditor.com\api\...
and run :
$newFileName = "$(get-date -f yyyyMMddHHmm)_lng.xml"
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile("https://www.somewhere.com/file.xml","C:\path\$($newFileName)")

How can I do it in 1 script ?
Top Expert 2014

Commented:
my code would take the response you get and extract the item value (=URL). Your posted script could use that URL to download the XML file.
Top Expert 2014

Commented:
I don't have curl on my laptop, so I hard-coded the JSON response.
Benny CaspiIntegration

Author

Commented:
each time the url is different:


StatusCode        : 200
StatusDescription : OK
Content           : {"response":{"status":"success","code":"200","message":"OK"},"item":"https:\/\/poeditor.com\/api\/d
                    ownload\/file\/ba1ffd4ee8a80c4a7754b3e2bbb6bfcc"}
RawContent        : HTTP/1.1 200 OK
                    Pragma: no-cache
                    Content-Length: 148
                    Cache-Control: no-store, no-cache, must-revalidate
                    Content-Type: application/json; charset=utf-8
                    Date: Sun, 01 Apr 2018 21:31:06 GMT
                    Expires:...
Forms             : {}
Headers           : {[Pragma, no-cache], [Content-Length, 148], [Cache-Control, no-store, no-cache, must-revalidate],
                    [Content-Type, application/json; charset=utf-8]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 148

how to aassign 'customparamater' to 'item' on the fly (in xml format ) ?
and run  

$WebClient.DownloadFile("'customparamater'","C:\path\$($newFileName)")
Top Expert 2014
Commented:
1. Assign the response to the curl command to a variable, $curlresponse
2. run the following statement:
($curlresponse | convertfrom-json).content.item

Open in new window

That should get you the URL string.  You can then use that URL string in your download.
Benny CaspiIntegration

Author

Commented:
when I run:
$b=[pscustomobject]@{name="item";value=($curlresponse | convertfrom-json).item} | convertto-xml

I get:
PS C:\curl> echo $b

xml                            Objects
---                            -------
version="1.0" encoding="utf-8" Objects

how I can get just the https://url ?
Top Expert 2014
Commented:
my prior comment:
($curlresponse | convertfrom-json).content.item

Open in new window

Benny CaspiIntegration

Author

Commented:
additional Powershell Question:
in my script I use $response.Content and get the output not pretty
script:
$response=Invoke-WebRequest -Uri http://tve-web1-dev.corbina.net/api_v3/service/configurations/action/serveByDevice -Method POST -Body $postman -ContentType "application/json"
$response

 output:
"apiVersion":"4.7","intro_video":"564916","bg_video":"564915","Regions":"http://static.beeline.tv/catalog/re
gion_20180302.xml","ott_user_type_default_region":"1077","EULA":"http://interface.beeline-tv.ru/html_forms/EULA.html","
rolling_buffer_hours":"72","CDNCachingGW":"http://static.beeline.tv/","dictionary_url_rus":"http://static.beeline.tv/ca
talog/translations/183011410_rus.json","dictionary_url_en":"http://static.beeline.tv/catalog/translations/183011410_en.
json","Suggested_recalculation_period ":"7","Suggested_calculation_by_last_x_items ":"20"}}

1.how to show it pretty
2. how to update 1 value in the text
,"dictionary_url_rus":"http://static.beeline.tv/ca
talog/translations/183011410_rus.json with new URL ?
Benny CaspiIntegration

Author

Commented:
1st question : how to show it pretty - resolved by
$response.Content.Split(',')
Top Expert 2014

Commented:
Since this question thread is closed, please open a new thread for your new questions.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial