locked
Variables file for Powershell variables RRS feed

  • Question

  • Hi All,

    I have a powershell script and I have hard-coded various values in it and now I want to create a variables file in which I would be adding all variables

    But I am not sure which type of file I should be creating for variables ? Should be an excel, XML, txt or what ?



    Thanks in Advance, Jiniv Thakkar

    Sunday, November 20, 2016 5:05 PM

Answers

  • Hi,

    this is a sample of parameters:

    [CmdletBinding()]
    param
    (
    <#
        [Parameter(Mandatory = $true, HelpMessage="Enter the URL of the office 365 admin site collection, e.g. 'https://intranet-admin.mydomain.com'")]
        [String]
        $AdminSiteUrl,
    
        [Parameter(Mandatory = $true, HelpMessage="Enter the URL of the target site collection, e.g. 'https://intranet.mydomain.com/sites/targetSite'")]
        [String]
        $TargetSiteUrl,
    
        [Parameter(Mandatory = $false, HelpMessage="Enter the site collection title'")]
        [String]
        $SiteTitle,
    
    )

    you can put this syntax in top of your powershell script, as you can see the mandatory parameter are required when you run the script and then you have to fill in these parameters.

    If you want you can create another powershell file for example "config.ps1" and reference it in your main powershell script.

    Config.ps1 sample:

    # ******************************************* #
    # - Parameters
    $solutionFolder = "C:\Temp\Pkg\";

    Main powershell script sample:

    # Load up our common parameters and functions 
    $commons = Join-Path (Get-ScriptDirectory) "Config.ps1"
    if (!(Test-Path $commons))
    {
        $errorMessage = "ERROR: The input parameters file '$commons' not found";
    	$exception = New-Object System.Exception ($errorMessage)
    	throw $exception
    }
    # Here you can call directly the variable present in the config.ps1 file
    $solutionFolder
    



    Giuliano De Luca - MCSD SharePoint, MCSD Web Applications, MCPS, MS. www.delucagiuliano.com Twitter: @giuleon




    Sunday, November 20, 2016 7:01 PM

All replies

  • Hi Jiniv

    You can use any file type you are comfortable with. There is no recommended type as such. 


    Thanks
    Mohit

    • Proposed as answer by romeo donca Sunday, November 20, 2016 8:43 PM
    Sunday, November 20, 2016 6:21 PM
  • Hi,

    this is a sample of parameters:

    [CmdletBinding()]
    param
    (
    <#
        [Parameter(Mandatory = $true, HelpMessage="Enter the URL of the office 365 admin site collection, e.g. 'https://intranet-admin.mydomain.com'")]
        [String]
        $AdminSiteUrl,
    
        [Parameter(Mandatory = $true, HelpMessage="Enter the URL of the target site collection, e.g. 'https://intranet.mydomain.com/sites/targetSite'")]
        [String]
        $TargetSiteUrl,
    
        [Parameter(Mandatory = $false, HelpMessage="Enter the site collection title'")]
        [String]
        $SiteTitle,
    
    )

    you can put this syntax in top of your powershell script, as you can see the mandatory parameter are required when you run the script and then you have to fill in these parameters.

    If you want you can create another powershell file for example "config.ps1" and reference it in your main powershell script.

    Config.ps1 sample:

    # ******************************************* #
    # - Parameters
    $solutionFolder = "C:\Temp\Pkg\";

    Main powershell script sample:

    # Load up our common parameters and functions 
    $commons = Join-Path (Get-ScriptDirectory) "Config.ps1"
    if (!(Test-Path $commons))
    {
        $errorMessage = "ERROR: The input parameters file '$commons' not found";
    	$exception = New-Object System.Exception ($errorMessage)
    	throw $exception
    }
    # Here you can call directly the variable present in the config.ps1 file
    $solutionFolder
    



    Giuliano De Luca - MCSD SharePoint, MCSD Web Applications, MCPS, MS. www.delucagiuliano.com Twitter: @giuleon




    Sunday, November 20, 2016 7:01 PM