Clash of the Titans in Automation: Python vs PowerShell

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's get ready to code in the left corner with the blue logo and developed in 2007 by microsoft we've got powershell and the challenger in the right corner created by hirofa rossum in we've got python let's see which language fares best when it comes to working with apis okay so much from a stunning introduction there i hope you guys like that good morning good afternoon and good evening and welcome to clash of the titans in automation where we're going to tell you everything you ever wanted to know and much much more about powershell and python here at citrix converts and yes before anyone else remarks in the q a i am fully aware that they messed up the uh the title slide the title is python versus powershell and i messed it up and i put the powershell logo first he's gonna hint you for days ljo yeah so sorry so talking about the q a after our session there will be a q a where you can pick our brains on anything related to our recession or to powershell and python actually so before we officially kick off uh please allow us to introduce ourselves my name is alto vergulic and as you might have guessed by my exit and my pronunciation i am dutch i coincidentally work for a dutch company called rawworks where i'm the principal consultant for the workspace domain i started coding way back in the days with microsoft basic and stuff like that and later on i dabbled a little with java university which was actually my first uh object-oriented language that i tried out i'm here with my good friend and colleague ryan ferris by gary yeah that's me so i also work at raw works as a technical evangelist and where we focus on the intelligent workspace so i actually started with uh powershell so therefore i'm also in the powershell corner it's this corner something here and um later in my career i shifted also towards the uh to c-sharp so uh and that's uh this is a different game but we're going to talk about powershell today and python you're going to talk about python i'm not oh yeah that's true that's true all right can you tell us a little bit about the company we work for rob works yeah of course so um i also i already mentioned a little bit in the introduction so we are a dutch consultant company focusing on the intelligent workspace so our mission is called mission one where we focus on making your work more intelligent and we use the entire citrix portfolio for that and especially also the micro apps so i really recommend to go look at the agenda of c2 converts because we're going to present multiple sessions now the one unique thing is that we have a wide spectrum of people so we have a lot of intelligent smart colleagues that do a lot of with implementation but we also have colleagues that can really code so we are a little bit of a hybrid company covering all spectrums from the it and that's uh that makes us quite unique in this market yeah as a company not as a person by the way hey i'm unique as well too very very true okay okay let's skip to the agenda guys what we got for you today is as following we'll start with a short introduction on powershell and python a general introduction next rein is going to talk a little bit more about apis and about rest and afterwards we're going to bring everything together and show you guys how you can use both languages to consume several citrix apis so you can start to automate your day-to-day citric related work right away so let's start with the clash and then i'm not talking about the 1918 band but i'm talking about the clash between the two programming languages yeah ryan is laughing because he really doesn't know what the clash is by the way before my time before his time yeah let's start off with what powershell is powershell is a task automation configuration management framework from microsoft consisting of a command line shell and an associated scripting language it was initially a windows component only known as windows powershell but it was made open source and cross-platform in august of 2016 with the introduction of powershell core where's the form is built on the net framework the letters built on.net core microsoft actually started working on powershell way back in 2002 and small trivia answering question for you uh it was called uh microsoft monet back then did you know that ryan no i didn't know no me neither but fortunately wikipedia did uh so next slide please and the next slide please yeah there we go okay on to my favorite programming language python and python is an interpreted high level general purpose programming language python design philosophy emphasizes on code readability and is mostly use of white space and indentation instead of under programming languages it was as we can see in the next slide developed in 1980 and we can a little bit be a little bit chauvinistic here because it was developed by a dutch guy called hiro from russom at the center for whisken and informatica cwi you didn't know that did you rhyme no i actually thought it was a python that created the python okay in a recent study done by stack overflow which is a well-known question and answer website for programmers we can see that the top used programming language is javascript uh python actually ranks fourth and powershell ranks sixth together with bash and shell nearly 41 of the developers who responded in the 2020 survey code in paris in python while that number is a 40 34 for powershell so i have to i have a quick remark though powershell is not a programming language it's really a scripting language but it's still listed as a programming language so yeah it's quite weird thank you uh what maybe is even more interesting is one of the questions on the survey was which programming languages do developers want to learn next and developers already work with other programming languages are most interested in learning python javascript and go powershell by the way is all the way back with roughly four percent now that can mean two things either people don't want to learn powershell of so many people on stackoverflow already know powershell that they don't need to learn powershell i think it's the last one yeah i thought you were gonna say that um so it's actually a very very interesting um uh survey and if you look at the next slide here is the link to the complete survey and a qr code for your convenience as well where you can read all about the uh the surveys really interesting read why python is so complex that people still need to learn that's my conclusion of that slide yeah and i'm gonna say that only four percent of people want to earn powershell because they really don't like it okay next slide so now that we know what both are let's look at some similarities and differences uh python and powershell are actually both object-oriented languages one slide-back please so both languages have the concept of objects that can be reused to perform specific tasks both languages also use code by means of importing modules and these modules can be potentially reused in other programs or you can directly import the modules created by other programmers so both languages also have a very large library of modules that you can pick from the internet and use also coincidentally powershell and python both use the hash sign to comment out parts of the code if we look at a couple of code snippets here you can see that there is a slight but very important difference between the two languages powershell for instance is very heavily dependent on parentheses and curly braces uh respire shell uses indentation so white space in order to accomplish the same thing so let's look at a couple of differences um variables in powershell always start with a dollar sign python on the other hand does not use any other symbols to distinguish variables for numeric variables in both languages we can do some basic arithmetic similarly in powershell and python alike as you can see in line four on both ones both languages in case strings in double or single quotes at least i think it's double or single quotes can powershell also use single quotes yeah of course the the big difference when you use at least the variable within a string and you use the double quote the variable is printed if you use the single quote where the variable is in the string it's it's flattened so the variable is not used anymore okay so that's a important one yeah uh i would think variables we can use so also use some string manipulation method for instance to convert the string to uppercase uh it's called to upper in powershell and in python uh we just use upper for boolean variables uh this can either be true or false the boolean variable in uh powershell is preceded by a dollar sign again whilst in python we don't do that but we do have to capitalize the first letter of true or false and if we look at conditional statements like the if statement as mentioned before in line 15 through 20 for powershell it uses curly braces whereas as you can see in the python the python code snippet it uses indentation instead of curly braces quick question also yeah why is there false on the left side on line 18 and right and through on the python that is a very good question because i was a little chaotic when i'm writing these code snippets thank you very much for that by the way okay another bombshell let's skip to the next part uh apis and rest and the rest is this joke yeah so let's talk about the api and i know that eltjo added all handy details and notes in a slice that i cannot see at this moment so i have to do the top of my head but let's first talk about the the api itself and it stands what does api stand for the following thing the a stands for application the b stands for programming and the i stands for interface there you go application programming interface basically what it does it's it's another ui to talk towards an application to get and retrieve post data or manipulate data and what you see that in a lot of applications an api is used for communications towards a backend component but also a lot of applications open their api to allow others use this api to create their own integrations one perfect example of course are the citrix microapps where you can leverage these apis to get and manipulate the data and use it in another interface so if we look at a nice example and i think everybody can relate to this one it's called twitter and you can use the twitter interface to write a tweet or how it here calls tweten as it's dutch now if you go to the documentation and i have to provide credits towards twitter because they document their api very very well you can here see all different kinds of post gets to at least get all tweets or post a tweet now a perfect example is tweetdeck so tweetdeck started as an independent application leveraging this api allowing to have multiple like swimming lanes to collect tweets and schedule tweets and now um tweetdeck uses the uh the api of twitter so it's building an entire standalone application based on the apis which is really unique and shows the possibilities what you can do using the apis now let's talk about rest and res stands for this i have to state that i cannot uh pronounce difficult uh difficult day so i'm not gonna burn on this i'm so sorry for you guys yeah then i make sure you pronounce this but uh res is basically an um an api architecture that you can use for the standards so we have an example for this so if you look at the the rest um it's like guidelines also i think it is right yeah they call it an an um architectural style they specifically say in the documentation that it's not a standard but it's an architectural style okay so if you look at the rest style you can see here a url and in the url we specify the api.example.com which shows that we are in the application programming interface what we just learned yeah so the funny thing about rest is that the information you retrieve is called a resource which is funny because if you look at the acronym for url which stands for uniform resource locator so there you see everything comes back together again ah oh that's a good one now if you look at users this is the resource that's gonna be leverage the in this specific api so we're going to collect the information from the resource users and what you see behind it is the user names.json which is the actual object we're going to retrieve in the json format so based on the url we're going to retrieve all user names from all users in this specific application are there any notes still in the in the slide that i missed ljo uh no no no no you're good you're all good perfect now there are different um methods how they called where you can manipulate or retrieve the data and the first one is is the get ah nice animation animation by the way so what we do here is we call the usernames and retrieve the json file towards our client located on the left side now we can also do a post where we post the usernames information backs towards the application and we also have a delete where you delete all usernames so those are basic methods that are um yeah depending of course on the application available one thing that we didn't listed is the put you can also update records in the uh in the items yeah i was too lazy to put it into the animation i'm so sorry it's also a difficult animation of course now if we and this is an important one by default you have response codes so when using the api or the rest api you can leverage the response goes to see what's going on of course when you're getting information um you will eventually retrieve the information so you already know if it's successful but especially when you post or delete data or put data it's very important to validate the response code to see if everything went okay now there are a couple basics here now you have the one series which is informational communication or codes you have the two numbers that there's a success yeah that's basically the one you want exactly then you have the free which is basically a redirection and this especially uh handy when doing an authentication where are you gonna be redirected towards multiple resources you got a four and i think the four one is uh clear for everybody because everybody experience a 404 on a website which is basically an error code on the client side and you have five error which is an also on server side error so these are important at least also when you do a coding style and to at least validate ah the 404 i know this one yeah we've all seen this one i guess yep okay thank you very much ryan uh well so without further ado now that we know the differences and similarities between both languages and we got a hopefully a good understanding about apis and rest the architectural style we can start to put it all together and for that we need to have some laser groundwork and for powershell it's not that hard but for python we need to install a module before we can work with apis next slide please okay so we need to import a module called requests i'm not sure if the video is playing automatically right i'm gonna play it now but you did the voice over so we're gonna listen to the voiceover yeah let's go so first of all for python we will need to install a module called requests for that we need to open a command prompt and use pip to install the module to install the module we can use the command pip install requests i love the typo there we go easy as that so now that we installed the module we can use it later on uh but we're not there yet uh because with powershell we also need to do some ground work uh what does this do exactly line yeah so f hopefully everybody understands that powershell is available on every windows device so any windows 10 operating system but also the windows server operating system but by default um powershell five point something is uh installed into 5.1 yeah so in order to use the latest version you need to run this following command which will install the latest version in this case this version seven yep okay so next up before we can do anything most apis that we're going to use require authentication and in this case we're going to use several citrix apis and for the citrix apis the first thing we need to do is to set up api access and this is again a movie lines if you could please play it there we go we can use the citrix sphere to apps and desktops orchestration apis to perform various tasks like publish an application or enumerating session with these apis but before we can use the api we first need to authenticate while some apis don't require any authentication a lot of apis do require you to authenticate one of the methods that is used is called bearer authentication with better authentication we create a so-called bearer token to create the bearer token for the cvet server orchestration apis we'll first need to have a api client this api client can be easily created in city cloud management console in console click on the hamburger menu and yes it is really called the hamburger menu navigate to identity and access management in identity and access management go to api access here we can create our secure client let's call it powershell versus python the client make sure to copy the id and the secret because we know this later also take the note of the customer id because when it will need this as well in my case it's called citrix ctp acc oh what a lovely voice i have by the way okay indeed and we got two el chose here okay if you show the next slide please ryan um now that we got the uh all the prerequisites we need to know some information about the api and specifically we need to know where the resource that we want to use is located and for that we need to check the documentation for the api so luckily citric done a great job the last couple of months of documenting the apis uh although i must admit they aren't really up to par with twitter's apis just yet so if you look at the api documentation for instance for the orchestration apis in the next slide and we can see here that there's a lot of information about the api we do not only get the resource location which is slash tech preview slash me we also get a list of parameters that we need to put in uh the response or the request actually and also we get a list of possible responses that we could get back from the api if you look at the bottom uh if we have a good if we done a good request we should get a code 200 back and as you recall from ryan's response methods slide 200 means okay so let's dive into our first example um so we'll start with some code about uh with python and the first thing we want to do um is to import and what we're going to import is we're going to import the module request remember that it's the same module we just installed with pip we also need to import json uh this is a model that is included with python standard so we don't need to uh install it on line five we'll start to build up our url and in this case it is trust.citric rustbase rp.customerid slash tokens slash clients we substituted the client id for our client id which is citrix acc ctp acc and in line 8 we will create our header and in the header we tell that the information we want to retrieve is formatted as json so in lines 10 to 14 we will build up our payload which will contain the client id and the client secret uh oh yeah by the way if you are a smart ass i already recycled the client id and password so if you want to use it tough luck so now that we got the uh url the header and the payload we can start to do the request and for that we'll use request.post because we're going to do a post action and in parameters we'll pass the url we'll pass the payload as json and we will pass the headers and we'll save the uh request in the my request variable and we'll encase everything in a uh error handling loop with try and accept so if everything went well and we got a response go 200 back which we can view in line 24 we should have our token available in line 28 so now that we got that in python let's look how that see how that looks in powershell one thing i got one question i got one question though on line 19 you stated uh yeah this except for only the uh exceptions only request exceptions yeah so in either other case you will not handle it in this tri-catch right no no no i'm only handling a request that is not 200 or a 201 or a success okay just double check yep no problem now there's one sneaky thing if you look at the code is that eltjo added a lot of enters to at least look like similar length but we can do it way way quicker when using powershell so let's go over the details again we have to specify in variable using the uh the url and here you can see ljo decided to use single quotes which still works it's the same url's previous example now in case of defining the header we also need to specify the content type in this case it's going to be application json also know that there are other formats that can be used in apis now this is done using the curly brackets so this is an important thing that that needs to be specified in this case because it's going to be an object now also for the payload we're going to specify the client secret and the client id and again these are recycled so they cannot be reused now here we have a try catch specified and within the tri-catch we do an invoke rest method we're going to use the method post because we're going to post the information using the headers and we're using the body and the body will be converted from a powershell object towards json so it can be accepted by the api everything will be stored also here in the variable my request which will be shown at the line 26 for the token now one thing here is that within the catch if something goes wrong and in this case we're gonna catch everything so not only um exceptions on the rest method it will be displayed with the status code now please note it states right host and then it will break out so it will just continue with the code usually you want to at least specify the error so the script will eventually stop or you want to handle it and do some other stuff it's depending on your use case of course yeah i was a little bit lazy encoding that i'm very very sorry people don't take my example um so if we look at the next slide we can see what we get back as the response and here we can see our token that we got back um so now that we got the token uh and we got the customer id let's move to the next slide and see if we can actually retrieve some data and for that we want to see what resource locations a specific customer has in its uh civic cloud environment as you can see in this slide one background one back one back oh sorry i was going too quick yeah we we need the customer id and the bearer toggle so luckily we have both if we look at the example again now we can move to the next slide yep we'll start again with python and in line 52 we have the new url uh which again has the customer id embedded in it in line 45 uh please note that the authorization because we're using vero authentication we need to precede our token that we just got by cws oath bearer is don't know why but the documentation states we do so we're going to do that there's also a quick note on that is that some apis of citrix are using different token methods so please be aware in the documentation to validate that yeah and also autoresacci means authorization in dutch yeah yeah again so sorry so sorry uh but as ryan said citrix isn't really concise in how the authentication works across the different api so you can might as well we have another authentication so please read the documentation on that if you want to use that slide um 56 we are going to set up the header in this case we're gonna tell it again that application type is json and as an authorization we are going to pass our new bear token as you can see on line 62 we are going to have a get method an http get to request the information and we will pass the url and the headers s2 parameters uh and in line uh 56 we can see the status code hopefully there will be 200 and in line 66 we can print the content but did you spot my mistake in the code here because i completely forgot to use any error handling on this request shame on me so let's see if ryan did any better with the powershell code yeah and there you go now again here if if we look at the lines they're quite similar but here is some some try catches or at least added but again we need to specify the url for the api in this case again the resource locations also here we need to define the authorization variable or the authorization variable here by the way using the bearer plus the token so we're combining both strings and then we're going to write the the authorization variable so we're going to specify this in the uh in the header and i note that you didn't add the application json here nope i don't i don't know why but i think it works yeah funny thing is it does work without specifying that interesting and then uh in the tri-cats we're going to invoke at least the rest method using the url specified in on line 50 and the header that we just specified on line 55 which will be the thing about this is as you can see we need to specify that we're going to use get with python but with infogress method if you don't state any method it is explicitly of implicitly supplied that you're going to use a get yeah so that's the default defined method yeah which is also documented on the partial documentation which is well-defined by the way so at the end in line 64 we're going to list all the items that we retrieve from the api so i have to admit this is quite similar towards the the python part yeah so and if we can skip the next slide we can see what we get back then uh we got a list yeah sorry all blurry values all blurry values yeah gdpr you know okay so um let's move up to the final example uh which is actually a pretty fun one uh we're gonna show you guys how you can send a notification to admins in the civic cloud portal but what we're going to do we're not going to specify anything out because it's going to get a little bit repetitive um there is one thing though if you look at the prerequisites again the only thing we need again is the in the next slide is the customer id and the bearer token as you can see at the cup on the prerequisites we also have url that we can call to send the notifications and we can see that is a post method there's one thing though it says in a note you have to replace acme with the customer id and we have to generate a good and this is something new that we haven't looked at yet so uh and now we get somewhere and ryan's gonna be have a field day here because to generate a good in powershell you only need one line of code yeah it's the the new dash guide and it responds at least a good object and i have to add one note when you want to use the gui you need to specify that grid of the object of course to get the entire grid string so this is an important thing at least to provide deck it's two lines of code with powershell okay and i have to add a single one you can also do it in a single line i know i know i don't know so if we look at my favorite python it is a little bit more work because you cannot create a good without first installing a module and a module's called uuid and we have to again use pip to install that again once we've installed the module pip we can import sorry the module pip once we use skip to install the module uuid we can then import uuid and we can generate uuid with uuid dot uuid one so takes a little bit more work so points for you on that part there drying guys did you also know that eltjo is using powershell to install python it's a free one more credits the word powershell yeah i didn't even without his death okay i like powershell a little bit okay i must have did that so now that we looked at the couple of examples you might be wondering what else can we do with the citrix apis well actually the possibilities are endless and if you look at the new uh cfat services rest api tech preview we can see that citrix is really aiming to make it possible to automate every part of the product with the apis the list goes down and down and down and they are really well documented and with these apis you can do anything from creating a hypervisor connection to setting up a golden image publishing your image to a machine catalog updating machine catalogs the whole shebang but what if you want to use an api that is not documented or you're not sure if there's an api there's probably a way to do that right ryan yeah so if you open any modern browser and i think it's even supported in internet explorer but when you hand the f12 you will get the developer information and when you go to the tab network and this works for chrome firefox edge i'm not sure uh safari or oprah but i think it works there but you can see all the network transactions that take place so it will retrieve javascripts and images and all stuff but you can also see here the all the api calls that are used so this allows you to have a sneak peek into the the web site what api is leverage but you can also use in your own automation yeah so what you can see here also in the in the header you have to request url which is basically the status information for c2 cloud yeah there's one caveat in this though because these are undocumented apis citrix can either change them or remove them without any prior notice at any moment so if you're going to use them use them at your own risk and there might be also authorization involved which needs to be figured out and can be quite complex to uh to get all the details yeah and we now have the example here to use the citrix status page but if you use citrix cloud virtual apps and desktop services the new web studio ui is also completely built on apis so ah 37 minutes went into this presentation 37 minutes was actually way way too short to tell you guys everything you want to know about powershell and python and apis i did promise i wanted to tell you more everything you need to know and more but we just don't have enough time we do want to leave you guys with a couple of coding best practices when you work with code um first thing always use versioning and to do that you can best use a versioning control system like git please please please do not confuse git with github git is a version control system whilst github on the other hand is a cloud-based hosting service for git repos so there's a lot of best practices using git and versing in general so i really recommend to to try google it but i especially use branching at least to separate the production code for developer code yep very good one second please always adhere or try to adhere to coding standards for powershell and for um python as well there are coding standards now for python it's called pep8 naughty what's called prepaid which is a very long document which states the stylistic best practices you can use in writing your code and if you use coding standards it makes your code more readable for other programmers and it means that you write more concise and consistent code now i also have a note on this one coding standards can be public coding standards but it can also be internal or private coding standards defined by your company so make sure everybody is at least aligned on your coding standards okay so going into the next one is comments now it's important to write readable quotes and you can at least leverage comments to specify that but i know that elton has a very nice phrase on this one documentation yeah i know but don't think what it says is oh yeah um always no contradictional comments is worse than no comments at all so if you're going to comment your code please make sure that it's correct comments but you know generally speaking you might know today what the variable t1 does and why it is used but are you also sure that you can remember that six months in because i sure as hell don't there's uh there's sometimes the debate on keeping code clean without comments or just commenting the words yeah in scripting so it's a little bit depending on your preference and your coding standards and your coding standards very good and the last one and that's also one which you always have to do and i didn't adhere to that in all my examples please always try to use as much error handling as you can that makes your code more robust uh then when you don't use coding sorry error handling and also enforce you at least to uh to think about all these scenarios and situations so you try to at least handle it but it's uh it can be complex yeah yeah so that actually brings us to the end of the session we're 40 minutes in thank you very very much for watching uh please also do check out our other session uh ryan and i are presenting another session this uh converts on the devops way of maintaining citrix microwaves uh furthermore my other colleagues are works are also hosting free sessions so please make sure you check them out as well uh again thank you very much for watching and take care cheers bye guys
Info
Channel: Citrix Developer
Views: 1,143
Rating: 4.8095236 out of 5
Keywords: Citrix, Developer, SDK, API, Tools, Python, PowerShell
Id: 98H6iS0EG4s
Channel Id: undefined
Length: 40min 58sec (2458 seconds)
Published: Thu Nov 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.