I made a small script that passes makes a site based on a template:
Function CreateSite($parentUrl, $SubSiteUrl, $Template, $Title) {
try {
write-host "init"
$site = New-Object -Type Microsoft.SharePoint.SPSite -ArgumentList $parentUrl
write-host "load web"
$web = $site.OpenWeb()
write-host "reading template"
$wtemplate = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Name -eq $Template}
$url = $parentUrl + "/" + $SubSiteUrl
write-host "creating site"
New-SPWeb -Url $url -Name $Title -UseParentTopNav
$web = GET-SPWeb $url
write-host "applying template"
$web.ApplyWebTemplate($Template)
write-host "done"
return "OK"
}
catch {
$ErrorMessage = $_.Exception.Message
write-host $ErrorMessage
return ($ErrorMessage)
}
The parameters are provided in a list, and in the calling function I write the output of the function back to the list. This works with all of my other functions, except this one. I call it like:
$message = CreateSite -parenturl $url.Url -SubSiteUrl $SubSiteUrl -Template $Template -Title $Title
During debug, I can see that the function returns this:
$message=
Url
---
https://... (here comes the full url of the site i just made)
OK
In the log list I only see System.Object[] as output. I expect only OK to be returned. This looks like a common problem, but only related to arrays, and here I only return a static value. Can someone give me a hint?
New-SPWeb
has an output -- try setting a variable ahead of it like$NewWeb = New-SPWeb
or pipe it intoOut-Null
.$NewWeb = New-SPWeb -Url $url -Name $Title -UseParentTopNav
did the job :)