【问题标题】:Creating a site in sharepoint using powershell使用 powershell 在 sharepoint 中创建站点
【发布时间】:2013-01-23 21:25:32
【问题描述】:

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

谢谢

【问题讨论】:

  • 这是一个很好的模式,可以显示您的代码、您遇到的错误并添加有关您的环境的任何信息,这些信息可以帮助您找到答案。
  • 请分享您的代码或错误以帮助您解决问题

标签: sharepoint powershell


【解决方案1】:

试试这个,如果你想使用 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
{}

【讨论】:

  • 哎呀,我把这个问题误读为如何创建一个网站,所以如果你需要的话,这里是如何做到这一点的!
【解决方案2】:

要编写管理 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

【讨论】:

  • 我希望你得到了你想要的答案,我可以提供任何澄清,将此标记为答案帮助其他搜索相同内容的人知道这是对你有帮助的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-09
  • 2017-04-29
  • 1970-01-01
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 2011-03-20
相关资源
最近更新 更多