Powershell Script | Create New Web application in Sharepoint 2019 - azure

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

Related

How to add SharePoint App to Site using PnP Powershell script

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

Link application insights to Azure web app through powershell

I'm trying to link new application insights to existing Azure web app through Powershell with the below script. I'm able to create a new app insight but unable to link the new app insight to the existing Azure web app.
$appInsights = New-AzResource -ResourceName 'MyWebsite09' -ResourceGroupName 'Test' `
-Tag #{ applicationType = 'web'; applicationName = 'sample1'} `
-ResourceType 'Microsoft.Insights/components' -Location 'North Central US' `
-PropertyObject #{'Application_Type'='web'} -Force
$appSetting = #{'APPINSIGHTS_INSTRUMENTATIONKEY'= $appInsights.Properties.InstrumentationKey}
Set-AzWebApp -Name 'sample1' -ResourceGroupName 'Test' -AppSettings $appSetting
Here is the Powershell commands to link application insights with exisiting azure web app . As your code will not enable the application insights , Follow the below code
$app = Get-AzWebApp -ResourceGroupName "AppMonitoredRG" -Name "AppMonitoredSite" -ErrorAction Stop
$newAppSettings = #{} # case-insensitive hash map
$app.SiteConfig.AppSettings | %{$newAppSettings[$_.Name] = $_.Value} # preserve non Application Insights application settings.
$newAppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"] = "012345678-abcd-ef01-2345-6789abcd"; # set the Application Insights instrumentation key
$newAppSettings["APPLICATIONINSIGHTS_CONNECTION_STRING"] = "InstrumentationKey=012345678-abcd-ef01-2345-6789abcd"; # set the Application Insights connection string
$newAppSettings["ApplicationInsightsAgent_EXTENSION_VERSION"] = "~2"; # enable the ApplicationInsightsAgent
$app = Set-AzWebApp -AppSettings $newAppSettings -ResourceGroupName $app.ResourceGroup -Name $app.Name -ErrorAction Stop
You can Refer to this MS DOC for linking application insights to azure web-app fully.
or, You can even refer the SO thread for more details .
The below commands work for me in Azure portal CloudShell. After implementing the below code we were able to link New Application insight for existing webapp.
Code:
$resourceGroupName = "****"
$resourceName = "***"
$appInsightsInstrumentationKey = "***"
$app = Get-AzWebApp -ResourceGroupName $resourceGroupName -Name $resourceName -ErrorAction Stop
$newAppSettings = #{} # case-insensitive hash map
$app.SiteConfig.AppSettings | %{$newAppSettings[$.Name] = $.Value} # preserve non Application Insights application settings.
$newAppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"] = $appInsightsInstrumentationKey; # set the Application Insights instrumentation key
$newAppSettings["APPLICATIONINSIGHTS_CONNECTION_STRING"] = "InstrumentationKey=$appInsightsInstrumentationKey"; # set the Application Insights connection string
$newAppSettings["ApplicationInsightsAgent_EXTENSION_VERSION"] = "~2"; # enable the ApplicationInsightsAgent
$app = Set-AzWebApp -AppSettings $newAppSettings -ResourceGroupName $app.ResourceGroup -Name $app.Name -ErrorAction Stop
Restart-AzWebApp -ResourceGroupName "*** " -Name "***"
Please update the values and try this code in Azure Portal CloudShell.
Note: The code is not working for me in Windows Powershell ISE application.

Not able to add my client IP in server firewall rule

I have two subscription plans QA-##### and Prod-########. First is used for QA environment and second one is used for the production environment. I am able to connect the Azure database on QA-###### subscription after adding my client IP in the server firewall list.
But I am not able to connect Azure SQL Database on Prod-######## subscription. When I am going to add my client IP in a server firewall rule, It's showing a success message but not listed there.
I also submitted a support ticket on azure help and support section but no response.
Sometimes there are issues with Azure portal that affect a small set of Azure customers. My suggestion is to use PowerShell to add the firewall rule needed while you wait for the issue to be fixed by Azure Support.
# Connect-AzAccount
# The SubscriptionId in which to create these objects
$SubscriptionId = ''
# Set the resource group name and location for your server
$resourceGroupName = "myResourceGroup-$(Get-Random)"
$location = "westus2"
# Set an admin login and password for your server
$adminSqlLogin = "SqlAdmin"
$password = "ChangeYourAdminPassword1"
# Set server name - the logical server name has to be unique in the system
$serverName = "server-$(Get-Random)"
$startIp = "0.0.0.0"
$endIp = "0.0.0.0"
# Set subscription
Set-AzContext -SubscriptionId $subscriptionId
# Create a resource group
$resourceGroup = New-AzResourceGroup -Name $resourceGroupName -Location $location
# Create a server with a system wide unique server name
$server = New-AzSqlServer -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-Location $location `
-SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminSqlLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
# Create a server firewall rule that allows access from the specified IP range
$serverFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-FirewallRuleName "AllowedIPs" -StartIpAddress $startIp -EndIpAddress $endIp

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

Apply template to a sharepoint online site

I am using the PnP approach for getting the publishing site template and creating a new sitecollection and applying the template so that i get the replica of the source site.
I am getting the error- Scope of template does not match target while running
the below powershell script to apply the template -
$web="https://shareptdev.sharepoint.com/sites/newpub/replicasite
$templateFile = "E:\Path\template.xml";
Apply-PnPProvisioningTemplate -Web $web -Path $templateFile
The variable $web must be your site context must use the Get-PnPWeb on the connected site. Something like this:
$siteUrl = "https://shareptdev.sharepoint.com/sites/newpub/replicasite"
Connect-PnPOnline -Url $siteUrl -UseWebLogin
$web = Get-PnPWeb
$templateFile = "E:\Path\template.xml"
Apply-PnPProvisioningTemplate -Web $web -Path $templateFile

Resources