Results 1 to 12 of 12
  1. #1
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts

    PowerShell and White Space

    Hey Y'all,

    I came across an interesting thing today with PowerShell I thought would be useful to pass along.

    I have a program (I've posted it elsewhere in this forum) that reports a PCs Inventory. It runs just fine on all my machines and others have tested it also.

    Today I tried, via TeamViewer, to load it on my cousin's Win 7 HP machine and it threw syntax errors. I checked and this machine is running PS 2.0 and it does not handle white space the same as PS 4+ (don't have PS 3 to test).

    I've always been of the neatness in programming camp and try to make the structure of a program visible via indentation and line length.

    Generally, in later versions of PS where you can use one space you can have many and even new lines. However, it seems that PS 2 doesn't like this.

    For Example:
    Code:
      $VideoHW    = 
         $citable.rows[$VideoTabStartCnt..$(
                               $CITable.Rows.Count - 1)] |
         Format-Table -Property $fmtVid -Wrap | Out-String
    Works just fine on PS 4+ but throws a syntax error on PS 2. Close it up like this:
    Code:
      $VideoHW    = 
         $citable.rows[$VideoTabStartCnt..$($CITable.Rows.Count - 1)] |
         Format-Table -Property $fmtVid -Wrap | Out-String
    And no problemo! Of course it just moves to the next occurrence in the program...Grrrrrrrrrrr!

    The nasty part of this is that the program checks for OS & PS versions to make sure it will work, and issues appropriate messages before exiting, but it can't get that far because of the syntax problems and just blows up on the user.

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  2. #2
    2 Star Lounger
    Join Date
    Dec 2009
    Location
    Tennessee,USA
    Posts
    161
    Thanks
    24
    Thanked 17 Times in 15 Posts
    Generally, in later versions of PS where you can use one space you can have many and even new lines. However, it seems that PS 2 doesn't like this.
    I also noticed that, but, I wasn't sure what to do about it. The only solution I know of is to make sure you are using the same version of PS that the script author used.
    michael

  3. #3
    4 Star Lounger
    Join Date
    Jan 2010
    Location
    Fort McMurray, Alberta, Canada
    Posts
    561
    Thanks
    51
    Thanked 68 Times in 66 Posts
    I've always been of the neatness in programming camp and try to make the structure of a program visible via indentation and line length.
    Right there with you, RG! I'm an old school programmer and code readability is paramount for me. I can use many different code formatting styles but the style must achieve a high level of transparency.

    Make the programmer's job easy, or at least as easy as still gets the job done. I find that when I do that:

    1). Errors go down. That's always a win;
    2). Factoring and refactoring gets better and easier;
    3). Even performance problems and their origins become easier to spot;
    4). Most of the time new functionality is much easier to add. Often you can see exactly where it needs to be plugged in.

    So, what to do? That's a pickle, isn't it? Do you support PS v2, which is still in wide circulation with Windows 7? After all compatibility isn't something you should walk away from easily.

    Or do you say no, this is a deal-breaker, the PS must be v3-4? And even if you do, having the script break when run with PS v2 is frankly not cool. At minimum the script should throw an error that makes it plain what is wrong and how to fix it. And syntax errors don't get you there.

    This isn't enterprise software where you can simply set a rule in a Getting Started document and expect a computer analyst to read the rules and follow them. That's before even installing the software. No, this is PC software, the wild west of software. Users will run the script without reading anything, without even being sure the script is safe and reputable.

    So, can you try this? Have an initialization section that does version checks, and use the PS v2 rules there. That way the script can kick out an internal error and exit normally. The rest of the script can use the more advanced formatting capabilities. Just my 2 cents.

  4. #4
    2 Star Lounger
    Join Date
    Dec 2009
    Location
    Tennessee,USA
    Posts
    161
    Thanks
    24
    Thanked 17 Times in 15 Posts
    Have an initialization section that does version checks, and use the PS v2 rules there. That way the script can kick out an internal error and exit normally. The rest of the script can use the more advanced formatting capabilities.
    +1 for above.

    There is an outside chance that you could try an automated replacement routine. However, it's not guaranteed. Something along the lines of; replace "( " with "(".
    Only a try would show how complete it works.
    Cheers,
    Michael

  5. #5
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Quote Originally Posted by BHarder View Post
    So, can you try this? Have an initialization section that does version checks, and use the PS v2 rules there. That way the script can kick out an internal error and exit normally. The rest of the script can use the more advanced formatting capabilities. Just my 2 cents.
    Unfortunately, the program will not start while it has a syntax error, if memory serves since I don't have a version 2 machine to test it on. The current errors are showing up in Functions not the Main routine when I attempted to run it on my cousin's machine.

    Since this is definitely NOT a commercial program, just a learning exercise for me and anyone else it may benefit, I think I'll just place a #Requires -version 4 statement at the front of the program.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  6. #6
    WS Lounge VIP Browni's Avatar
    Join Date
    Dec 2009
    Location
    Rochdale, UK
    Posts
    1,653
    Thanks
    38
    Thanked 161 Times in 139 Posts
    I've got a v2 installation if you need any testing.

    psver.PNG

    I'm not very familiar with PS so may need a little handholding

  7. #7
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Browni,

    Thanks for the offer but I don't think I want to worry about making this run on PS 2. Unfortunately, I've got other anomalies to solve on much newer system.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  8. #8
    2 Star Lounger
    Join Date
    Dec 2009
    Location
    Tennessee,USA
    Posts
    161
    Thanks
    24
    Thanked 17 Times in 15 Posts
    just a learning exercise for me and anyone else it may benefit,
    I learned a lot from it. Understanding that the version of PS is important when using scripts is a very valuable lesson. That plus your detailed explanation of why is very useful.
    thanks RG,
    michael

  9. #9
    4 Star Lounger
    Join Date
    Jan 2010
    Location
    Fort McMurray, Alberta, Canada
    Posts
    561
    Thanks
    51
    Thanked 68 Times in 66 Posts
    OK, here's another thought. You've already got one external program for setting Remote WMI capability, right? How about another external script to check the PS versions?

    As I see it this has the following design attributes. On the plus side of the ledger:

    1). It will work, for sure;
    2). The version checking script can stick entirely to older formatting rules, thus you aren't mixing styles in the same script. I happen to like that a lot.

    On the negative side:

    1). It does get a bit scattered, all these auxiliary pieces of code. Ideally it would be better if it were all integrated;
    2). This doesn't actually stop anyone from running the main script and getting the syntax error on an old setup.

    I know you want to keep this low key, RG, and you have said this isn't non-commercial. In reply I would say only, non-commercial doesn't mean low quality. And I would say you have created a quality piece of code here. Thus I would encourage you to follow your instincts for good quality; it's hard to go wrong when you do that.

  10. #10
    WS Lounge VIP Browni's Avatar
    Join Date
    Dec 2009
    Location
    Rochdale, UK
    Posts
    1,653
    Thanks
    38
    Thanked 161 Times in 139 Posts
    I was looking for something else and came across this in W10 Pro...

    ps2.PNG

    I know you said you're not going to bother with PS2 compatibility but this may be of future use

  11. #11
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Browni,

    Yes, I have the same thing. I guess if I would place a Set-StrictMode -Version 2 statement in the code to check for compatibility but I know there are cmdlets and functions I'm using that require at least ver. 3 so that wouldn't be of much help. But it is definitely something to keep in mind.

    To paraphrase Marie Antoinette, "Let them Upgrade!"

    Thanks,
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  12. #12
    4 Star Lounger
    Join Date
    Jan 2010
    Location
    Fort McMurray, Alberta, Canada
    Posts
    561
    Thanks
    51
    Thanked 68 Times in 66 Posts
    I believe Marie Antoinette was using CakeShell version 1.5?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •