MOL: Powershell Scripting (2017) script error Ch 17

This topic contains 7 replies, has 2 voices, and was last updated by  Jeff Taylor 2 weeks ago.

  • Author
    Posts
  • #81200

    Jeff Taylor
    Participant

    I wanted to try this sample code from "Learn Powershell Scripting in a Month of Lunches" but get an error re: the variable $cs

    function Get-DiskInfo {
     foreach ($domain in (Get-ADForest).domains) {
       $hosts = Get-ADDomainController -filter * -server $domain |
       Sort-Object -Prop hostname
       ForEach ($host in $hosts) {
        $cs = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName
    [CA]$host
        $props = @{'ComputerName' = $host
                   'DomainController' = $host
                   'Manufacturer' = $cs.manufacturer
                   'Model' = $cs.model
                   'TotalPhysicalMemory(GB)'=$cs.totalphysicalmemory / 1GB}
         New-Object -Type PSObject -Prop $props
        } #foreach $host
      } #foreach $domain
    } #function

    Error: "Missing an argument for parameter 'ComputerName'" Shouldn't it be -ComputerName $host ?

    yet, If I do that (remove [CA]) I get error: "Cannot overwrite variable host becuase it is read-only or constant"

    I am considering purchasing this book but just wanted to test some of its scripts in my lab environment

  • #81203

    Don Jones
    Keymaster

    Ugh. The CA thing is a typesetting mark; I don't know why that's in the sample code. If you can tell me the listing number I'll go grab the original for you. I think you've just got typesetting crap messing you up. $host itself MIGHT be a problem on certain systems, which is a brand new thing, but you could just replace that variable globally with something else. The original code runs fine on my computer.

  • #81208

    Jeff Taylor
    Participant

    I found it here: https://livebook.manning.com/#!/book/learn-powershell-scripting-in-a-month-of-lunches/chapter-17/v-3/65
    17.3.3 Our Take

    ...but it seems to be all over, starting here as well: 17.2.1 The walkthrough (various tables/examples)

    I'm hoping not to have to troubleshoot the code though too much which sort of breaks my focus from the lesson at hand. Can these be fixed at least in the online version?

    note: I see I can remove the [CA] then move that line back up to the end of the previous line in order to make it work (no pipe or escape character of course there) but if I have to do that for numerous examples I think I'll pass on this particular purchase at this time. I had a 50% coupon but it expires tonight at midnight.

  • #81229

    Don Jones
    Keymaster

    Yeah, we have listings without all that mess. I'll have Jeff resend it to them.

  • #81232

    Don Jones
    Keymaster

    OK, Jeff's going to get them an updated downloadable. Might take a day – he wants to run through and make sure there aren't more of these lingering in them. Sorry about the confusion.

    I will note, though, that the "Scripting" MoL isn't really the "starting point" book. You'll want to get the other one as a starting point, if you've not already done so, unless you basically already know everything in it.

    The [CA], incidentally, is a typesetting mark that tells them to put in a line-wrap character in the book. It'll always appear in all of the manuscript files – e.g., PDF, etc. The actual sample code ZIP shouldn't have those, though, and that's what Jeff's gonna fix and get to them.

    • #81259

      Jeff Taylor
      Participant

      the "Scripting" MoL isn't really the "starting point" book. You'll want to get the other one as a starting point

    • What do you mean, "the other one"? I have own a few MOL books.

  • #81233

    Don Jones
    Keymaster

    You know, I want to clarify something – are you just copying and pasting code from the live book manuscript, or did you actually download the sample code?

    If you're copying and pasting, you're always going to run into this. We have to go through contortions to make code fit into a printed page, and the manuscript reflects that. That's why the download is provided – it's the un-contorted code. I know that's provided to everyone who purchases the book, and Jeff's gone through it and isn't seeing any of the typesetting stuff.

    • #81257

      Jeff Taylor
      Participant

      I've downloaded pdf and eBook for my purchased stuff but when I saw "LiveBook" under books I was considering purchasing, I thought that best for testing the scripts that had "COPY" under them. That's where the trouble started 🙂

You must be logged in to reply to this topic.