0

I am running a PowerShell script as follows:

$url = "http://api.weatherunlocked.com/api/forecast/29.36,47.97?app_id=****&app_key=****";
$req = [System.Net.WebRequest]::Create($url)
$req.Method ="GET"
$req.ContentLength = 0
$req.Timeout = 600000
$resp = $req.GetResponse()
$reader = new-object System.IO.StreamReader($resp.GetResponseStream())
$reader.ReadToEnd() | Out-File weatherOutput.json

It's giving the following error:

run.sh: line 2: syntax error near unexpected token `('
run.sh: line 2: `$req = [System.Net.WebRequest]::Create($url)'
0

2 Answers 2

3

It looks like you're trying to run a PowerShell script in UNIX considering the error starts with run.sh. Powershell code can only be run on Windows machines With PowerShell installed (built in from Win7+, separate install for xp and vista).

Try running your script like using this command on a Windows-machine:

PowerShell.exe -ExecutionPolicy Bypass -File "C:\myscript.ps1"
Sign up to request clarification or add additional context in comments.

5 Comments

thanks Frode F for your response. i am triying to get data in command line from website the above script works fine in windows i can get data in command line . can you please suggest how can do the same in UNIX .
This code might already even work in Pash
@Suyog you use wget for get site like wget --recursive http://stackoverflow.com
hi hidd3n when i run the command wget --recursive api.weatherunlocked.com i got error 403 forbidden
Total necromancy on this ancient discussion, but just for the sake of anybody stumbling upon this 9+ years later, in 2024 you can simply download a pwsh .deb and install it with dpkg on any Debian-based Linux (such as Ubuntu). .NET Core itself, which matured into .NET 5+, is fully functional on Linux, which means there's no emulation layer involved, you're literally running .NET code on pwsh regardless of whether you're running it on Windows or Linux (or OS X, for that matter). Turns out we do have flying cars here in the future!
0

In a Unix environment use wget or curl.

url="http://api.weatherunlocked.com/api/forecast/29.36,47.97?app_id=****&app_key=****"
wget -O weatherOutput.json $url

If you want the data echoed to STDOUT just replace the filename with -. You may also want to add the parameter -q to suppress informational/progress output.

wget -qO - $url

Or use curl, which writes the data to STDOUT by default.

2 Comments

thank you Ansgar it worked i am getting it data in json format and it is saving it in seperate file. please tell me how can get same information in command line i mean the contents which are storing in json file i want it on command line. is there any way to get it done..??
What do you mean by "on the command line"? Do you want the data echoed to STDOUT?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.