【问题标题】:How to create a sharepoint site collection remotely via powershell?如何通过powershell远程创建sharepoint网站集?
【发布时间】:2021-06-02 01:26:57
【问题描述】:

如何通过 Powershell 远程创建新网站集 我尝试了以下方法,但都失败了 试用一:

# Create a PSCredential Object using the "User" and "Password" parameters that you passed to the job
$SecurePassword = '...............' | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList "domain\user", $SecurePassword
$spms = New-PSSession -ComputerName servername -Credential $cred 


$Parameters= @{

Session = $spms

ArgumentList ="siteurl","sitename",'domain\user','domain\user2'
ScriptBlock = { Param ($SiteURL,$SiteName,$SiteOwner,$SecondSiteOwner)
                Add-PSSnapin Microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
                Function Recreate_Site_Collection ($SiteURL,$SiteName,$SiteTemplate,$SiteOwner,$SecondSiteOwner){
# Delete Site Collection
$site = get-spsite  -Identity $SiteURL -ErrorAction SilentlyContinue
if($site){

Write-host "Removing the site $SiteURL" -f Red
Remove-SPSite -Identity $SiteURL -Confirm:$false
}
# Create Site Collection
Write-host "creating  the site $SiteURL" -f green
echo $SiteOwner 
echo $SecondSiteOwner
New-SPSite -Name $SiteName -Url $SiteURL -Template $SiteTemplate -OwnerAlias $SiteOwner -SecondaryOwnerAlias $SecondSiteOwner -Confirm:$false
}
                $SiteTemplate = "DEV#0" #Developer Site Template

                 Recreate_Site_Collection $SiteURL $SiteName $SiteTemplate $SiteOwner $SecondSiteOwner
}}
Invoke-Command  @Parameters

错误: 找不到用户。 但是,如果我在服务器上使用相同的变量运行相同的代码,那么当我以管理员身份运行 PowerShell 时就可以了

试验二:

# Create a PSCredential Object using the "User" and "Password" parameters that you passed to the job
$SecurePassword = '..........' | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList "domain/user", $SecurePassword
$spms = New-PSSession -ComputerName servername -Credential $cred 


$Parameters= @{
Session = $spms
ArgumentList = "siteurl","sitename",'domain/user',"1033","DEV#0"
ScriptBlock = { Param ($PortalUrl,$SiteName,$userName,$LCID,$WebTemplate)
$adminSiteUrl = "centraladminurl"
$user = "user"
$pwd = '..............'
$securePwd = ConvertTo-SecureString $pwd -AsPlainText -Force
$cred = New-Object PSCredential($user, $securePwd)
$wsdlUrl = $adminSiteUrl + "/_vti_adm/Admin.asmx?WSDL"
$svc = New-WebServiceProxy -Uri $wsdlUrl -Credential $cred
$svc.Timeout = 300000    # 5 minute timeout
$svc.CreateSite(
    $siteUrl,   # URL
    $siteTitle, # Title
    $Description, # Description
    $LCID, # LCID 1033 arabic
    $WebTemplate,    # WebTemplate
    $user,      # Owner Login
    "user",  # Owner Name
    "mail",
    $PortalUrl ,
    "portalname"
    )}}
    
    Invoke-Command  @Parameters

错误: 使用“10”参数调用“CreateSite”的异常:“引发了‘Microsoft.SharePoint.SoapServer.SoapServerException’类型的异常。” 但是,当使用 PowerShell 在服务器本身上以管理员身份运行相同的脚本时,它也可以使用相同的变量

请帮忙 我需要从 Jenkins 自动化 SharePoint 部署,这需要远程 PowerShell 脚本,主要步骤是创建无法建立的站点

【问题讨论】:

  • 这个类似的答案可能会有所帮助,看起来您可能会遇到声明身份验证问题:sharepoint.stackexchange.com/questions/108504/…。在运行CreateSite() 之前,$user 的值是多少?
  • 实际上,在这个解决方案中,他使用的是 ADFS,但我使用的是域“windows 用户”,它在本地机器上正常工作,因为我以管理员身份运行 PowerShell,我已经尝试过它,但我无法创建声明,因为我无法创建受信任的登录提供程序
  • 请帮助,因为所有部署都无法从任何地方“TFS,Jenkins”自动化,因为如果有任何解决方案而不是 TFS,Jenkins 和 PowerShell,我无法远程重新创建 SharePoint 站点,欢迎您只需要我需要远程重新创建 SharePoint 网站集

标签: powershell sharepoint


【解决方案1】:

这绝对是权限问题,但查找起来可能有点麻烦。我的用户(运行脚本的用户 - 可能是您的 Jenkins 用户)是:

  • 服务器上的本地管理员,
  • 列在https://sharepointsite/ > 网站设置 > 权限 > 网站集管理员中。

但远程运行时仍然出现奇怪的错误:

# These commands did not work - I'm quoting the errors:
Enter-PSSession -ComputerName MySPServer
Add-PSSnapIn Microsoft.SharePoint.PowerShell
$URI = 'https://MySharePoint.domain.com'

# Can't pipe Site object to Web
Get-SPSite $URI | Get-SPWeb
'Get-SPWeb : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'

# Can't get SP users using web:
$web = Get-SPWeb $URI
Get-SPUser 'DOMAIN\user' -Web $web
'Get-SPUser : Cannot find an SPWeb object with Id or Url: 7aee0dc1-fe3b-4f9b-bd4b-000000000000 and Site Id: 8b662499-bb26-41ce-978e-000000000000.'

# Can't list properties of a site object
Get-SPSite $URI | fl
'Exception has been thrown by the target of an invocation.'

# Can't create a new site because the user can't be found:
New-SPSite -Url $URI -Name 'Test' -Description 'Test Site' -Template 'DEV#0' -OwnerAlias 'DOMAIN\user'
'New-SPSite : User cannot be found.'

我发现我的用户由于某种原因没有在 powershell 中显示为站点管理员。请注意,我在这里直接在服务器上运行以查看用户:

$URI = 'https://MySharePoint.domain.com'
$web = Get-SPWeb $uri
Get-SPUser "$env:USERDOMAIN\$env:USERNAME" -Web $web | Select UserLogin,IsSiteAdmin,IsSiteAuditor,AllowBrowseUserInfo

'UserLogin   IsSiteAdmin IsSiteAuditor AllowBrowseUserInfo
 ---------   ----------- ------------- -------------------
 DOMAIN\user False       False         True'

所以我将自己添加为我的主站点的站点管理员,如下所示:

# Set my user as collection admin:
Get-SPUser 'DOMAIN\User' -Web $web | Set-SPUser -IsSiteCollectionAdmin:$true

# Check permissions again:
Get-SPUser "$env:USERDOMAIN\$env:USERNAME" -Web $web | Select UserLogin,IsSiteAdmin,IsSiteAuditor,AllowBrowseUserInfo

'UserLogin   IsSiteAdmin IsSiteAuditor AllowBrowseUserInfo
 ---------   ----------- ------------- -------------------
 DOMAIN\user True        True          True'

Get-SPSite $uri | fl UserIsSiteAdminInSystem

'UserIsSiteAdminInSystem : True'

现在上面所有的命令都可以正常工作了。似乎 sharepoint 的 powershell 模块需要对主要 Web 服务对象进行一些访问才能解析用户名。我不确定真正需要什么级别的访问权限,但这对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多