How to add SharePoint App to Site using PnP Powershell script - sharepoint

I manually do these now. Is there a way to automate the above steps via PowerShell script?

You need site owner permissions to be able to add apps to SharePoint Online Site.
Refer to my PnP Powershell script:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Legal"
$AppName = "Modern Script Editor web part by Puzzlepart"
#Connect to SharePoint Online App Catalog site
Connect-PnPOnline -Url $SiteURL -UseWebLogin
#Get the App from App Catalog
$App = Get-PnPApp -Scope Tenant | Where {$_.Title -eq $AppName}
#Install App to the Site
Install-PnPApp -Identity $App.Id

Related

Powershell Script | Create New Web application in Sharepoint 2019

Can someone share powershell script of creating new web application in Sharepoint 2019 WITH SQL SA authentication? I have found so many scripts which just talk to windows authtication of SQL Server but I have Azure managed SQL instance linked with Sharepoint server 2019.
Please help.
By doing some research I got working script:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Define Variables for Web Application Creation
$WebAppName = "abc.xyz.com"
$WebAppURL = "http://abc01"
$WebAppPort = 9000
$ContentDBName = "WSS_Contents_Abc"
$AppPoolName = "SPABC"
$AppPoolAccount = "Test\ABC"
$DatabaseServer = "sql-managedinstance-902932test.net" #Alias of your DB Server
#Authentication Provider
$AuthProvider = New-SPAuthenticationProvider -UseWindowsIntegratedAuthentication
-DisableKerberos $FarmAccountLogin = "Test\ABCSQL";
$FarmAcountObj = Get-Credential -Credential $FarmAccountLogin;
#Create new Web Application
New-SPWebApplication -name $WebAppName -port $WebAppPort -URL $WebAppURL -
ApplicationPool $AppPoolName -ApplicationPoolAccount (Get-SPManagedAccount
$AppPoolAccount) -AuthenticationMethod NTLM -AuthenticationProvider
$AuthProvider -DatabaseServer $DatabaseServer -DatabaseCredentials
$FarmAcountObj -DatabaseName $ContentDBName

azure active directory login failed while deploying blockchain workbench

Some one please help after opening the url of app services i am adding my institutional AD tenant to cloud shell for authentication but it gives error of login failed.orkbench
There were some recent changes to Azure Cloud Shell. While we are working on a solution, here is some workaround:
Before running cd; Invoke-WebRequest -Uri https://aka.ms/workbenchAADSetupScript -OutFile workbenchAADSetupScript.ps1; ./workbenchAADSetupScript.ps1 -SubscriptionID * -ResourceGroupName * -DeploymentId ***
Run Remove-Item -Path Function:\Connect-AzureAD in your Azure Cloud Shell session
Reference: https://social.msdn.microsoft.com/Forums/en-US/a6101462-ccc1-4922-a5ea-2d28890cb9a4/azure-blockchain-workbench-cloud-shell-active-directory-tenant-login-failed-error?forum=azureblockchain

How do I run PnP Script on Azure Function App

I am trying to get a user to run a powershell script through an Azure function. This script is supposed to create a site on SharePoint with some info provided by users.
I created an Azure Function App to run some PowerShell scripts. When I tried to run my code, it returned an error http 502 at Connect-PnPOnline
If I remove Connect-PnPOnline, I get a 200 response. So I am sure that it has to do with Connect-PnPOnline in my script.
I followed the post by #Lee_MSFT Get PowerShell Script to Run SharePoint Commands on Azure
and able to import modules.
using namespace System.Net
param (
[string]$projectnumber,
[string]$projectname
)
$site = "https://xxxxx.sharepoint.com/sites/projects"
$projectsite = -join($site, "/", $projectnumber)
$projecttitle = -join($projectnumber, " ", $projectname)
Connect-PnPOnline -url $site
...
I got 500 from Connect-PnPOnline -url $site and a 502 from Connect-PnPOnline -url $site -UseWebLogin
Anyone know that why I have 5xx errors while using Connect-PnPOnline?
Thanks a lot!
Make sure you have upload the PnP PowerShell module for your Azure Function, then use the PowerShell below to connect the SharePoint.
$siteURL = "https://tenant.sharepoint.com/sites/projects"
$userId = "abc#tenant.onmicrosoft.com"
$plainText= "*****"
$pwd = ConvertTo-SecureString $plainText -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential($userId,$pwd)
Connect-PnPOnline -Url $siteURL -Credentials $creds
Or use this:
Connect-PnPOnline -AppId $env:SPO_AppId -AppSecret $env:SPO_AppSecret -Url "https://tenant.sharepoint.com/sites/projects"
SPO_AppId - Set the value to the Client ID you copied in the first step when you created your app on your tenant.
SPO_AppSecret - Set the value to the Client Secret that you copied in the first step when you created your app on your tenant.
I suggest you check the steps in the article below.
Azure Functions For SharePoint Operations Using PnP PowerShell

Setting script to run at user login on Azure VM via ARM

I have generalized a windows Azure image and I am using it in an ARM (Azure resoure manager) template to create a new VM. At the time of VM creation I am doing some bootstrapping work using Custom Script extension that runs a Powershell script. It works fine except for few things that are specific to a user that will log on to machine. For example, I can't map a network drive or set powershell exection policty to remotesigned for the user who will log in to the machine via Custom Script Extension.
Is there a way to accomplish user specific tasks in the ARM template or set some scripts to run when user will log in?
Maybe a bit late, but I am using sth similar to this:
However, we can’t directly hand this script to Custom Script Extension yet. Custom Script Extension uses NTAUTHORITY\SYSTEM account to execute scripts, but the account doesn’t have necessary privileges to execute the SQL configuration commands. To work around this, we’ll create a separate bootstrap script, which impersonates an administrator account before invokes the script.
Here is the code used:
$password = ConvertTo-SecureString "[your admin account user password]" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential("$env:COMPUTERNAME\[your admin account]", $password)
$command = $file = $PSScriptRoot + "\CustomScriptSQLPS.ps1"
Enable-PSRemoting –force
Invoke-Command -FilePath $command -Credential $credential -ComputerName $env:COMPUTERNAME
Disable-PSRemoting -Force
I don't know if remoting is relevant for this though ...
Credits to Haishi Bai

VSO/VSTS: Azure Powershell - The specified credentials cannot be used with the authentication scheme 'Basic'

I have the following build step which runs a script in Azure Powershell. The script basically publishes files to the web app (see further down for details).
This works just fine with all of my web apps EXCEPT FOR ONE. I get the following error:
scriptCommand= & "C:\a\8b0700333\MyProj\Scripts\Publish.ps1"
-websiteName mywebapp -packOutput "C:\a\8b0700333\MyProj\Api"
Stopping web app...
Publishing web app...
Publishing with publish method [MSDeploy]
[error]Error: The specified credentials cannot be used with the
authentication scheme 'Basic'.
[error]Error: Default credentials
cannot be supplied for the Basic authentication scheme.
[error]Parameter name: authType
[error]Error count: 1.
The Publish.ps1 script:
param($websiteName, $packOutput)
$website = Get-AzureWebsite -Name $websiteName
# get the scm url to use with MSDeploy. By default this will be the second in the array
$msdeployurl = $website.EnabledHostNames[1]
$publishProperties = #{'WebPublishMethod'='MSDeploy';
'MSDeployServiceUrl'=$msdeployurl;
'DeployIisAppPath'=$website.Name;
'Username'=$website.PublishingUsername;
'Password'=$website.PublishingPassword
'SkipExtraFilesOnServer'='False'}
Write-Output "Stopping web app..."
Stop-AzureWebsite -Name $websiteName
Write-Output "Publishing web app..."
$publishScript = "${env:ProgramFiles(x86)}\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Web Tools\Publish\Scripts\default-publish.ps1"
. $publishScript -publishProperties $publishProperties -packOutput $packOutput
Write-Output "Starting web app..."
Start-AzureWebsite -Name $websiteName
Had the same issue, the problem was coming from the fact that apparently the script Microsoft provides doesn't support slots.
Made a fixed version here https://gist.github.com/baywet/f7ed425c48ccde4399c7
Helping myself from this post How can I get the PublishUrl via Azure PowerShell?
This error usually occurs when using MS deploy HTTP Basic Authentication without any credentials specified. So you can add following code in the PowerShell script to check if the publish username and password for the web app is get correctly.
Write-Output $website.PublishingUsername
Write-Output $website.PublishingPassword
And also, check the value of msdeployurl to see if it is the correct URL (Usually like: youwebappname.scm.azurewebsites.net) for deployment.
Write-Output $msdeployurl

Resources