0

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?

  • 1
    Most likely New-SPWeb has an output -- try setting a variable ahead of it like $NewWeb = New-SPWeb or pipe it into Out-Null. – Taylor May 22 '17 at 12:00
  • Yes, that was it $NewWeb = New-SPWeb -Url $url -Name $Title -UseParentTopNav did the job :) – vilmarci May 22 '17 at 12:24
0

As discussed in the comments, New-SPWeb has a return value. Set it in a variable or pipe it into Out-Null.

$NewWeb = New-SPWeb -Url $url -Name $Title -UseParentTopNav

Or

New-SPWeb -Url $url -Name $Title -UseParentTopNav | Out-Null

https://technet.microsoft.com/en-us/library/ff607579.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.