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

杰夫·泰勒

我正在使用Import-SPAppPackage部署应用程序Install-SPApp我希望能够用来Set-AppPrincipalPermission设置权限,但无法正常使用。

我正在使用PowerShell cmdletImport-SPAppPackage将SharePoint托管的应用程序上载到SharePoint Install-SPApp对于不需要其他权限的SharePoint托管应用程序,此方法可以很好地工作。

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

当我通过PowerShell添加此应用程序时,它没有机会提出要求。继续安装不会出现问题,但是该应用程序无法正常工作。(它失败并出现权限问题,这是绝对正确的行为,因为尚未授予权限。)

我可以通过以下方法修复权限:转到网站目录,单击问题应用程序的“ ...”,选择“权限”,然后单击显示“如果应用程序权限有问题的链接,请单击此处以信任”再来一次。

但是我真的很想能够通过PowerShell进行整个部署。

Set-AppPrincipalPermissioncmdlet应该允许我设置权限,但是我无法使其正常工作。具体来说,我无法获得在部署应用程序时自动创建的应用程序主体的句柄,因此无法将此应用程序主体传递给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。

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

马库斯·施密特(Markus Schmitt)

我像您一样在同样的问题上挣扎,最后在这两个博客中找到了答案:

具有良好的安装,更新和删除脚本的博客

这是一篇关于通过PowerShell链接“按下”“信任”按钮的好帖子

而且因为我知道像我这样的懒惰程序员,所以可以随时使用此合并脚本来安装Apps:

    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.";
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过 PowerShell 授予 Azure 应用程序对 API(例如“Office SharePoint Online”)的权限

存储SharePoint托管的应用程序的属性

通过PowerShell检索Azure AD应用程序的“ API权限”

通过PowerShell在IIS中设置应用程序设置

Sharepoint 2013自动托管的应用程序与Azure中的提供商托管的应用程序

如何设置应用程序/缓存和应用程序/日志文件夹的权限?

如何使用 powershell 命令为 Azure DataLake Store 中的 Azure Active Directory 应用程序设置权限

修剪Sharepoint托管应用程序中内容的最佳实践?

特定于应用程序的权限设置

如何托管开发的Web应用程序?

如何使用Powershell Az模块授予Azure AD应用程序访问所需权限的权限

如何使用Powershell设置Azure逻辑应用程序定义

如何利用Powershell通过命令打开应用程序?

如何通过Powershell配置新的Azure AD应用程序?

如何通过我的应用程序设置定期任务?

授予托管标识权限以通过 ARM 模板部署脚本在 Azure AD 中创建应用程序注册

通过应用程序设置设置MachineKey

发布打包为SharePoint Online门户的自动托管应用程序的Office应用程序

通过Powershell向Azure RM AD应用程序添加权限

通过Az模块创建azure应用程序,并使用powershell分配API权限

如何通过功能应用程序中的应用程序设置来解析KeyVault对机密的引用?

如何在React应用程序中设置生产环境变量?(托管在Netlify上)

如何为要由JBoss AS 7.1托管的JSF2应用程序设置Gradle构建?

如何在运行时设置非托管应用程序的首选CLR(<supportedRuntime>)?

如何为Katana / Owin自托管应用程序设置默认静态网页?

如何设置具有节点后端的 Angular 8 应用程序并将其托管在 firebase 上?

如何使用REST API作为托管应用程序从客户端上载和获取SharePoint文档库中的项目?

Android如何检查应用程序权限?

如何列出flatpak应用程序的权限?