powershell - 如何通过 PowerShell 设置 SharePoint 托管应用程序的权限?

标签 powershell sharepoint sharepoint-2013

我正在使用 Import-SPAppPackage 部署应用程序和 Install-SPApp .我希望能够使用 Set-AppPrincipalPermission设置权限,但我无法让它工作。

我正在使用 PowerShell cmdlet 将 SharePoint 托管的应用程序上传到 SharePoint Import-SPAppPackageInstall-SPApp .这适用于不需要额外权限的 SharePoint 托管应用程序。

但是,一个应用程序需要对站点进行读取访问,因此在 list 中声明了这一点。它在通过 Visual Studio 运行时工作正常 - 在第一次启动时,它正确地要求信任该应用程序以对该站点进行读取访问。

当我通过 PowerShell 添加此应用程序时,它没有机会询问。安装继续没有问题,但随后应用程序无法运行。 (它因权限问题而失败,这绝对是正确的行为,因为尚未授予权限。)

我可以通过转到站点内容来修复权限,单击有问题的应用程序的“...”,选择“权限”,然后单击显示“如果应用程序的权限有问题,请单击此处信任”的链接再说一遍'。

但我真的希望能够通过 PowerShell 完成整个部署。
Set-AppPrincipalPermission cmdlet 应该允许我设置权限,但我无法让它工作。具体来说,我无法获得部署应用程序时自动创建的应用程序主体的句柄,因此我无法将此应用程序主体传递给 Set-AppPrincipalPermission .

应用程序主体的名称格式为“i:0i.t|ms.sp.int|@”,它列在/_layouts/15/appprincipals.aspx 上。当我使用 Get-SPAppPrincipal有了它,我得到的是:

Get-SPAppPrincipal : The app principal could not be found.

我还没有看到任何使用 Get-SPAppPrincipal 的例子对于任何 SharePoint 托管的应用程序 - 它们似乎都适用于提供商托管的应用程序。它们似乎都只使用从客户端 ID 和领域 ID 构建的应用程序主体 ID,但我的 SharePoint 托管应用程序没有客户端 ID。

是否可以通过 PowerShell 获取 SharePoint 托管应用程序的应用程序主体并使用它来设置权限?我做错了什么,还是有另一种方法?

最佳答案

我和你一样遇到了同样的问题,终于在这两个博客中找到了答案:

Blog with a nice Install, Update and Delete Script

这是一篇关于通过PowerShell“按下”“信任”按钮的好文章Link

而且因为我知道像我这样的程序员是多么的懒惰,所以可以随意使用这个合并的脚本来安装应用程序:

    param
(
    [string]$Web = $(throw '- Need a SharePoint web site URL (e.g. "http://portal.contoso.com/")'),
[string]$Source = "ObjectModel"
)

Write-Host -ForegroundColor White "-------------------"
Write-Host -ForegroundColor White "| App Installer |"
Write-Host -ForegroundColor White "-------------------"
Write-Host -ForegroundColor White "- "

#Global vars
$AppPackageName = "App.app";

#Loads powershell settings
Write-Host -ForegroundColor White "- Load Powershell context.."
$0 = $myInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)

#Loads the SharePoint snapin
Write-Host -ForegroundColor White "- Load SharePoint context.."
$ver = $host | select version
if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell";
}
[void][System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") 

#Imports the App package
Write-Host -ForegroundColor White "- Import app package '$AppPackageName'..."
$appPath = "C:\Projects\App\App\bin\Debug\app.publish\1.0.0.0" + "\" + $AppPackageName;
if ($Source.Equals("ObjectModel", [System.StringComparison]::InvariantCultureIgnoreCase)) {
$sourceApp = ([microsoft.sharepoint.administration.spappsource]::ObjectModel);
}
elseif ($Source.Equals("Marketplace", [System.StringComparison]::InvariantCultureIgnoreCase)) {
$sourceApp = ([microsoft.sharepoint.administration.spappsource]::Marketplace);
}
elseif ($Source.Equals("CorporateCatalog", [System.StringComparison]::InvariantCultureIgnoreCase)) {
$sourceApp = ([microsoft.sharepoint.administration.spappsource]::CorporateCatalog);
}
elseif ($Source.Equals("DeveloperSite", [System.StringComparison]::InvariantCultureIgnoreCase)) {
$sourceApp = ([microsoft.sharepoint.administration.spappsource]::DeveloperSite);
}
elseif ($Source.Equals("RemoteObjectModel", [System.StringComparison]::InvariantCultureIgnoreCase)) {
$sourceApp = ([microsoft.sharepoint.administration.spappsource]::RemoteObjectModel);
}

$spapp = Import-SPAppPackage -Path "$appPath" -Site $Web -Source $sourceApp -Confirm:$false -ErrorAction SilentlyContinue -ErrorVariable err;
if ($err -or ($spapp -eq $null)) 
{
Write-Host -ForegroundColor Yellow "- An error occured during app import !"
throw $err;
}
Write-Host -ForegroundColor White "- Package imported with success."

#Installs the App
Write-Host -ForegroundColor White "- Install the APP in web site..."
$app = Install-SPApp -Web $Web -Identity $spapp -Confirm:$false -ErrorAction SilentlyContinue -ErrorVariable err;
if ($err -or ($app -eq $null)) {
Write-Host -ForegroundColor Yellow "- An error occured during app installation !"
throw $err;
}
$AppName = $app.Title;
Write-Host -ForegroundColor White "- App '$AppName' registered, please wait during installation..."
$appInstance = Get-SPAppInstance -Web $Web | where-object {$_.Title -eq $AppName};
$counter = 1;
$maximum = 150;
$sleeptime = 2;

Write-Host -ForegroundColor White "- Please wait..." -NoNewline;

$url = "$($Web)_layouts/15/appinv.aspx?AppInstanceId={$($appInstance.Id)}"
$ie = New-Object -com internetexplorer.application
try
{
    $ie.visible=$true
    $ie.navigate2($url)
    while ($ie.busy)
    {
        sleep -milliseconds 60
    }
    $trustButton = $ie.Document.getElementById("ctl00_PlaceHolderMain_BtnAllow")         
    $trustButton.click() 
    sleep -Seconds 1
    Write-Host "App was trusted successfully!"
}
catch
{
    throw ("Error Trusting App");
}

while (($appInstance.Status -eq ([Microsoft.SharePoint.Administration.SPAppInstanceStatus]::Installing)) -and ($counter -lt $maximum))
{
Write-Host -ForegroundColor White "." -NoNewline;
sleep $sleeptime;
$counter++;
$appInstance = Get-SPAppInstance -Web $Web | where-object {$_.Title -eq $AppName} 
}

Write-Host -ForegroundColor White ".";

if ($appInstance.Status -eq [Microsoft.SharePoint.Administration.SPAppInstanceStatus]::Installed) {
Write-Host -ForegroundColor White "- The App was successfully installed.";
$appUrl = $appInstance.AppWebFullUrl;
Write-Host -ForegroundColor White "- The App is now available at '$appUrl'.";
Write-Host -ForegroundColor White  "- (Don't forget to add app host name in your host file if necessary...).";
Write-Host -ForegroundColor White "- "
}
else {
Write-Host -ForegroundColor Yellow "- An unknown error has occured during app installation. Read SharePoint log for more information.";
}

关于powershell - 如何通过 PowerShell 设置 SharePoint 托管应用程序的权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25246144/

相关文章:

javascript - 共享点; Javascript 重复点击事件已经在页面上

Powershell 查找服务器操作系统

linux - Windows cmd/powershell 是否有等效的 pkexec?

powershell - PowerShell 和正斜杠有什么奇怪的吗?

powershell - 展开目录列表并复制

sharepoint - 在 Sharepoint 2013 中使用 JSLink 在 View 上创建编辑控制 block (ECB)

sharepoint - 为什么 SPList 的 LastItemModifiedDate 与列表中显示的修改日期不同?

c# - 从 SharePoint 组中的 Active Directory 获取用户权限

javascript - 删除空白文本节点/链接

javascript - SharePoint 2013 JSLink OnPostRender