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

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

Parents
  • 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>
    

     

Reply
  • 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>
    

     

Children
No Data