Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, March 8, 2011 3:18 AM
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
All replies (6)
Tuesday, March 8, 2011 4:13 AM âś…Answered | 1 vote
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%5D')"
)
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...
Tuesday, March 8, 2011 3:22 AM
You can read this. You may have to wrap the powershell script in a bat file.
Blog | SharePoint Field Notes Dev Tool | ClassMaster
Tuesday, March 8, 2011 4:15 AM | 1 vote
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 5:13 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:29 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:36 AM
Thanks Nik.