Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
ProjectWise
  • Product Communities
ProjectWise
ProjectWise PowerShell Extensions Forum Validate Get-PWFolders Attempt
    • Sign In

    • State Verified Answer
    • +1 person also asked this people also asked this
    • Replies 2 replies
    • Subscribers 66 subscribers
    • Views 1250 views
    • Users 0 members are here

    Validate Get-PWFolders Attempt

    Matt Kovach
    Offline Matt Kovach over 5 years ago

    Hello again PowerShell gurus,

    Is it possible to check if a Folder exists before performing subsequent commands? If the Folder exists, then do "stuff". Otherwise, just move along to the next Folder (if there is one).

    Example

    $Folder = "FDOT\Projects\5167056\Working\Fred.Smith"

    Get-PWFolders -FolderPath $Folder -JustOne -Verbose

    But, FDOT\Projects\5167056\Working\Fred.Smith actually doesn't exist in the Datasource (maybe it was renamed or deleted just prior to running the PowerShell Script). I immediately get the following message:

    Thanks for your wisdom in advance!

    • Sign in to reply
    • Cancel

    Top Replies

    • Kevin van Haaren
      Offline Kevin van Haaren Fri, Sep 28 2018 3:51 PM +1 verified
      There is no check you could run that wouldn't run the risk of being invalid when you finally execute the Get-PWFolders command. Even if it was the command immediately before the Get-PWFolders there is…
    Parents
    • Kevin van Haaren
      +1 Offline Kevin van Haaren Fri, Sep 28 2018 3:51 PM

      There is no check you could run that wouldn't run the risk of being invalid when you finally execute the Get-PWFolders command. Even if it was the command immediately before the Get-PWFolders there is a chance the user renamed or deleted at exactly that time.

      Instead what you want to do is catch the error and do something when the error occurs.  In PowerShell you use a Try Catch block to do this.

      Try {
          Get-PWFolders -ErrorAction Stop
      } Catch {
          # handle the error here
          write-error "An error occurred!"
      }
      
      

      You want as few statements in the Try block as possible so you can be sure where the error occured. there a bunch of options you can do with the Try Catch block to handle different errors in different ways.

      If you just want your script to not stop and don't care about handling errors you can use -ErrorAction Continue or -ErrorAction SilentlyContinue options. The first will still print the error message but won't halt the script. The 2nd won't even print the error. I'm not a fan of these but for quick and dirty they're available.

       

      Answer Verified By: Brian Flaherty 

      • Cancel
      • Vote Up +1 Vote Down
      • Sign in to reply
      • Reject Answer
      • Cancel
    Reply
    • Kevin van Haaren
      +1 Offline Kevin van Haaren Fri, Sep 28 2018 3:51 PM

      There is no check you could run that wouldn't run the risk of being invalid when you finally execute the Get-PWFolders command. Even if it was the command immediately before the Get-PWFolders there is a chance the user renamed or deleted at exactly that time.

      Instead what you want to do is catch the error and do something when the error occurs.  In PowerShell you use a Try Catch block to do this.

      Try {
          Get-PWFolders -ErrorAction Stop
      } Catch {
          # handle the error here
          write-error "An error occurred!"
      }
      
      

      You want as few statements in the Try block as possible so you can be sure where the error occured. there a bunch of options you can do with the Try Catch block to handle different errors in different ways.

      If you just want your script to not stop and don't care about handling errors you can use -ErrorAction Continue or -ErrorAction SilentlyContinue options. The first will still print the error message but won't halt the script. The 2nd won't even print the error. I'm not a fan of these but for quick and dirty they're available.

       

      Answer Verified By: Brian Flaherty 

      • Cancel
      • Vote Up +1 Vote Down
      • Sign in to reply
      • Reject Answer
      • Cancel
    Children
    • Matt Kovach
      0 Offline Matt Kovach Sat, Oct 13 2018 12:02 PM in reply to Kevin van Haaren

      Thank you, Kevin! Apologies for the extremely late response (side-tracked a bit longer than expected).

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

    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