trying to understanding how PowerShell deals with scripts inner to outer.

Welcome Forums General PowerShell Q&A trying to understanding how PowerShell deals with scripts inner to outer.

This topic contains 8 replies, has 3 voices, and was last updated by  brian catlin 6 months, 3 weeks ago.

  • Author
    Posts
  • #97223

    brian catlin
    Participant

    I started working on a script to get CPU information and found I could use Get-WmiObject -class Win32_processor. I then started running into some other problems and lack of understanding.

    For example I wanted to get the processors for all servers on the domain and so I molded together some code. (mind you was not worried about most efficient here but just why or why it does not work and how to approach).

    following script:

    Get-WmiObject –class Win32_processor -computername (get-adcomputer -Filter{OperatingSystem -Like '*Windows Server*' -and UserAccountControl -notlike 4098} -Properties ipv4address, OperatingSystem, OperatingSystemServicePack| Select Name, ipv4address, OperatingSystem, OperatingSystemServicePack ).name

    output below:

    Caption : Intel64 Family 6 Model 63 Stepping 2
    DeviceID : CPU1
    Manufacturer : GenuineIntel
    MaxClockSpeed : 2297
    Name : Intel(R) Xeon(R) CPU E5-2670 v3 @ 2.30GHz
    SocketDesignation : CPU socket #1
    Caption : Intel64 Family 6 Model 44 Stepping 2
    DeviceID : CPU0
    Manufacturer : GenuineIntel
    MaxClockSpeed : 2533
    Name : Intel(R) Xeon(R) CPU E5649 @ 2.53GHz
    SocketDesignation : Proc 1

    You see that it gives me wmiobject information. In addition, it was giving me the information for every server. However it did not give me the ipv4 address and operating system and other information. So for some reason the portion:

    -Properties ipv4address, OperatingSystem, OperatingSystemServicePack| Select Name, ipv4address, OperatingSystem, OperatingSystemServicePack ).name

    did not work. It does not have to be this example but I am trying to understand in the most basic form how the scripts execute. From math I know its inner first and there may be something glaring here that is obvious that I am just missing.

    I understand that the programming concept of Arrays might be a way to do this and store the information briefly then to be used with other information and I am way rusty on arrays but understand the concept but does that always have to be used here or is there more basic and caveman type methods that would have made my script work without something such as an array or variables? I will certainly study the other approach as well but that I did not catch something here may need to have it spelled out to me in basic talk lol..

  • #97226

    Don Jones
    Keymaster

    You queried the Win32_Processor class. It doesn't have, say, IP address information. That class represent a processor, not the entire computer system and operating system.

    • #97227

      brian catlin
      Participant

      Yes I am good on everything you said there and can state perhaps ignorantly that I think I am good on that concept. I thought you could use parenthesis to get around that but is the problem that I am not sticking the information into a variable or an array? and then pulling it back up when needed and adding information?

      Is this why they always use loops and if statements to get the one set of information and then add to it? Meaning its almost impossible to get information from both classes because its not saved in memory it just goes away when the next info is added into memory.

      If that's the case is there any good portions that can focus on this portion or I will buy your book or another that is not a problem

        but wanted to focus on this area specifically

      because if I can learn to manipulate this then I will be able to do a lot minus being perhaps creative with all the loops and interrelations I realize that takes time to develop a pattern of probability and vision.

      Right now I am trying to reverse engineer the scripting guy post: https://blogs.technet.microsoft.com/heyscriptingguy/2011/09/26/use-powershell-and-wmi-to-get-processor-information/

      but it has more than I need and a lot of stuff to go in there but that reminded me of the arrays and variables which got me thinking about those things at least.

  • #97230

    Don Jones
    Keymaster

    Well... I mean, there's nothing to "get around." And this has nothing to do with parenthesis or variables or loops or anything.

    If you look at https://msdn.microsoft.com/en-us/library/aa394373(v=vs.85).aspx, the class you're querying simply doesn't possess the information you want. It'd like going to Wal-Mart and asking them to sell you a Toyota – they just don't have that.

    You'll wind up having to query multiple classes. And, in order for the output to be sensible, you'll probably have to combine information from those multiple classes into one structure that PowerShell can display. This is basically the running narrative in "Learn PowerShell Scripting in a Month of Lunches." So, for a very basic example:

    $proc = Get-WmiObject -Class Win32_Processor
    $os = Get-WmiObject -Class Win32_OperatingSystem
    
    $combo = @{MaxClockSpeed = $proc.MaxClockSpeed
               ServicePackMajorVersion = $os.ServicePackMajorVersion}
    New-Object -Type PSObject -Prop $combo
    

    Rather than trying to cram this into a one-liner, I find it easier to query each class and save them each into a variable. I can then access the properties of each class by using what's stored in those variables. I use a "hash table" to construct a combination of whatever properties I want in the final output, and then create a new object to hold all of that data.

    Get-WmiObject is simply not capable of querying two classes in one go; it wasn't designed for it. It's not a matter of "not saved in memory" per se. The difficulty you get into with trying to cram everything into a one-liner is that one-liners are really only meant to perform one sequential action where one set of objects are passed from command to command.

    I mean, you -could- do this in a one-liner. It's just weird-looking, inefficient, and incredibly hard to get correct. And there's virtually no upside to doing so.

    • #97335

      brian catlin
      Participant

      Don,

      I am older and have issue with paperbacks and my eyes. I don't seem to be able to find any of your books on Kindle am I just lookin in the wrong place? I thought I had one of your older books on PDF but not at my home PC right now to check.

  • #97338

    Don Jones
    Keymaster

    Depends on the book.

    For what I've written at https://leanpub.com/u/donjones, they sell MOBI and can deliver to your Kindle directly.

    For the books published by Manning, including the Month of Lunches books, electronic editions are only sold directly by Manning at http://manning.com. They offer in PDF, MOBI, and EPUB. All of the paperbacks they sell, through any channel, come with a voucher for free electronic versions.

    • #97464

      brian catlin
      Participant

      Do you have comprehensive book that covers everything from a domain administrator type view. (I realize Powershell works all over but wanted to start with server, users type stuff that a regular domain system engineer would work with). Or maybe I need to add a couple digital books as long as they will get me to writing more intermediate code I can script as a Network/server Engineer.

      I saw you had this book that will stays updated but would good to get your input on what you think I need to add to library.

      https://leanpub.com/powershell-scripting-toolmaking

      https://www.manning.com/books/powershell-in-depth-second-edition (this one from manning looks interesting as well but still looking through them.)

      Most books all start out with Help and basic stuff and I get it starting with Help and all that is great and I am reviewing that now catching up on a ton of forgotten stuff. My main problem is connecting different lines together.

      Like this question you posted the variables and it makes sense but I am still confused on how to add different classes together I am good with parts but not putting together the whole. I need to get better with simple loops and if statements and iterations and combining these different classes to say get info on a computer and its hardware or a user and the properties. and outputting that.

      So any suggestions on must have for my library that I can buy in digital form and I will get it. once I can starting adding all the parts together to make working whole items then I think I will be in a lot better shape.

      books I already own:

      Windows Powershell 4th edition TFM by Jason Helmick and Mike F Robbins

      Microsoft PowerShell, VBScript, and JScript Bible William R Staek, James O`Neill and Jeffrey Rosen

      P.S. what do you think of the SQL in month of lunches? I have ad SQL and Access in past and programming and I am really thinking of ramping up that area as well as is very specific and business side loves BI stuff and they use SQL.

      Edited I went ahead and bought 3 of your books you have posted back to me a few times here over the year so I appreciate that. I bought the following from manning:

      https://www.manning.com/books/powershell-in-depth-second-edition

      https://www.manning.com/books/learn-windows-powershell-in-a-month-of-lunches-third-edition

      https://www.manning.com/books/learn-sql-server-administration-in-a-month-of-lunches

      I am going to wait on The tools book for now. I know some will be redundant but I wanted the in depth for the database stuff as well. I don't know where I am heading with all this but I think I am going to really push into the programming side a bit more using PowerShell and SQL and probably Phython as a launch pad and will see where I land and how far I get and how I feel about it.

      what is live book? I downloaded the kindle and pdf and saved copy of each on my google one drive. so I can access from work or home or even IPAD for trip and such.

  • #97481

    David Schmidtberger
    Participant

    From my own experience learning, and teaching co-workers PowerShell fundamentals, the learn PowerShell in a month of lunches is the best resource I've come across.

    While it can be frustrating if you've been using PowerShell for awhile to go all the way back to square one, it is very much worth it. The month of lunches books breaks PowerShell down into small easily parsec examples basically and explain at the lowest level. This allows you to actually understand the building blocks and how they all piece together.

    Sure jumping in and getting your "RIGHT NOW" tasks done gives a good feeling, but realistically you're just damaging your ability to make even more fundamental improvements in your environment by not having that foundation built.

    • #97482

      brian catlin
      Participant

      I agree and already started doing that on book to start on Chapter 3.54 reviewing how to read help syntax again and it will be helpful not only to remember it again but start picking up the right terminology to describe and ask questions. I had been through a bit of this in in the past and was at the point where I was still not very good at combining stuff like the question I had originally here but I certainly had the basics down a lot better.

      However, then I fully went into networking got my CCNA and CCNP Switch and lived and breathed networking and had not touched power shell since so very rusty. I am finding though that everyone wants my server side skills and networking skills they just don't want networking only so I am back pushing technology again on all fronts.

      I just hope this time around I can finally start getting better at the more intermediate and some advanced stuff.

The topic ‘trying to understanding how PowerShell deals with scripts inner to outer.’ is closed to new replies.

Skip to toolbar