locked
Passing parameters to ps1 file RRS feed

  • Question

  • Hi,

    I have just started to learn windows powershell scripting for sharepoint. I need to execute the cmdlet Get-SPWebApplication <<sitename>>. I have stored this cmdlet in a ps1 file and need to pass the <<sitename>> as an argument to the file. 

    How do I pass this argument in the poweshell console.

    If I say

    .\filename.ps1 http://<<sitename>>

    Its throwing an error and the cmdlet in the ps1 file executes and gives result ignoring the argument I passed in.

    Thanks,

    Cutloo

     

    Tuesday, March 8, 2011 3:18 AM

Answers

  • Here are the two steps..

    1. create the new powershell file "test.ps1" and pass the parameter as follow. Please notice that parameter is used in the main function. Main function ensures that sharepoint powershell snapin loaded and main function is called within the ps1 file.

    param (
        [string]$siteUrl = "$(Read-Host 'Enter the Site Collection URL. [e.g. http://sp2010vm]')"

    )

    function main() {

     # check to ensure Microsoft.SharePoint.PowerShell is loaded
     $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
     if ($snapin -eq $null)
     {
      Write-Host "Loading SharePoint Powershell Snapin"
      Add-PSSnapin "Microsoft.SharePoint.Powershell"
     }

      DoSomething $siteUrl

    }

    main

    2. Create the .bat file and run the powershell script and pass the parameter as following. Run the batch file as an administrator.

    powershell -Command "& {Set-ExecutionPolicy bypass}" -NoExit
    powershell -Command "& {C:\test.ps1  -siteUrl http://sp2010vm}" -NoExit

    pause

    Hope this helps...

    • Marked as answer by Cutloo Tuesday, March 8, 2011 5:37 AM
    Tuesday, March 8, 2011 4:13 AM

All replies

  • You can read this. You may have to wrap the powershell script in a bat file.

    http://stackoverflow.com/questions/1293907/how-to-pass-command-line-arguments-to-a-powershell-ps1-file


    Blog | SharePoint Field Notes Dev Tool | ClassMaster
    Tuesday, March 8, 2011 3:22 AM
  • Here are the two steps..

    1. create the new powershell file "test.ps1" and pass the parameter as follow. Please notice that parameter is used in the main function. Main function ensures that sharepoint powershell snapin loaded and main function is called within the ps1 file.

    param (
        [string]$siteUrl = "$(Read-Host 'Enter the Site Collection URL. [e.g. http://sp2010vm]')"

    )

    function main() {

     # check to ensure Microsoft.SharePoint.PowerShell is loaded
     $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
     if ($snapin -eq $null)
     {
      Write-Host "Loading SharePoint Powershell Snapin"
      Add-PSSnapin "Microsoft.SharePoint.Powershell"
     }

      DoSomething $siteUrl

    }

    main

    2. Create the .bat file and run the powershell script and pass the parameter as following. Run the batch file as an administrator.

    powershell -Command "& {Set-ExecutionPolicy bypass}" -NoExit
    powershell -Command "& {C:\test.ps1  -siteUrl http://sp2010vm}" -NoExit

    pause

    Hope this helps...

    • Marked as answer by Cutloo Tuesday, March 8, 2011 5:37 AM
    Tuesday, March 8, 2011 4:13 AM
  • You don't need to create the batch file. You can run the ps1 file as an administrator and hard code the parameter values. If you want to automate and schedule the powershell script, I would use the second step...
    Tuesday, March 8, 2011 4:15 AM
  • Hi Nik,

    Thanks a ton for your inputs, before I try this out I want to understand few points in step#2 as I would be required to create a batch file.

    I need to open the command prompt as administrator and run powershell -Command ,correct?

    What does "& {Set-ExecutionPolicy bypass}" -NoExit mean?

    In the second line you are calling the ps1 file and passing the argument with "siteUrl", the name "siteUrl" should match with what we have used in the param line? [param (
        [string]$siteUrl = "$(Read-Host 'Enter the Site Collection URL. [e.g. http://sp2010vm]')")]

     

     

     

    Tuesday, March 8, 2011 5:13 AM
  • No, You don't need to run from the command line. Just create a text file from notepad and save as batch file extension (.bat file). Once you have batch file, run the batch file by right clicking it and run as administrator.

    To run the powershell command on the Sharepoint servers, you need administrative capability. "Set-ExecutionPolicy bypass" will by pass the powershell execution policies to allow you to run the powershell command as administrator.

    For the second question, yes, parameter name should match in the batch file and ps1 file..

    Hope this helps.

    Tuesday, March 8, 2011 5:29 AM
  • Thanks Nik.
    Tuesday, March 8, 2011 5:36 AM