使用powershell在sharepoint中创建站点

时间:2013-02-08 06:36:02

标签: sharepoint powershell

我对PowerShell很新,因为我知道sharepoint。我想使用Powershell在sharepoint中创建一个站点。找到一些链接,但“Get”命令会抛出一条错误消息。我是否需要进行任何初始配置/设置才能开始使用powershell w.r.t sharepoint? 请指导...... 如何使用powershell在Sharepoint中创建网站...

谢谢

2 个答案:

答案 0 :(得分:0)

试试这个,如果你想使用powershell远程处理,命令看起来像这样:

Invoke-Command -ComputerName MyComputer `
               -FilePath .\Create-WebSite.ps1 `
               -ArgumentList "MySite", "C:\inetpub\MySite", 443

或在当地:

.\Create-WebSite.ps1 -WebSiteName "MySite" -PathInfo "C:\inetpub\MySite" -Port 443

以下是脚本,我将其命名为 Create-WebSite.ps1

[cmdletbinding(SupportsShouldProcess=$True,ConfirmImpact='High')]  
Param(  
    [Parameter(Mandatory = $True,Position = 0,ValueFromPipeline = $True)]  
    [string]$WebSiteName,  

    [Parameter(Mandatory = $True,Position = 1,ValueFromPipeline = $True)]  
    [string]$PathInfo,

    [Parameter(Mandatory = $false,Position = 3,ValueFromPipeline = $True)]  
    [int]$Port = 80,

    [Parameter(Mandatory = $false,Position = 4,ValueFromPipeline = $True)]  
    [string]$HostHeader
) 

begin
{
    Import-Module WebAdministration
}

process
{
    ForEach-Object {

        New-Item $PathInfo -ItemType Directory -force
        New-Item "$PathInfo\$WebSiteName" -ItemType Directory -force
        Set-Content "$PathInfo\$WebSiteName\app_offline.htm" "$WebSiteName under maintenance" 

        if ($WhatIfPreference.IsPresent) 
        {
            write-host "What if: Performing operation New-WebAppPool -Name $WebSiteName"
            write-host "What if: Performing operation New-Website -Name $WebSiteName -ApplicationPool $WebSiteName -HostHeader $HostName -PhysicalPath $PathInfo -Port $Port"
        }
        else
        {
            New-WebAppPool -Name $WebSiteName 

            New-Website -Name $WebSiteName `
                        -ApplicationPool $WebSiteName `
                        -HostHeader $HostHeader `
                        -PhysicalPath $PathInfo `
                        -Port $Port `
                        -SSL

            New-WebApplication -Site $WebSiteName `
                               -Name $WebSiteName `
                               -PhysicalPath "$PathInfo\$WebSiteName" `
                               -ApplicationPool $WebSiteName
        }

    }
}

end
{}

答案 1 :(得分:0)

要编写用于管理SharePoint的PowerShell脚本,您有两个选择

  1. 打开SharePoint Shell并逐行编写脚本
  2. 打开PowerShell ISE并导入eh SharePoint管理单元并编写代码

这是您可以在Powershell ISE中使用的代码: 注意:根据您的环境更改变量
首先,要在SharePoint中创建网站,您必须创建一个Web应用程序,您可以通过以下链接进行操作: https://docs.microsoft.com/en-us/powershell/module/sharepoint-server/new-spwebapplication?view=sharepoint-ps

并创建网站,编写此代码

Add-PSSnapin Microsoft.Sharepoint.powershell -ErrorAction SilentlyContinue
#Variables
#-----------------------------------
$SiteURL = "http://contoso:4455/" #the site URL of webapplication which you have created
$SiteName = "sitename" 
$SiteOwner = "domain\farmadmin" #Primary Site Collection Administrator 
$SecondSiteOwner = "Domian\secondadminuser" #Secondary Site Collection Administrator 
$SiteTemplate = "DEV#0" #Developer Site Template for more site template go to https://vladtalkstech.com/2017/03/sharepoint-2016-site-template-id-list-for-powershell.html

# Create Site Collection

New-SPSite -Name $SiteName -Url $SiteURL -Template $SiteTemplate -OwnerAlias $SiteOwner -SecondaryOwnerAlias $SecondSiteOwner -Confirm:$false