There might be the need to get the IIS absolute path of your webapplication.
Here is the Powershell-script with which you can achieve that:
function GetIISPath ([string] $webApplicationName)
{
foreach($app in Get-SPWebApplication)
{
if($app.Name -eq $webApplicationName)
{
$IISSettings = $app.IisSettings[[Microsoft.SharePoint.Administration.SPUrlZone]::Default]
Write-Host $IISSettings.Path
}
}
}
This function writes the absolute path of the overloaded webapplication-name.
Posts mit dem Label Powershell werden angezeigt. Alle Posts anzeigen
Posts mit dem Label Powershell werden angezeigt. Alle Posts anzeigen
Montag, 13. Februar 2012
Donnerstag, 26. Januar 2012
Powershell script how to run a timer-job
If you want to run a specific timer-job, you can use this code (replace the value in the %%):
$myJob = Get-SPTimerJob "%Add the timer-job-name in here%"
$myJob.RunNow()
$myJob = Get-SPTimerJob "%Add the timer-job-name in here%"
$myJob.RunNow()
Powershell script how to add a field to a list
There might be the need for you to add a field to a list. Here is a script how to manage. Replace the values in %% with your values:
$isRequired = $%enter the boolean value in here%
$listName = "%enter your list-name%"
$siteUrl = "%enter the url of your site%"
$fieldName = "%enter the field-name%"
$fieldtype = "%enter the field-type%"
[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.rootweb
$spList = $web.Lists[$listName]
if($spList -ne $null)
{
$spList.Fields.Add($fieldName,$fieldtype,$isRequired)
}
$web.Dispose()
$site.Dispose()
$isRequired = $%enter the boolean value in here%
$listName = "%enter your list-name%"
$siteUrl = "%enter the url of your site%"
$fieldName = "%enter the field-name%"
$fieldtype = "%enter the field-type%"
[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.rootweb
$spList = $web.Lists[$listName]
if($spList -ne $null)
{
$spList.Fields.Add($fieldName,$fieldtype,$isRequired)
}
$web.Dispose()
$site.Dispose()
Powershell script how to create a list
You might want to create a list with Powershell. Here is some code you can use (Replace the values in % with your values):
[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$siteUrl = %your site-url%
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.rootweb
$templateType = $web.ListTemplates["Custom List %or add another list-template%"]
$web.Lists.Add("%enter the list-name you want to use%", "%enter the description%", $templateType)
$web.Dispose()
$site.Dispose()
[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$siteUrl = %your site-url%
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.rootweb
$templateType = $web.ListTemplates["Custom List %or add another list-template%"]
$web.Lists.Add("%enter the list-name you want to use%", "%enter the description%", $templateType)
$web.Dispose()
$site.Dispose()
Freitag, 20. Januar 2012
Powershell script how to change a property of list(s) or page(s)
Sometimes you might have to change a property of a list or page in a site-collection. Maybe the amount of items is so large, that handling this manually would take ages to do.
Here is a script, where I change all the list-titles to add the " 1" to it.
$sites = Get-SPSite
foreach($site in $sites)
{
$web = $site.RootWeb
foreach($list in $web.lists)
{
$title = $list.Title
$list.Title = "$title 1"
$list.Update()
}
}
This code changed now the title of each list in your web-pplications to it's origin name plus the string " 1". You can also check for a specific site-collection to make the changes at:
$sites = Get-SPSite
foreach($site in $sites)
{
if($site.Url -eq "%url of the site-collection you want to make the changes in%")
{
$web = $site.RootWeb
foreach($list in $web.lists)
{
$title = $list.Title
$list.Title = "$title 1"
$list.Update()
}
}
}
Here is a script, where I change all the list-titles to add the " 1" to it.
$sites = Get-SPSite
foreach($site in $sites)
{
$web = $site.RootWeb
foreach($list in $web.lists)
{
$title = $list.Title
$list.Title = "$title 1"
$list.Update()
}
}
This code changed now the title of each list in your web-pplications to it's origin name plus the string " 1". You can also check for a specific site-collection to make the changes at:
$sites = Get-SPSite
foreach($site in $sites)
{
if($site.Url -eq "%url of the site-collection you want to make the changes in%")
{
$web = $site.RootWeb
foreach($list in $web.lists)
{
$title = $list.Title
$list.Title = "$title 1"
$list.Update()
}
}
}
Powershell script how to add a webpart to a page
This script in no. 1 will add a webpart to a single page, no 2 will iterate through a site-collection and add the webpart to each sub-site/page. Please change the values in the %%- marked areas according to your settings/environment. Best practice would be to copy the code into PowerGui or any power-shell editor to see the code structured and the comments highlighted.
1. Adding a webpart to a single SPWeb
#load the assembly of your webpart
[void][reflection.assembly]::LoadWithPartialName("%your webpart assembly-name%")
#this code will do the publishing of the file, if publishing/versioning is disabled, you will not need this code
function PublbishSPFile([Microsoft.SharePoint.SPFile] $spfile)
{
if ($spfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
$spfile.CheckIn("%your comment%", [Microsoft.SharePoint.SPCheckInType]::MajorCheckin)
}
if ($page.ListItem.ParentList.EnableModeration)
{
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
{
$spfile.Approve("%your comment%")
}
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
{
$spfile.Publish("%your comment%")
}
}
}
#you have to get the web, where you want to add the webpart, you can also include everything into a function and call the function, when iterating, this is explained in no. 2
$site = Get-SPSite -Identity "%url of the site%"
$web = $site.RootWeb
#first you have to check out the file/page, if publishing/versioning is disabled, then you can skip this step
$file = $web.GetFile("/Pages/%your page%.aspx")
$file.CheckOut()
#initialize the webpart-manager and locate it to the page
$webpartmanager = $web.GetLimitedWebPartManager("%complete url%/Pages/%your page%.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webpart = new-object %add the object/class with namespace here, that corresponds to your webpart (e.g. Mynamespace.MyWebpartClass)%
$webpart.Title = "%title of the webpart%"
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
#if your webpart does need some properties to be set, do it now
$webpartmanager.AddWebPart($webpart, "Zone1",0); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place
$webpartmanager.Dispose();
#last but not least you have to check in the file/page, if publishing/versioning is disabled, then you can skip this step
PublbishSPFile($file)
2. Adding a webpart to multiple SPWebs
#load the assembly of your webpart
[void][reflection.assembly]::LoadWithPartialName("%your webpart assembly-name%")
#this code will do the publishing of the file, if publishing/versioning is disabled, you will not need this code
function PublbishSPFile([Microsoft.SharePoint.SPFile] $spfile)
{
if ($spfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
$spfile.CheckIn("%your comment%", [Microsoft.SharePoint.SPCheckInType]::MajorCheckin)
}
if ($page.ListItem.ParentList.EnableModeration)
{
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
{
$spfile.Approve("%your comment%")
}
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
{
$spfile.Publish("%your comment%")
}
}
}
#I used the code from no 1 and enclosed it into a function, that will be called in an iteration
function AddWebpart([string] $web)
{
#first you have to check out the file/page, if publishing/versioning is disabled, then you can skip this step
$file = $web.GetFile("/Pages/%your page%.aspx")
$file.CheckOut()
#initialize the webpart-manager and locate it to the page
$webpartmanager = $web.GetLimitedWebPartManager("%complete url%/Pages/%your page%.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webpart = new-object %add the object/class with namespace here, that corresponds to your webpart (e.g. Mynamespace.MyWebpartClass)%
$webpart.Title = "%title of the webpart%"
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
#if your webpart does need some properties to be set, do it now
$webpartmanager.AddWebPart($webpart, "Zone1",0); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place
$webpartmanager.Dispose();
#last but not least you have to check in the file/page, if publishing/versioning is disabled, then you can skip this step
PublbishSPFile($file)
}
$site = Get-SPSite -Identity "[url of the site]"
foreach($subsite in $site.AllWebs)
{
AddWebpart $subsite
}
1. Adding a webpart to a single SPWeb
#load the assembly of your webpart
[void][reflection.assembly]::LoadWithPartialName("%your webpart assembly-name%")
#this code will do the publishing of the file, if publishing/versioning is disabled, you will not need this code
function PublbishSPFile([Microsoft.SharePoint.SPFile] $spfile)
{
if ($spfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
$spfile.CheckIn("%your comment%", [Microsoft.SharePoint.SPCheckInType]::MajorCheckin)
}
if ($page.ListItem.ParentList.EnableModeration)
{
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
{
$spfile.Approve("%your comment%")
}
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
{
$spfile.Publish("%your comment%")
}
}
}
#you have to get the web, where you want to add the webpart, you can also include everything into a function and call the function, when iterating, this is explained in no. 2
$site = Get-SPSite -Identity "%url of the site%"
$web = $site.RootWeb
#first you have to check out the file/page, if publishing/versioning is disabled, then you can skip this step
$file = $web.GetFile("/Pages/%your page%.aspx")
$file.CheckOut()
#initialize the webpart-manager and locate it to the page
$webpartmanager = $web.GetLimitedWebPartManager("%complete url%/Pages/%your page%.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webpart = new-object %add the object/class with namespace here, that corresponds to your webpart (e.g. Mynamespace.MyWebpartClass)%
$webpart.Title = "%title of the webpart%"
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
#if your webpart does need some properties to be set, do it now
$webpartmanager.AddWebPart($webpart, "Zone1",0); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place
$webpartmanager.Dispose();
#last but not least you have to check in the file/page, if publishing/versioning is disabled, then you can skip this step
PublbishSPFile($file)
2. Adding a webpart to multiple SPWebs
#load the assembly of your webpart
[void][reflection.assembly]::LoadWithPartialName("%your webpart assembly-name%")
#this code will do the publishing of the file, if publishing/versioning is disabled, you will not need this code
function PublbishSPFile([Microsoft.SharePoint.SPFile] $spfile)
{
if ($spfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
$spfile.CheckIn("%your comment%", [Microsoft.SharePoint.SPCheckInType]::MajorCheckin)
}
if ($page.ListItem.ParentList.EnableModeration)
{
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
{
$spfile.Approve("%your comment%")
}
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
{
$spfile.Publish("%your comment%")
}
}
}
#I used the code from no 1 and enclosed it into a function, that will be called in an iteration
function AddWebpart([string] $web)
{
#first you have to check out the file/page, if publishing/versioning is disabled, then you can skip this step
$file = $web.GetFile("/Pages/%your page%.aspx")
$file.CheckOut()
#initialize the webpart-manager and locate it to the page
$webpartmanager = $web.GetLimitedWebPartManager("%complete url%/Pages/%your page%.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webpart = new-object %add the object/class with namespace here, that corresponds to your webpart (e.g. Mynamespace.MyWebpartClass)%
$webpart.Title = "%title of the webpart%"
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
#if your webpart does need some properties to be set, do it now
$webpartmanager.AddWebPart($webpart, "Zone1",0); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place
$webpartmanager.Dispose();
#last but not least you have to check in the file/page, if publishing/versioning is disabled, then you can skip this step
PublbishSPFile($file)
}
$site = Get-SPSite -Identity "[url of the site]"
foreach($subsite in $site.AllWebs)
{
AddWebpart $subsite
}
Powershell script how to change the master-page
If you want to change your master-page in your whole site-collection with all sub-sites, then you can use this powershell-script to manage this quite easily (change the %%-values according to your settings or needs):
$site = Get-SPSite -Identity "%url of the site%"
foreach($subsite in $site.AllWebs)
{
$subsite.AllowUnsafeUpdates = "True"
$subsite.CustomMasterUrl = "/_catalogs/masterpage/%my master-page%.master"
$subsite.Update()
$subsite.Dispose()
}
$site = Get-SPSite -Identity "%url of the site%"
foreach($subsite in $site.AllWebs)
{
$subsite.AllowUnsafeUpdates = "True"
$subsite.CustomMasterUrl = "/_catalogs/masterpage/%my master-page%.master"
$subsite.Update()
$subsite.Dispose()
}
Montag, 28. März 2011
How to: c# code in powershell script
I was looking for a way to add C# code to a powershell script, because sometimes you are not able to run everything with powershell.
It works like this:
first reference to the assemblies your code uses:
$referencingassemblies = (
"Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" ,
"Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c",
"System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
)
Then insert the source and store it in a variable. Be aware to have all the assemblies you are using referenced in your variable above:
$mysource = @"
using Microsoft.SharePoint.Publishing.Administration;
using Microsoft.SharePoint;
using System;
using System.Data;
using System.Xml;
using Microsoft.SharePoint.Publishing;
namespace MyNamespace
{
public static class MyClass
{
public static void Do(SPWeb rootWeb)
{
Console.WriteLine(rootWeb.Url);
Console.WriteLine(rootWeb.Title);
}
}
}
"@
Then add the code to the memory and call your code:
#Here you just add the assemblies and the code to the memory
Add-Type -ReferencedAssemblies $referencingassemblies -TypeDefinition $mysource -Language CSharp
#For this sample I will need another assembly
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")
#Load the site-object of your webapp
$site = New-Object Microsoft.SharePoint.SPSite("http://www.mydomain.com")
#Use the rootweb of your site
$root = $site.rootweb
#Overload to your C# method. To call the method use in brakets the namespace and class.
[MyNamespace.MyClass]::Do($root)
In this sample above you already see how to combine powershell code with C# code. If you change something in your C# code, then close the powershell window and re-open, so the code will be loaded/added to memory again. Otherwise you won't see the changes you made.
You can copy the code-segments above into one powershell script file and give it a try.
It works like this:
first reference to the assemblies your code uses:
$referencingassemblies = (
"Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" ,
"Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c",
"System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
)
Then insert the source and store it in a variable. Be aware to have all the assemblies you are using referenced in your variable above:
$mysource = @"
using Microsoft.SharePoint.Publishing.Administration;
using Microsoft.SharePoint;
using System;
using System.Data;
using System.Xml;
using Microsoft.SharePoint.Publishing;
namespace MyNamespace
{
public static class MyClass
{
public static void Do(SPWeb rootWeb)
{
Console.WriteLine(rootWeb.Url);
Console.WriteLine(rootWeb.Title);
}
}
}
"@
Then add the code to the memory and call your code:
#Here you just add the assemblies and the code to the memory
Add-Type -ReferencedAssemblies $referencingassemblies -TypeDefinition $mysource -Language CSharp
#For this sample I will need another assembly
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")
#Load the site-object of your webapp
$site = New-Object Microsoft.SharePoint.SPSite("http://www.mydomain.com")
#Use the rootweb of your site
$root = $site.rootweb
#Overload to your C# method. To call the method use in brakets the namespace and class.
[MyNamespace.MyClass]::Do($root)
In this sample above you already see how to combine powershell code with C# code. If you change something in your C# code, then close the powershell window and re-open, so the code will be loaded/added to memory again. Otherwise you won't see the changes you made.
You can copy the code-segments above into one powershell script file and give it a try.
Donnerstag, 3. März 2011
Powershell - site-defintion installation
I had the following problem. I created a powershell script to install a solution, that contains a site-defintion to a web-application. The issue I ran into was, that the template I am using to create the SPSite in this web-application was not recognized. That means everytime I tried to use this custom template, I received the error ""
On the web I found the cause for this error. The template is not recognized in this session. When you open parallel another powershell window and list the templates (get-spwebtemplates), then you see your template. But not in the host window.
To avoid this error, you have to start another powershell process out of the host window. There you can work with the template.
Be aware to add a sleep function, because the install of the solution with the site-definition needs to be terminated before being able to work with it.
You can copy the host-script into a file called hostscript.ps1 and the new window script into a file called window.ps1
---- your host script ------- (rename the %% variables with your parameters)
#This function installs the solution
function InstallSolution([string] $solutionname, [string] $url)
{
#Check if solution already exists
$solution = Get-SPSolution | where-object {$_.Name -eq $solutionname}
if ($solution -eq $null)
{
#If it is not added yet. Add it and then deploy it.
Add-SPSolution -LiteralPath "%your solution directory%\$solutionname"
Install-SPSolution –Identity $SolutionName –WebApplication "http://$url" –GACDeployment
}
$solution = Get-SPSolution | where-object {$_.Name -eq $solutionname}
#Run the sleeper. It will wait until the solution is finally deployed
do
{
Start-Sleep -Seconds 10
}
until ($solution.Deployed -eq $true)
}
#Enter your parameters here
$webapplicationname = %webapplication-name%
$webapplicationport = %webapplication-port%
$hostheader = %host-header%
$url = %url%
$iisdirectory = %iis-directory%
$apppool = %app-pool%
$apppoolaccount = %app-pool-account%
$databasename = %database-name%
$databaseserver = %database-server%
$sitecollectionadmin = %site-collection-admin%
#Create a new web-application with the above mentioned parameters
New-SPWebApplication -Name $webapplicationname -Port $webapplicationport -AllowAnonymousAccess:$true -HostHeader $hostheader -URL "http://$url" -Path $iisdirectory -ApplicationPool $apppool -ApplicationPoolAccount (Get-SPManagedAccount $apppoolaccount) -DatabaseName $databasename -DatabaseServer $databaseserver
#Call the function above and overload the needed parameters
InstallSolution "%my solution name%", $url
#Here is your template-name. If you don't know yours, then just install the solution and run get-spwebtemplate. It lists all templates. Yours is then supposed to be there as well with the correct # value.
$mydefinitiontemplate = "%My Template%#0"
#Here the new console powershell-window opens.
$newwindow = New-Object system.Diagnostics.ProcessStartInfo
$newwindow.FileName = "powershell.exe"
#I am overloading some variables just to show, how this works
$newwindow.Arguments = "-noexit %your install directory%\window.ps1 $url $mydefinitiontemplate $sitecollectionadmin"
$process = [system.Diagnostics.Process]::Start($newwindow)
#The process will wait, until the window will exit and close
$process.waitforexit()
#Finally just reset the iis
iisreset
---- end host script -------
---- your new window script ------- (rename the %% variables with your parameters)
#Here you catch the overloaded parameters and fill some variables with it
$url = $args[0]
$mytemplatename = $args[1]
$sitecollectionadmin = $args[2]
#Create the new site-collection
New-SPSite -URL "http://$url" -OwnerAlias $sitecollectionadmin -Name %your site name% -Template $mytemplatename
#Tell the host-window, that this window closed
$Host.SetShouldExit(0)
---- end new window script -------
Then save those files somewhere and open powershell and type:
[your path of the hostscript.ps1 file]\hostscript.ps1
This script now creates the web-application, installs the solution with the site-definition and as soon as the solution is deployed, it opens a new console window, to run the second script, that creates a new site-collection in the web-application with the site-definition template you deployed. The window closes afterwards and the host-window then does an iisreset.
It took me quite long to find out about this speciality with site-definition solution deployment and using the template afterwards when creating a site-collection.
On the web I found the cause for this error. The template is not recognized in this session. When you open parallel another powershell window and list the templates (get-spwebtemplates), then you see your template. But not in the host window.
To avoid this error, you have to start another powershell process out of the host window. There you can work with the template.
Be aware to add a sleep function, because the install of the solution with the site-definition needs to be terminated before being able to work with it.
You can copy the host-script into a file called hostscript.ps1 and the new window script into a file called window.ps1
---- your host script ------- (rename the %% variables with your parameters)
#This function installs the solution
function InstallSolution([string] $solutionname, [string] $url)
{
#Check if solution already exists
$solution = Get-SPSolution | where-object {$_.Name -eq $solutionname}
if ($solution -eq $null)
{
#If it is not added yet. Add it and then deploy it.
Add-SPSolution -LiteralPath "%your solution directory%\$solutionname"
Install-SPSolution –Identity $SolutionName –WebApplication "http://$url" –GACDeployment
}
$solution = Get-SPSolution | where-object {$_.Name -eq $solutionname}
#Run the sleeper. It will wait until the solution is finally deployed
do
{
Start-Sleep -Seconds 10
}
until ($solution.Deployed -eq $true)
}
#Enter your parameters here
$webapplicationname = %webapplication-name%
$webapplicationport = %webapplication-port%
$hostheader = %host-header%
$url = %url%
$iisdirectory = %iis-directory%
$apppool = %app-pool%
$apppoolaccount = %app-pool-account%
$databasename = %database-name%
$databaseserver = %database-server%
$sitecollectionadmin = %site-collection-admin%
#Create a new web-application with the above mentioned parameters
New-SPWebApplication -Name $webapplicationname -Port $webapplicationport -AllowAnonymousAccess:$true -HostHeader $hostheader -URL "http://$url" -Path $iisdirectory -ApplicationPool $apppool -ApplicationPoolAccount (Get-SPManagedAccount $apppoolaccount) -DatabaseName $databasename -DatabaseServer $databaseserver
#Call the function above and overload the needed parameters
InstallSolution "%my solution name%", $url
#Here is your template-name. If you don't know yours, then just install the solution and run get-spwebtemplate. It lists all templates. Yours is then supposed to be there as well with the correct # value.
$mydefinitiontemplate = "%My Template%#0"
#Here the new console powershell-window opens.
$newwindow = New-Object system.Diagnostics.ProcessStartInfo
$newwindow.FileName = "powershell.exe"
#I am overloading some variables just to show, how this works
$newwindow.Arguments = "-noexit %your install directory%\window.ps1 $url $mydefinitiontemplate $sitecollectionadmin"
$process = [system.Diagnostics.Process]::Start($newwindow)
#The process will wait, until the window will exit and close
$process.waitforexit()
#Finally just reset the iis
iisreset
---- end host script -------
---- your new window script ------- (rename the %% variables with your parameters)
#Here you catch the overloaded parameters and fill some variables with it
$url = $args[0]
$mytemplatename = $args[1]
$sitecollectionadmin = $args[2]
#Create the new site-collection
New-SPSite -URL "http://$url" -OwnerAlias $sitecollectionadmin -Name %your site name% -Template $mytemplatename
#Tell the host-window, that this window closed
$Host.SetShouldExit(0)
---- end new window script -------
Then save those files somewhere and open powershell and type:
[your path of the hostscript.ps1 file]\hostscript.ps1
This script now creates the web-application, installs the solution with the site-definition and as soon as the solution is deployed, it opens a new console window, to run the second script, that creates a new site-collection in the web-application with the site-definition template you deployed. The window closes afterwards and the host-window then does an iisreset.
It took me quite long to find out about this speciality with site-definition solution deployment and using the template afterwards when creating a site-collection.
Abonnieren
Posts (Atom)