Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baywet/9f967ff03f9aa48ef8bee8dec5a1cf8a to your computer and use it in GitHub Desktop.
Save baywet/9f967ff03f9aa48ef8bee8dec5a1cf8a to your computer and use it in GitHub Desktop.
powershell script to remove SharePoint provider hosted add-ins that have an app uninstall event which fails
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
Function Get-ClientContext([string]$Url,[string]$UserName,[string]$Password)
{
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
return $context
}
Function Uninstall-AppInstance([Microsoft.SharePoint.Client.ClientContext]$Context,[Guid]$AppInstanceId)
{
$appInst = $Context.Web.GetAppInstanceById($AppInstanceId)
$appInst.Uninstall()
$context.ExecuteQuery()
}
$UserName = "joe@contoso.onmicrosoft.com"
$Password = Read-Host -AsSecureString -Prompt "Enter the password"
$BSTR = [System.Runtime.InteropServices.marshal]::SecureStringToBSTR($Password);
$Password = [System.Runtime.InteropServices.marshal]::PtrToStringAuto($BSTR);
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR);
$Url = "https://consoto.sharepoint.com/"
$AppInstanceid = New-Object Guid("00000000-0000-0000-0000-000000000000") #specify App Instance Id here
$context = Get-ClientContext -Url $Url -UserName $UserName -Password $Password
Uninstall-AppInstance -Context $context -AppInstanceId $AppInstanceid
$context.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment