PowerShell 7 Tutorials for Intermediates #8 : Working with XML

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome into the eighth video in our Powershell 7.2 for intermediate series in this video we're going to be taking a look at how to parse XML files and also we're going to take a look at how to generate XML files using Powershell and also how to import those files so one key thing to note is Powershell does have a commandlet called import CLI XML um so we're going to see how to use that but for the most part if you are going to be using your own generated XML file either from your HR application or an XML file that you've written yourself similar to this one here that I've created for a list of employees you won't be able to use that commandlet so I'm going to show you guys how you would import these types of XML files into your Powershell scripts so let's actually go ahead and let's get started here so the first thing we're always going to have to do of course is create our variable with our file path so let's create a variable called XML file path and we are going to make that equal to our XML file path here and then what we're going to want to do is we're going to want to import this data as an XML data type so what we can actually do is do an open and close square brackets and put the letters XML in our square brackets and then we're going to create a variable called XML data and we're going to make that equal to get content and we are going to pass in our XML file path as the path and then what we're going to do is we're just going to take a look at what that looks like once we have that imported here so let's just run all of this so what we actually have is an XML object here so we have our XML type and our employees object so if we go into this XML here this is our XML version this is a required line for a valid XML file and then what we have is we have an object called employees and then in employees we actually have an object called employee which has all of our employee details and we have four employees in this file here so what we can actually do is we can actually do a DOT notation so XML file XML data dot employees and then we can actually say dot employee and if we actually just run that here we actually get all of our employee data now what I would do is I would then store this into a employee data variable and then what we can do with that is do a for each employee in employee data open and close curly brackets and we can actually just run this all together right away as we know this will actually give us our DOT notation on the employee object and as you can see we can actually see that we have our dob purchase date of birth employee ID um first name there should also be a last name as well and a title so what we can actually do here is just do a right output and we can do employee and then we can actually put employee Dot employee ID as name of and then we can put the employee Dot first name and we can go ahead and we can actually put the employee dot last name here and if we actually run this Brew on the for each Loop uh let me actually just accidentally created a whole bunch of these here all right there we are so we actually have all of our employee data printed on the screen here we have our employee IDs and then we have our names so you would actually be able to use this data in this Loop and loop through all the employee data compare it to your active directory or compare it to another file and detect if you have like any new employees or employees that should be removed from your system or if one of them has changed names you can detect all that information right there so again this would be for your own generated XML files this is the way that you would treat them you would treat them using the get content and then you would make sure that you always create that variable type with XML and this way it's going to create that object for you now another very uh good way to generate or import XML files in Powershell is the CLI XML now if you try to use the import CLI XML on a custom data you will get an error so let's actually let's test that out here so let's do CLI data and let's make that equal to import Dash CLI XML and then let's try to pass in our XML file path here if we try to run this here we will actually see that the element of wish namespace and then it provides us a Microsoft schema was not found so it does actually expect very specific object style but let's actually see how we can use the Import and Export CLI XML so one of the ways that we could actually use it is let's say you wanted to generate a XML of all the services on your computer so you can do a get service and then what we could do is do an export CLI XML and provide our path here so let me just provide a path here for me which is going to be C Scripts and then intermediate tutorials and then we are doing the XML files and we're going to name it test export.xml so if we actually run this here and we actually look at our XML file now as you can see this looks a lot different compared to our XML file that I created myself um but this actually is all created for me by Powershell like I didn't have to put anything in there there's that line that it expects right away and what this does this actually stores the different data types as well that it is so we can actually see that it is storing system.service.service controller which is the actual type of the object that we've created so what actually happens here is if we just do a get service again and we pipe that to get member which we've seen before in previous videos we will see if I just expand this out here we will see that it gives us a type name of system.service.service controller startup type and then we have all of our properties and methods of course that we can perform with those but now if we actually go ahead and create a variable here called service data and we have mixed actually make this equal import Dash CLI XML and then we provided the path and let's just provide it the path here to our XML data that we've just created and we actually import this and let's just take a look at it real quick first before we actually inspect it so as we can see we can actually see all the services here we can see all the different status name and display name so it actually kept all that information for us and actually if we even do a get Dash member on it we will actually see that it is a deserialized system dot service dot service controller so it actually kept its data type so it it is still a system.service.service controller data type so we can actually do a lot of information with that it's definitely a lot better than just storing it in a CSV file all those CSV files do work um another really great thing that you can do with import CLI XML as we've seen in one of the other videos that I'm actually going to put a link to that video um in the description down below but you can actually use the export and import CLI XML to store secure strings now of course there are other ways to store passwords but this is a way to store a password that will be locked to a machine and to a user um and then this way you can not have plain text in your scripts if you do anything at least it's better than putting it into plain text of course you can use secret vaults as well that provide a little bit of an extra layer but if you're going to do the pure basic that would be the best bet instead of just putting it in plain text in your scripts which would obviously not be recommended so there are a lot of uses for this Import and Export CLI XML like I said I'll be putting that description to that video um I'll be putting the link in the description for that video because I think that is a very useful video where you can use this functionality so hopefully that kind of helps you guys use XML files in your Powershell scripts they're great for storing configurations just like the Json files are or storing data like the Json files and CSV files gives you a little bit more flexibility because you can create object types similar to Json which gives you a little bit more benefit compared to a CSV file Json and XML are definitely very useful but CSV files are useful because you can open them in Excel and you can actually share them with the non-tech people in your group so hopefully that helps if you guys have any comments or questions please let me know in the comment section below if it's something specific I will try to answer you directly it's something a little bit more generic that a lot of people can benefit from I will try to create a video for it um also be sure to hit that subscribe button hit that like button also hit that notification Bell to be notified when that next video comes out and I will see you guys on the next video
Info
Channel: JackedProgrammer
Views: 3,109
Rating: undefined out of 5
Keywords: powershell 7, windows powershell, windows 10, visual studio code powershell, programming, coding, scripting, powershell, powershell for beginners, how to start with powershell, scripts, powershell 7.2, powershell 7 windows 11, windows 11, how to, methods in powershell, powershell scripting, functions in powershell, cmdlets in powershell, powershell tutorial, api, powershell api, xml, powershell xml parsing, powershell xml, export-clixml, import-clixml, xml files
Id: AhMuVOwk--8
Channel Id: undefined
Length: 11min 14sec (674 seconds)
Published: Mon Dec 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.