Power shell script

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)
Benny CaspiIntegrationAsked:
Who is Participating?
 
Shaun VermaakConnect With a Mentor Technical Specialist/DeveloperCommented:
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

1
 
David SankovskySenior SysAdminCommented:
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

0
 
aikimarkCommented:
why are you renaming a json file as .XML?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
Benny CaspiIntegrationAuthor 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
0
 
aikimarkConnect With a Mentor 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

0
 
Benny CaspiIntegrationAuthor 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 ?
0
 
aikimarkCommented:
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.
0
 
aikimarkCommented:
I don't have curl on my laptop, so I hard-coded the JSON response.
0
 
Benny CaspiIntegrationAuthor 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)")
0
 
aikimarkConnect With a Mentor 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.
1
 
Benny CaspiIntegrationAuthor 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 ?
0
 
aikimarkConnect With a Mentor Commented:
my prior comment:
($curlresponse | convertfrom-json).content.item

Open in new window

0
 
Benny CaspiIntegrationAuthor 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 ?
0
 
Benny CaspiIntegrationAuthor Commented:
1st question : how to show it pretty - resolved by
$response.Content.Split(',')
0
 
aikimarkCommented:
Since this question thread is closed, please open a new thread for your new questions.
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.