String Methods do not Exist Error

This topic contains 4 replies, has 3 voices, and was last updated by  Michael Delaney 1 year, 9 months ago.

  • Author
    Posts
  • #35417

    Michael Delaney
    Participant

    I have a PowerShell script that I wrote on an Windows 7 workstation that has English as the OS language. Script works without issue. If I run the same script on a Windows 7 workstation that has French as the OS language, I receive non-terminating errors when the script runs and tries to call the Trim method of the string class. The error I receive (translated to English) is "Calling the method failed because [System.String []] does not contain a method called 'Trim'"

    The English workstation has PS 4.0 installed. The French workstation has PS 2.0 installed.

    Ideas?

  • #35422

    Bob McCoy
    Participant

    You mean beside upgrade the French workstation to 4.0? Is there some compelling reason why it remains at 2.0?

  • #35423

    Dave Wyatt
    Moderator

    The error message has your hint. 🙂 Type is [System.String []] (an array of strings). Arrays don't have a Trim() method.

    Starting with PSv3, PowerShell gave us the ability to access properties and methods on arrays as though they were a single object. $arrayOfStrings.Trim(), etc. If you need your code to be friendly to PowerShell v2, then you could do this instead: $arrayOfStrings | ForEach-Object { $_.Trim() }

  • #35424

    Dave Wyatt
    Moderator

    BTW, Bob, please email me regarding this year's PowerShell Heroes. DaveW@powershell.org . Haven't been able to find contact information for you anywhere.

  • #35434

    Michael Delaney
    Participant

    Bob, I've been wanting my organization to update to PS v4 for some time now. It was ready to go for deployment but they didn't like the fact that it wasn't as easily removed since v4 requires registry backups prior to uninstall.

    Much appreciated Dave. I hadn't realized that change since I've only every really worked on version 4 when I started to learn PowerShell.

You must be logged in to reply to this topic.