Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
ProjectWise
  • Product Communities
ProjectWise
ProjectWise PowerShell Extensions Forum PWPS_DAB Version Check
    • Sign In

    • State Verified Answer
    • Replies 6 replies
    • Subscribers 66 subscribers
    • Views 1632 views
    • Users 0 members are here

    PWPS_DAB Version Check

    Dave Cole
    Offline Dave Cole over 3 years ago

    I am using this to check the version of PWPS_DAB before running a script.  Is there a better way to do this?

    $LocalDabModuleVersion = Get-Module -Name PWPS_DAB -ListAvailable
    $LocalVersionString = $LocalDabModuleVersion.Version.ToString().Replace(".","")
    
    If($LocalVersionString -lt "12110")
    {
        Write-Host "PWPS_DAB 1.21.1.0 or newer required to run this script." -ForegroundColor Cyan
        Write-Host
    }else{
        
        Write-Host "PWPS_DAB" $LocalDabModuleVersion.Version "found. Script will run." -ForegroundColor Cyan
        Write-Host
    }

    I can see potential issues with this method.  For example, when PWPS_DAB 2.0.0.0 is released.

    Thanks.

    • Sign in to reply
    • Cancel

    Top Replies

    • Dan Williams
      Offline Dan Williams Wed, Apr 22 2020 3:08 PM +1 verified
      You might want to check out the #Requires statement documented here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires?view=powershell-5.1 I tried it with…
    • Dave Cole
      Offline Dave Cole Wed, Apr 22 2020 3:47 PM in reply to Dan Williams +1
      Thanks! Tweaking it a bit, this will work in ISE #Requires -Modules PWPS_DAB, @{ModuleName="PWPS_DAB";ModuleVersion="1.21.1.0"} Adding ModuleVersion specifies the minimum version needed to run.
    Parents
    • Dan Williams
      +1 Offline Dan Williams Wed, Apr 22 2020 3:08 PM

      You might want to check out the #Requires statement documented here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires?view=powershell-5.1

      I tried it with this script:

      #https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires?view=powershell-5.1
      #Requires -Version 5.1
      #Requires -Modules @{ ModuleName="PWPS_DAB"; RequiredVersion="1.21.1.0" }
      write-host 'hello world'
      #Get-Module -Name PWPS_DAB -ListAvailable

      Be aware that if you try this with the run command in a PowerShell ISE session, it is ignored.  Just save your file and run that from the command prompt.

      Here's where it works OK:

      When versions are OK.

      And where it doesn't:

      Missing required version.

      Thanks for asking, I learned something new (to me) today!

      Answer Verified By: Dave Cole 

      • Cancel
      • Vote Up +1 Vote Down
      • Sign in to reply
      • Verify Answer
      • Reject Answer
      • Cancel
    • Robert McMillan
      0 Offline Robert McMillan Wed, Apr 22 2020 6:56 PM in reply to Dan Williams

      This can also be achieved with a -MinimumVersion or -RequiredVersion with Import-Module. Works in PowerShell ISE as well.

      Import-Module -Name 'PWPS_DAB' -MinimumVersion '1.20.2.0' -ErrorAction Stop

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    • Dan Williams
      0 Offline Dan Williams Wed, Apr 22 2020 7:12 PM in reply to Robert McMillan

      Apparently, I was wrong about the #Requires statement not working in an ISE session - I thought that I had read that somewhere.  From what I can tell, and tested so far, it does appear to work in an ISE session, at the command prompt or in the text window run with F5 or F8.

      I don't run PS scripts much from schedule tasks (other than occasionally testing something), so I pretty much run from ISE and I don't bother to import PWPS_DAB as it gets imported automatically, at least on my box, but Robert's approach may be more desirable, or not.  Test the two approaches out for yourself and see if one or the other "works" best for you.

      Thanks again for the question and the feedback, I keep learning stuff thanks to others!

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    Reply
    • Dan Williams
      0 Offline Dan Williams Wed, Apr 22 2020 7:12 PM in reply to Robert McMillan

      Apparently, I was wrong about the #Requires statement not working in an ISE session - I thought that I had read that somewhere.  From what I can tell, and tested so far, it does appear to work in an ISE session, at the command prompt or in the text window run with F5 or F8.

      I don't run PS scripts much from schedule tasks (other than occasionally testing something), so I pretty much run from ISE and I don't bother to import PWPS_DAB as it gets imported automatically, at least on my box, but Robert's approach may be more desirable, or not.  Test the two approaches out for yourself and see if one or the other "works" best for you.

      Thanks again for the question and the feedback, I keep learning stuff thanks to others!

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    Children
    No Data

    Communities
    • Home
    • Getting Started
    • Community Central
    • Products
    • Support
    • Secure File Upload
    • Feedback
    Support and Services
    • Home
    • Product Support
    • Downloads
    • Subscription Services Portal
    Training and Learning
    • Home
    • About Bentley Institute
    • My Learning History
    • Reference Books
    Social Media
    •    LinkedIn
    •    Facebook
    •    Twitter
    •    YouTube
    •    RSS Feed
    •    Email

    © 2023 Bentley Systems, Incorporated  |  Contact Us  |  Privacy |  Terms of Use  |  Cookies