Using Curl in Powershell

So you are a windows user, but really like what you have been able to do using curl on your Mac.  On windows you use PowerShell, what if you want to use cURL?

First of all, what is cURL?  To pull directly from their website :

curl is an open source command line tool and library for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling and more.

More simply stated, cURL is a tool that is used at the command line for transferring data.

So now what if you want to use it in PowerShell?  If you are using version 5.x of  PowerShell you may think that you already have it.  You can check your version of PowerShell by calling the command : $PSVersionTable.PSVersion
Ps1

 

Using cURL in PowerShell

You can type in a cURL command like one that downloads a file from a GitHub repository.

curl http://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/mongodb-on-ubuntu/azuredeploy.json > azuredeploy3.json

and  it will seem like it works but what it is actually doing is just using cURL as an alias. In the above instance, what will happen is that you will just get the headers instead of the file itself.  Alias’ in PowerShell allow you to create shortcuts for longer commands so you don’t have to type them out all of the time. If you type in the command Get-Alias, it will give you a list of all the Alias’ that are used in PowerShell. As you can see, the curl command just calls the Invoke-WebRequest command.  They are similar but not the same which is why the above request does not work for us.

ps2

To get this to work in PowerShell you would need to change the syntax slightly.

ps3

( file name cut off in image “https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/mongodb-on-ubuntu/azuredeploy.json”)

You then open the file from PowerShell either using Code or Sublime.

ps4
Utilizing the switches that Invoke-WebRequest uses instead of cURL does the trick.  But if you want to stay consistent and use the same commands on your MAC as you do on your PC, you will need to remove the alias and install cURL on the PC in order to achieve this.

The first thing you need to do is remove the alias.

Removing the cURL alias

If you don’t already have a profile, run the following command to create one.

New-Item $profile -force -itemtype file

Next, open up the profile file. (This will open the file up in notepad)

notepad $profile

add the following line to the profile file.

remove-item alias:curl

save the notepad file and close it.
Then either run the following command to reload the profile OR close and reopen Powershell to refresh.

remove-item alias:curl

(thanks to megamorf for these steps)

Now if you run the Get-Alias command again, you will see that the curl alias is gone.

Installing cURL on your PC

There are a number of ways you can do this.  One way is to use the cURL for Windows created by the Confused By Code author : http://www.confusedbycode.com/curl/ this is a step by step installer that installs curl, adds it to your path, installs the certs etc.. but in keeping with my new mindset of doing everything from the command line that I can, I decided to install it using Chocolately. https://chocolatey.org/

Chocolatey is a Linux style command line packet manager for windows.  I will be using it in the future so it makes sense to use it for this.   To install it do the following

Open up PowerShell in Admin Mode and run the following command.

iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))

Once you have installed Chocolatey, you can then use it to install curl. (Open and close PowerShell first)

choco install curl

Now you have curl installed but if you try to run the curl command from above, you will receive some OpenSSL errors. To fix this you have two choices, One use the -R switch to ignore the certs (not recommended) OR install the certs.

To install the certs, navigate to C:\ProgramData\chocolatey\lib\curl\tools ( do it in windows explorer or powershell, doesn’t matter to me)

Double click mk-ca-bundle.vbs, or if you have Perl, mk-ca-bundle.pl. The script will download a new SSL bundle from Mozilla. It will be called ca-bundle.crt. once it is downloaded rename the file to curl-bundle.crt and leave it in the same directory.

Open and close PowerShell and then run the following command.

curl https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/mongodb-on-ubuntu/azuredeploy.json > azuredeploy6.json

(By the way, this is an ARM template that I am using to install MongoDB in Azure on Ubuntu if you are curious. See my Mean On Azure Series if you want to learn more. http://sogeek.us/meanonazure)

That’s all.. I hope you found that useful.

Daniel – The Sociable Geek

Mean On Azure Episode 1

I just published my first episode in a series called Mean On Azure. In it I take you through my journey of discover in the Mean Stack in bite size videos showing you how to not only use the MEAN stack, but how to use it on Azure.

To view the whole series on Channel 9 go to http://sogeek.us/meanonazure

Adding Users to BizSpark\Azure

One of the common questions I get is how to add additional users to your Azure / Bizspark account.

This is actually two different questions.

1) Add a user to be able to administer your Azure account

If you add a user to your Azure account it allows them to make modifications (spin up VMs create cloud services, modify active directory, etc..)

2) Add a User to your BizSpark account

Adding a user to your bizspark account DOES NOT add them to YOUR Azure account, what it does is gives them the ability to have their own Azure Sandbox to test and play around with.

Both of these things are valuable but it is important to know the difference and how to set them up.

 

Adding an administrator to your account.

To get started the account owner needs to sign on to their azure account at http://manage.windowsazure.com

Once they do this they will need to scroll down on the left hand side and click on settings.

Next they would click near the top middle where it says ADMINISTRATORS

And finally, click on the ADD icon at the bottom of the screen

image

Next they would need to add the MicrosoftID of the person that will be an administrator. This needs to be a Microsoft account (if they don’t have one they can sign up for one at Outlook.com )

Check the name of the subscription you want to add them to (In most cases there will be only one)

click on the checkmark at the bottom of form when done.

image

Once this is done they can sign on to http://manage.windowsazure.com and start using the account.

NOTE : If they already had a subscription connected to the Microsoft ID added in the step above, there will be one more step.

If they already had an account associated with that ID when they sign onto http://manage.windowsazrue.com  they will now see a Subscriptions menu item up next to their name.

To work on the subscription they were just added to, they will need to

Click on Subscriptions

Select the subscription from the dropdown

Click on the Apply button.

image

 

They will now be accessing the the account they were just added to.

 

Adding a user to your BizSpark account

Remember, this will not add them as and administrator to your azure account like above (although you can do both), but this will add them to your BIZSPARK benefit so they can have their own Azure subscription to test and play around in without using up any resources or credits in the main account.
(Thank you Bret Stateham, this was taken from part of his BizSpark Enrollment tutorial.  Head over there for more information http://bretstateham.com/bizspark-enrollment-tutorial/)

The first thing you need to do is to go to http://BizSpark.com 

Sign in using your MicrosoftID and then click on My BizSpark

image

 

· Then, under the “Manage” heading, click the “add or delete members” link

clip_image002

· Scroll down on the “Manage” page, and click the “+ Add” link, then complete the fields to add the new member (you will need to know the member’s Microsoft Account).  If they don’t have one, they can sign up for one at Outlook.com (I can provide steps if needed) ( I think that is shows only 7 people can be added.  If more is needed, let me know and I will check into that for you.)

clip_image004

· When you press enter, the row will be added, and the membership status will be “Pending” while an invite email is sent to the Microsoft Account email address.

clip_image006

· When the member receives their invitation email, they can use the link in the email to activate their membership

clip_image008

· When they follow the link provided, they will need to sign into BizSpark.com as their Microsoft Account:

clip_image010

· Then complete the enrollment with their Phone, and by Agreeing to the program:

clip_image012

clip_image014

clip_image016

· Once enrolled, they can use the steps described above to download software from MSDN and to activate their Azure benefit, and more!

clip_image018

To see how the added user can add their Azure Benefit, head here http://bretstateham.com/bizspark-enrollment-tutorial/#azure 

Hope that helps.

Daniel