Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
ProjectWise
  • Product Communities
ProjectWise
ProjectWise PowerShell Extensions Forum Automation and Productivity Gains: A PowerShell Use Case - Accel ProjectWise virtual conference
    • Sign In

    • Replies 7 replies
    • Subscribers 68 subscribers
    • Views 1120 views
    • Users 0 members are here
    • powershell
    • ProjectWise
    • ProjectWise PowerShell Modules

    Automation and Productivity Gains: A PowerShell Use Case - Accel ProjectWise virtual conference

    Marty.Nickel
    Offline Marty.Nickel over 3 years ago

    Please use this thread for all questions relating to the Accel ProjectWise virtual conference's session on Automation and Productivity Gains: A PowerShell Use Case

    • Sign in to reply
    • Cancel
    • Kevin van Haaren
      Offline Kevin van Haaren Sat, Apr 25 2020 9:42 PM

      $logSplat = @{
      	Message = ""
      	Path = "$($ENV:Temp)"
      	Level = ""
      	Cmdlet = ""
      	NoClobber = $false
      }
      
      $loginSplat = @{
      	DatasourceName = ''
      	Username = ''
      	password = ''
      	UseGUI = $false
      	BentleyIMS = $false
      	NonAdminLogin = $false
      	DoNotCreateWorkingDirectory = $true
      	LoadWRE = $false
      }
      
      $logSplat.Cmdlet = "Login"
      try {
      	$result = New-PWLogin @loginSplat -ErrorAction Stop
      } catch {
      	$logSplat.Message = "ProjectWise login to datasource $($loginSplat.DatasourceName) failed"
      	$logSplat.Level = "Fatal"
      	Write-PWPSLog @logSplat
      	throw $logSplat.Message
      }
      
      if ($result -ne $true) {
      	$logSplat.Message = "ProjectWise login to datasource $($loginSplat.DatasourceName) failed"
      	$logSplat.Level = "Fatal"
      	Write-PWPSLog @logSplat
      	throw $logSplat.Message
      }
      
      $pwConnection = @{
      	'Connected'  = $true
      	'Datasource' = Get-PWCurrentDatasource
      	'Session'    = Get-PWCurrentDSSession
      }
      

      This is an example of logging into a datasource with error detection and logging. It uses PWPS_DAB's logging command. I setup a splat for logging and the login command.

      The New-PWLogin command sometimes fails with a catchable error, and sometimes just returning a value that isn't true (i can't remember if it's $null or $false, i just test for not true). This example catches both errors and allows you to log them.

      more info on using Try-Catch-Finally code:

      Microsoft docs: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-7

      One from another site that I like:

      https://ss64.com/ps/try.html

       

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Cancel
    • Kevin van Haaren
      Offline Kevin van Haaren Sat, Apr 25 2020 10:17 PM

      Here's some code that didn't make it into my presentation (darn time limits).

      I talked a lot about using configuration files when making scripts where you want to run the same script with different tasks in a scheduled task. but i didn't show i import a config file into a script. Here's some code for that, also shows how to process a command line option passed into your script.

      My config files are YAML formatted (https://en.wikipedia.org/wiki/YAML)

      Example config file:

      # General Configuration
      General:
          # can be in form of server:datasource or the datasource display name
          Datasource: 'server:datasource'
          # projectwise username use empty string for single sign-on.
          PWUsername: 'username'
          # password must be an encrypted secure string, empty string for single sign-on
          PWPassword: ''
      MoreOptions:
          LogFolder: 'c:\logs\'
          PWPath: 'Path\to\folder\in\PW'
      

      Powershell Code:

      #		 Script: Example_Config.ps1
      #		Version: 1.0
      #	Description: Read in a YAML configuration file
      #		   Date: 2020-04-25
      #
      
      [CmdletBinding()]
      Param (
      	[Parameter(
      		Mandatory = $True,
      		ValueFromPipeline = $False,
      		ValueFromPipelineByPropertyName = $True,
      		HelpMessage = 'Config file to load. In YAML format.'
      	)]
      	[ValidateScript({Test-Path $_ })]
      	[String]$ConfigFile
      )
      
      Import-Module PowerShell-Yaml -Verbose:$false -ErrorAction Stop
      
      # import yaml configuration
      # Use -raw option on Get-Content so content is returned as single string, not an array or strings.
      $config = ConvertFrom-Yaml -Yaml (Get-Content -Path $configFile -raw)
      
      write-host "Datasource: $($config.General.Datasource)"
      write-host "    PWPath: $($config.MoreOptions.PWPath)"

      This what you see when you run it:

      PS>.\read_config_file.ps1 -ConfigFile .\Example_Config.yaml
      Datasource: server:datasource
          PWPath: Path\to\folder\in\PW
      PS>
      

       

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • 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