SharePoint App Only Registration Key Renewal script
##################################
## Checking secret working
##################################
$clientSecret = 'xxxxx'
Connect-PnPOnline -Url "https://xxxx.sharepoint.com/sites/DevSite" -ClientId 67c86709-5348-45b9-a16b-9b7a983a20c2 -ClientSecret $clientSecret
Get-PnPConnection
Get-PnPListItem -List LookupList
###################################
## Get all Keys
####################################
$msolcred = get-credential
connect-msolservice -credential $msolcred
$clientId = "67c86709-5348-45b9-a16b-9b7a983a20c2"
$keys = Get-MsolServicePrincipalCredential -AppPrincipalId $clientId
$keys
##################################
## Remove keys
##################################
Remove-MsolServicePrincipalCredential -KeyIds $keys.KeyId -AppPrincipalId $clientId
########################################
## Generate key for 3 years
########################################
$bytes = New-Object Byte[] 32
$rand = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rand.GetBytes($bytes)
$rand.Dispose()
$newClientSecret = [System.Convert]::ToBase64String($bytes)
$dtStart = [System.DateTime]::Now
$dtEnd = $dtStart.AddYears(3)
New-MsolServicePrincipalCredential -AppPrincipalId $clientId -Type Symmetric -Usage Sign -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
New-MsolServicePrincipalCredential -AppPrincipalId $clientId -Type Symmetric -Usage Verify -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
New-MsolServicePrincipalCredential -AppPrincipalId $clientId -Type Password -Usage Verify -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
$newClientSecret
############################################
## Checking secret working again
############################################
Connect-PnPOnline -Url "https://xxxx.sharepoint.com/sites/DevSite" -ClientId 67c86709-5348-45b9-a16b-9b7a983a20c2 -ClientSecret $newClientSecret
Get-PnPListItem -List LookupList
Comments
Post a Comment
Your comments...