Powershell Pointers

EnticlesEnticles Member Posts: 69 ■■■□□□□□□□
Hi All,

I am struggling with wrapping my head around Windows PowerShell. the inner nerd inside of my head wants to be a masterful script creator using windows powershell, however whenever i try to write scripts i find myself getting lost in the syntax and not creating anything that even remotely works.

Thankfully the ISE does help me by providing hints about what the strings are for each cmdlet, but what it doesn't help me with is the logic behind building scripts that do multiple things and/or trigger a cmdlet based on a set of criteria being met. for example, something really simple: Deleting all hyper-v VM's in windows 10, i like to call this the "lab squashing script".

I have found numerous scripts online that do the trick just fine with individual VM's, which i have been re-purposing for my needs. However i am challenging myself to create my own script that achieves the same thing and once done will be comparing the code to see how efficient or inefficient it is.

The script i have compiled that does not work as intended is as follows:

Get-VM -Name * | Stop-VM | Remove-VMSnapShot | Remove-VMHardDiskDrive | Remove-VM -Name *

the goals are:

1) Retrieve the names of all Guest VM's installed
2) pipe this data into a cmdlet to shutdown all running VM's such as stop-vm
3) Next i would like to remove any snapshots for the VM's
4) Then i would like to delete all vhdx files associated with a VM, this is where i start to struggle. I have found remove-vmharddisk but that doesnt work the way i want it to.
5) Lastly, i would like to delete the guest VM object from the Hyper-V manager.

To be clear, i am not looking for someone to write a script for me, i would much prefer some pointers on how to go about learning the nature of the language and how to put together scripts that are more complex than "1 liners"Thanks in advance :)

Comments

  • DojiscalperDojiscalper Member Posts: 266 ■■■□□□□□□□
    "Powershell in a Month of Lunches" helped me learn enough of the syntax to get through the 70-410. I only needed to first few chapters. Doesn't sound like what your looking for, but I'm sure it will help you understand what MS is trying to do with powershell.
  • knownheroknownhero Member Posts: 450
    Enticles

    You'll need to do a foreach loop on each of your VM's and then do your code. I still want to write one liners as it looks sexy and boss to see one line do so much, but I have come to learn this... If you're code works, it works. So don't worry about making it look pretty.

    Start from the first part you want ALL your VM's by name so you'd have this:

    Get-VM -name *

    Seeing as you want to stop all VM. You might want to ask "Hey I want to turn them off, do I just want running ones"

    Now this will simply write these to your screen. So you want to store it in a variable:

    $vms = Get-VM | ?{$_.State -eq "Running"}

    So your $vms will now return you only Virtual Machines that are running.

    the ? in the above code is short hand for where. $_. means the current object and you want the STATE of that current object. In this case we're checking to see if its running.

    Now in your ISE (Or command) you can type $vms and press enter, this will output all your running machines. There is part one done.

    Here is where your FOREACH comes into play

    You want to loop through each running machine and STOP them.

    $vms = Get-VM | ?{$_.State -eq "Running"}
    foreach($vm in $vms)
    {
    #code in here
    }


    I think from here you can easily guess whats needed.

    But the reason why your code doesn't work is because it has a collection of object and has no idea what to do with them.

    Just make your code as long as it needs to be when you're learning. I hardly write one liners. And if anyone who tells you that you HAVE to is an idiot.

    Any questions feel free to ask.
    70-410 [x] 70-411 [x] 70-462[x] 70-331[x] 70-332[x]
    MCSE - SharePoint 2013 :thumbup:

    Road map 2017: JavaScript and modern web development

  • AndersonSmithAndersonSmith Member Posts: 471 ■■■□□□□□□□
    Are you wanting to dive really deep into Powershell or are you wanting to know enough to pass the MCSA exams? The Powershell covered on the exams really doesn't go too deeply into scripting, mostly just what the Cmdlets do. Of course it never hurts to have more knowledge of a subject than you'll need. I didn't really know anything about Powershell until I started studying for the 410 and I learned enough throughout my MCSA studies that I'm now easily able to incorporate it into my every day job functions. I learned a lot by using Technet and Google searches. Pluralsight and Infinite Skills both have excellent courses specifically on Powershell that really helped me understand it as well. Of course trial and error is also the best teacher.
    All the best,
    Anderson

    "Everything that has a beginning has an end"
  • Cisco InfernoCisco Inferno Member Posts: 1,034 ■■■■■■□□□□
    I do not think you can pipe into so many like that, you will need a loop. (programming 101)
    2019 Goals
    CompTIA Linux+
    [ ] Bachelor's Degree
  • EnticlesEnticles Member Posts: 69 ■■■□□□□□□□
    "Powershell in a Month of Lunches" helped me learn enough of the syntax to get through the 70-410. I only needed to first few chapters. Doesn't sound like what your looking for, but I'm sure it will help you understand what MS is trying to do with powershell.

    Any insight into Microsoft's logic is helpful, so i will check this out for sure - thank you :)
    Are you wanting to dive really deep into Powershell or are you wanting to know enough to pass the MCSA exams? The Powershell covered on the exams really doesn't go too deeply into scripting, mostly just what the Cmdlets do. Of course it never hurts to have more knowledge of a subject than you'll need. I didn't really know anything about Powershell until I started studying for the 410 and I learned enough throughout my MCSA studies that I'm now easily able to incorporate it into my every day job functions. I learned a lot by using Technet and Google searches. Pluralsight and Infinite Skills both have excellent courses specifically on Powershell that really helped me understand it as well. Of course trial and error is also the best teacher.

    My short term goal is to pass the exam, but realistically i would like to be able to work confidently and effectively within powershell. My current exposure is merely the simple cmdlets i am coming across in course materials, but deep down i would really like to be competent at creating scripts to automate tasks which i currently do through the GUI or through cmd commands.

    knownhero wrote: »
    Enticles

    You'll need to do a foreach loop on each of your VM's and then do your code. I still want to write one liners as it looks sexy and boss to see one line do so much, but I have come to learn this... If you're code works, it works. So don't worry about making it look pretty.

    Start from the first part you want ALL your VM's by name so you'd have this:

    Get-VM -name *

    Seeing as you want to stop all VM. You might want to ask "Hey I want to turn them off, do I just want running ones"

    Now this will simply write these to your screen. So you want to store it in a variable:

    $vms = Get-VM | ?{$_.State -eq "Running"}

    So your $vms will now return you only Virtual Machines that are running.

    the ? in the above code is short hand for where. $_. means the current object and you want the STATE of that current object. In this case we're checking to see if its running.

    Now in your ISE (Or command) you can type $vms and press enter, this will output all your running machines. There is part one done.

    Here is where your FOREACH comes into play

    You want to loop through each running machine and STOP them.

    $vms = Get-VM | ?{$_.State -eq "Running"}
    foreach($vm in $vms)
    {
    #code in here
    }


    I think from here you can easily guess whats needed.

    But the reason why your code doesn't work is because it has a collection of object and has no idea what to do with them.

    Just make your code as long as it needs to be when you're learning. I hardly write one liners. And if anyone who tells you that you HAVE to is an idiot.

    Any questions feel free to ask.

    This is fantastic - Thank you! I will have a crack at writing a script myself with as little reference to your example as possible and see if it comes out any differently. Depending on its outcome i may post back with my result for additional pointers / critique :)

    To go back to AndersonSmith's original question - I would like to be able to throw together scripts in pretty much the same way that knownhero just threw one together for me. the end goal is that level of competency within powershell.
  • AvgITGeekAvgITGeek Member Posts: 342 ■■■■□□□□□□
  • AndersonSmithAndersonSmith Member Posts: 471 ■■■□□□□□□□
    Enticles wrote: »

    My short term goal is to pass the exam, but realistically i would like to be able to work confidently and effectively within powershell. My current exposure is merely the simple cmdlets i am coming across in course materials, but deep down i would really like to be competent at creating scripts to automate tasks which i currently do through the GUI or through cmd commands.


    To go back to AndersonSmith's original question - I would like to be able to throw together scripts in pretty much the same way that knownhero just threw one together for me. the end goal is that level of competency within powershell.

    That's awesome that you're really wanting to learn how to actually use Powershell rather than just what you need for the exam. I think that's what sets the true professionals apart from the ones who just simply want to pass an exam so kudos for that! That being said, if your short-term goal is to pass the exams you might be better off right now focusing on the Cmdlets specific to the exam and how to manipulate the environment using them. Find a task that you know how to do in the GUI, such as adding a DHCP scope for example, and learn how to do it in Powershell. One thing I did while studying was to find check boxes, drop-down menus, etc in different screens and then look up how to change them in Powershell. I'd change them in Powershell and then open up the screen in the GUI to see the change. I pretty much left no stone unturned. In doing so I was able to really learn the ins and outs of Powershell and how to use it in my job to make life easier! Of course I say all that but I'm sure it doesn't hurt to over-prepare yourself by learning more than you need for the exams, I'm just thinking if you have a certain time-frame in mind you'd like to be done by it might be better to start off with just what you need for the exams. I remember when I first started looking up info about these exams I was really nervous about Powershell because so many people had said they'd had problems with it and that the exams were very Powershell heavy (and they were), but I didn't have much trouble learning the Powershell once I started investing my time in it. Again though, I give you major respect for wanting to actually learn the technology rather than simply pass the exams. That's what makes a successful SysAdmin!
    All the best,
    Anderson

    "Everything that has a beginning has an end"
  • knownheroknownhero Member Posts: 450
    Just to put my two cents into the whole "PowerShell is hard"... It's not that hard. Once you get the understanding of how it works you'll know what to look for and how to tackle tasks.

    The only reason its hard for the exam is you're unable to do get-help on text and you have to know the commands they're showing you.

    The more you do at home the easier it is. For example I was on SharePoint technet, someone asked for a script to do something and me doing that for them allowed me to ace a question in my exam because I knew the syntax.

    This might be NDA so please let me know if I have over stepped my bounds..

    The type of questions you'll be asked is to "finish" off a command. I would 100% learn the short hands for... foreach (%) and Where(?) you might see that in their examples and it would be best for you to understand.
    70-410 [x] 70-411 [x] 70-462[x] 70-331[x] 70-332[x]
    MCSE - SharePoint 2013 :thumbup:

    Road map 2017: JavaScript and modern web development

  • EnticlesEnticles Member Posts: 69 ■■■□□□□□□□
    the shorthands are some of the most confusing aspects for me, to be completely honest i had not invested much time into researching powershell so when i saw scripts with shorthand notation in them it absolutely screwed me. so those little tidbits alone have already made me understand powershell that little bit better - so thankyou for that :)

    I had read AndersonSmiths approach to powershell in another thread in this forum, and i did think to myself that was a very sound approach to preparing for the exam, subconsciously i was already considering taking that approach to exam preparation - and now i think i may just go full speed ahead with it.

    Really appreciating the insight people, thank you so much!
  • QordQord Member Posts: 632 ■■■■□□□□□□
    AvgITGeek wrote: »
    Check this tutorial out. One of the people providing the training is Jeffrey Snover.
    https://mva.microsoft.com/en-US/training-courses/getting-started-with-microsoft-power-shell-8276?l=r54IrOWy_2304984382


    This is also what I'd suggest for intro to powershell learning.
  • EnticlesEnticles Member Posts: 69 ■■■□□□□□□□
    Qord wrote: »
    This is also what I'd suggest for intro to powershell learning.

    Unfortunately life has been interfering quite significantly with my studies, I am prone to quite harsh migraines and have been suffering from more than i am usually accustomed to. This means time that should be spent studying after work is spent in bed with a pillow over my head/eyes trying to sleep off some pretty significant pains inside of my head.

    lame excuse aside, i have watched the first of these videos and it looks like their "take you by the hand" style of introducing powershell is going to serve me well, thank you so much for this resource :)
  • shochanshochan Member Posts: 1,014 ■■■■■■■■□□
    I definitely feel your pain about Powershell, been working through the edx.org Powershell course & it has been tough to grasp for me too...I am glad this forum is here for us!

    Have you heard of this migraine remedy? https://www.youtube.com/watch?v=FzHv71JuzsA
    CompTIA A+, Network+, i-Net+, MCP 70-210, CNA v5, Server+, Security+, Cloud+, CySA+, ISC² CC, ISC² SSCP
  • EnticlesEnticles Member Posts: 69 ■■■□□□□□□□
    that is really fascinating. I knew about the pressure point but didnt think a piercing would be a viable option.

    I have been seeing an acupuncturist on an irregular basis about my migraines and what he had done in addition to an acupuncture treatment was put a few small ball-bearing style objects on the cartilage of my ears, they have an adhesive that slowly disintegrates and the ball-bearings simply fall out off your ear cartilage when it does.

    his treatment advice was telling me to put pressure on these ball-bearings with my finger or thumb when i suspect i am about to have, or am currently experiencing a migraine.

    i didn't have a strong enough improvement to really know if these things stuck to my ear really helped or not, but considering how frequently i have been getting migraines lately i should probably go pay him another visit... hahaha.

    anyway, i am digressing. Will report back once i have had chance to go through the MVA course and let anyone know who is wondering how much of a help it was :)
Sign In or Register to comment.