PowerShell: How to develop interactive logs

In this article we will look for developing a logging mechanism for PowerShell automation components that can provide logs not just in form of a flat file but also as interactive html format. This best part of the story is we can design this html log format using whatever html/Css elements to make as intuitive as possible.

For the sake of clear understanding I am going to explain the complete process in various steps-

We need to download & install the “EnhancedHTML2” module.

In Step 1 we can see the command to download & install the module. We can use this command using PowerShell Console

Or we can download & install this module manually as shown in Step 2 from the PowerShell Gallery

0

In Step 3 we have added the code to query for all Lists in SharePoint Site, I keep it simple & straight for this demo

In Step 4 we have added code to build a collection keep list of SharePoint Lists using a PowerShell hash table as shown below-

1

In Step 5&6 we can see the implementation of “EnhancedHTML2” module

In Step 5 we will convert the data collection (Row & Column format) to “EnhancedHTMLFragment” by using “ConvertTo-EnhancedHTMLFragment” cmdlet. Similarly we can create any number of HTML Fragments as per our need

This cmdlet uses a switch “PreContent” that allows you to any HTML snippet before the fragment add to the HTML outcome

“-MakeTableDynamic” switch allows the cmdlet to embed basic html plumbing like Search, Pagination and so on to the HTML outcome

In Step 6 we will append all the HTML Fragments to build a single HTML file by using “ConvertTo-EnhancedHTML” cmdlet.

This cmdlet has got “HTMLFragments” parameter that will accept comma separated values of names of the Fragments that you want to include in the HTML document

Another important parameter “CssUri” accept the path of CSS file that will define the styling attributes for the HTML page

“Out-File” parameter defines the path to the save the HTML Log file

2

In Step 7 we will call the method to execute above steps

3

Step 8 the dlls that we need to include the script to make SharePoint Calls work

4

Step 9 shows the CSS file that we referred with “ConvertTo-EnhancedHTML” in Step 6

5

Step 10 shows the HTML outcome log file generated after we successfully executed the steps above

6

In Step 11 & 12 we can explore the html generated as part of this execution and it is amazing to see how this HTML is structured by adding references to the required JavaScript files and body elements.

It can be clearly noticed the generated html body content is in the form of table structure, this happened since we specified “-As Table” switch with “ConvertTo-EnhancedHTMLFragment” cmdlet in Step 5

Also we can notice the “<h1>” tag in the HTML body, it is the same content that we include to the fragment using “-PreContent” switch with “ConvertTo-EnhancedHTMLFragment” cmdlet in Step 5

78

And finally we can see the HTML based interactive logs in Step 13 shown below. This would be feature rich HTML Page and we can even further add more feature as per our needs, thought this would cost additional effort.

9

That is all for this demo.

Hope you find it helpful.

Advertisements

SharePoint 2016: How To Implement Alternate Access Mapping

In this article we will discuss implementing “Alternate Access Mapping” or commonly known as “AAM” in SharePoint 2016.

Alternate Access Mapping Architecture

If you are not aware of AAM or you have some misconceptions about it, I would highly recommend you to read through an excellent blog Alternate Access Mappings (AAMs) *Explained by “Brain Pendergrass” from Microsoft and would like to thank him for such an awesome blog to make me understand this concept in depth.

This article will focus on guided steps to configure AAM in SharePoint 2016 and we won’t discuss AAM as a concept.

Create New Web Application

Step 1: To start the demo let go to SharePoint Central Admin Site and click on “Manage Web Applications” as shown below-

1

I am creating a new Web Application for demo purpose. In real environments we can use any existing Web Application to perform these steps.

Step 2: Click “New” menu to launch “Create New Web Application” wizard

2

Step 3: Enter Web Application Name and other necessary information

3

Once done click OK to start the process

4.0

When the creation process has been completed then we can see a new Web Application listed in the list of Web Applications

4.1

Also we may see the Modal Dialog that offers quick link to create new Site Collection for this Web Application

4.2

Create New Site Collection

Step 4: Click on “Create Site Collection” link to create new Site Collection for the Web Application.

Select Web Application

5

Enter Title, URLs, Template, Primary Site Collection Administrator

6

Click “OK” to start the process

7

Once process has been completed we can see new Site Collection created for the Web Application

9

We can navigate to the site collection by clicking the URL

10

Add DNS Entries

Step 5:  Now we have configure add host entries to DNS.

Search for DNS

11

Add new Host Entry to the “Forward Lookup Zone” as shown below-

12

In New Host screen, enter Host Name, Select FQDN, enter IP Address. Once done click “Add Host”

13

If operation completed successfully we can see popup window show success message

14

We can see this new Host added to the existing list

15

Add/Update IIS Bindings

Step 6: Add “IIS” Bindings

Now we have to add IIS bindings for the new Host. Search for “IIS” and select “Internet Information Services (IIS) Manager”

16

Select the Web Application that we have created in above steps from the “Connections” Panel on the left under “Sites” node

Click on “Bindings…” on the right to Add/Edit Web Application Bindings

17

In the “Site Bindings” screen select the default binding and click “Edit”

18

In the “Edit Site Binding” screen enter Host Name value and this should match the entry that we have created earlier in DNS

Click OK

19

Once saved the existing binding will look like as shown below-

20

Configure Alternate Access Mappings Using Central Admin

Step 7: Configure Alternate Access Mappings (From Central Admin)

Go To Central Admin -> “Application Management”

Under Web Applications Click “Configure alternate access mappings”

21

Click “Edit Public URLs”

22

In the “Edit Public Zone URLs” Screen and enter “Default Zone” URL with “Host Name” configured earlier as shown below.

Click “Save” once you entered the default Zone URL to save the data.

23

Now try accessing SharePoint Web Application using Public Zone URL and if the configuration goes well, the access will be granted to you

Test Alternate Access Mappings

2425

Configure Another Mapping

Step 8: Repeat Step 5 to add another DNS entry to add a new Host Name

262728

Step 9: Repeat Step 6 to Add Web Application Bindings

2930313233

Configure Alternate Access Mappings Using PowerShell

Step 10: Configure Alternate Access Mappings (Using PowerShell)

Now we will add this new Host Name to the default zone for the web application

Launch SharePoint 2016 Management Shell

34

“New-SPAlternateURL” cmdlet gives us option to add new Alternate URLs to the required zone for web application

This cmdlet take following parameters

  • New Alternate URL – URL that you need to register as alternate URL
  • Web Application Path – URL that represent Web Application
  • Zone – Represents a zone that you need this alternate URL to add to

35.0

Once this command executes successfully we can a new Alternate Access Mapping added to the list

35.1

Test Alternate Access Mapping

Now if try to access this web application using this new mapping it still be translated to the same Public URL for the Web Application.

3637

By following above steps we can enable a web application that receives the request from an internal URL in one of the five authenticated zones to return pages that contain links to the public URL for the zone

Hope you find it helpful.

SharePoint 2016: How To Get SPUser By User ID

While working with a whole range of applications using SharePoint Platform & Services I have come across lot of issues specially while dealing with SharePoint User Information let it by user names entered by application users on the UI or by querying user information by User ID.

In most scenarios you will need this to query data for the users based on User ID from within code itself. I have add a little User Interface in this article to make sure you get the idea on how this approach will work and what all properties will be exposed as results.

Here is the process flow that depicts the flow of information between Client Request & Server Response:

0

To start with the demo I have add some of HTML elements to prepare the UI with a textbox to enter User ID as shown below-

1

In the below screen shot you can see the simple HTML markup for the user interface.

We have a textbox where users can enter user id of the SharePoint User

2

In order to display the results I have add a HTML table as container. Purpose is to prepare HTML on the fly and paste it at runtime into this container.

3

In Step 1 we have bound the blur event of the textbox to a function that will execute the query against the User Data based on the User ID

4

In Step 2 we call another helper function “getUserById” by passing user id to it. This function call return a jQuery promise which can be further evaluated in upcoming steps

In Step 3 we will check if the JQuery call has been completed or not by using JQuery “when” construct.

In Step 4 we will call another helper function “renderUser” once the JQuery call has been completed in Step 3. The “renderUser” function is responsible to render User Information into the container.

5

In Step 5 we call “_api/Web” REST API endpoint can using its function “getUserById”, during this call we will specify “json” as datatype to ensure that we will get results in “json” format

6

In Step 6 we are rendering the details of the user in the container

7

And here is the final output of the operation performed.

So we can see Title, Login Name, Email returned back for a specific User Id as shown below-

8

That is all for this demo.

Hope you find it helpful.

 

SharePoint 2016: How to configure Usage & Health Service Application using PowerShell

Usage & Health Service Application is one of the most important Service Applications that provides vital information on the Health & State of SharePoint Farm.

In SharePoint 2016, Usage Service is collecting information on Timer Service Monitoring, Event Logs, Performance Counters, Search Usage, Sandbox Usage, and Site Collection Usage and much more.

This Service application is responsible to keep monitoring the resource, health & state associated with the SharePoint Farm and logs this information to Log Files on SharePoint Hive or any designated path along with SharePoint Logging Database “WSS_Logging” in SQL Server Database.

Many other components in SharePoint like Developer Dashboard, Search Analytics, Web Analytics Reports are using this data to provide logical User Interface for the end users.

You will get empty reports in Web Analytics reports section, if usage and health data collection service application is not configured properly.

It is not possible to Provision Usage & Health Service using Central Admin, as there is no User Interface available.

Let us first get into Central Admin Site to see if there is options available on the User Interface to provision Usage Service

Go to “Central Admin” – > “Manage Service Applications”

2

Click on “New” Menu on the Ribbon and in the existing list of available Service Application Templates we can see there is no template available for provisioning “Usage & Health Service Application”

3

This concludes that we have to provision Usage Service using PowerShell as shown in the following steps-

1

Launch “SharePoint 2016 Management Shell”

4

Step 1: Provision Service Application Instance

In this step, we will provision the Usage Service using “New- SPUsageApplication” cmdlet as shown below

New-SPUsageApplication -Name "Usage and Health Data Collection"

5

Step 2: Provision Service Application Proxy

In this step, we will provision Service Application Proxy for Usage Service by using the following commands

$serviceProxy = Get-SPServiceApplicationProxy | where {$_.TypeName -eq "Usage and Health Data Collection Proxy"}

6

$serviceProxy.Provision()

7

Step 3: Verify Service Application

In this step we will verify the provisioning of service application by looking at “Service Application” Page in “Central Administration” as shown below-

8

Step 4: Verify Service Application Database

In this step we will verify the provisioning of service application database by logging into “SQL Server Management Studio” as shown below-

9

10

This concludes the Service Application Provisioning Process using PowerShell.

Hope you find it helpful.

SharePoint 2016: How to configure State Service Application using PowerShell

There are many SharePoint Components like InfoPath Form Services, Visio, Search Service, Workflows and many more, relies on SharePoint State Service Application to application sessions across related HTTP(S) requests in a SQL Server Database.

In this article, we will discuss the steps involved in configuring SharePoint State Service Application using PowerShell

First, let us go to Central Administration Site to make sure that State Service Application is not available to be provisioned from the UI.

Go to “Central Administration” -> “Application Management” and click on “Manage Service Application”

2

On Service Application Page, Click “New” Menu in Ribbon Bar to see list of Service Applications that we provision from this UI and we can see State Service Application is missing from this list

That means we can provision this Service Application only using PowerShell and that what we are going to do in this article.

3

In order to provision the service application using PowerShell, launch “SharePoint 2016 Management Shell” and perform the following steps-

1

4

Step 1: Provision Service Application Instance

In this step we will provision the State Service using “New-SPStateServiceApplication” cmdlet as shown below

New-SPStateServiceApplication -Name "State Service Application"

5

Step 2: Provision Service Application Proxy

In this step we will provision Service Application Proxy for State Service by using the following command

Get-SPStateServiceApplication| New-SPStateServiceApplicationProxy –defaultproxygroup

6

Step 3: Provision Database for State Service Application

In this Step, we will provision database for State Service Application using the following command. We choose “State_Service_Database” as database name

Get-SPStateServiceApplication| New-SPStateServiceDatabase -Name "State_Service_Database"

7

Step 4: Install the State Database Schema

In this step we will install the database schema into State Service Database by using following command

Get-SPDatabase | where-object {$_.type -eq "Microsoft.Office.Server.Administration.StateDatabase"} | Initialize-SPStateServiceDatabase

8

Step 5: Verify Service Application

In this step we will verify the provisioning of service application by looking at “Service Application” Page in “Central Administration” as shown below-

9

Step 6: Verify Service Application Database

In this step we will verify the provisioning of service application database by logging into “SQL Server Management Studio” as shown below-

1011

This concludes the Service Application Provisioning Process using PowerShell.

Hope you find it helpful.

SharePoint 2016: How to configure Search Service Application using PowerShell

In this article we will discuss how to provision Search Service Application for SharePoint 2016 using PowerShell

During this whole article I will guide you steps involved in provisioning Search Application along with the corresponding PowerShell Script

Here the steps that we will explore in the upcoming section-

1

2

3

Though we can provision Search Application from Central Admin as well but the purpose here is to demonstrate the use of PowerShell commands required to provision Search Application so that’s what we will do.

First let’s go to Central Admin to ensure that there is no existing instance of Search Service Application has been provisioned earlier

Under Application Management -> Click on “Manage Service Applications”

4

Here we can see what all Service Application Instances provisioned earlier and we can see there are none

5

Now launch SharePoint Management Shell to run the required PowerShell commands

6

We will run the PowerShell commands in the following order to make sure each Sub component provisioned as desired.

Step 1: Provision Service Application Instance

In this Step we will first provision Search Service Application using the following cmdlet-

$sa = New-SPEnterpriseSearchServiceApplication -Name "Search Service Application" -DatabaseName "SearchDB" –ApplicationPool  "SecurityTokenServiceApplicationPool"

Make sure Application Pool should exists before you run this command else it will fail since this cmdlet wont’ add the Application Pool automatically

7

Step 2: Provision Application Proxy

Then we need to provision Application Proxy by using following cmdlet referring the Service Application Instance provisioned in the previous step

New-SPEnterpriseSearchServiceApplicationProxy -Name "Search Service Application Proxy" -SearchApplication $sa

8

Step 3: Validate Service Instance

Next step is to validate that the Service Instance is online and to do so we can use the following cmdlet

Get-SPEnterpriseSearchServiceInstance -local

9

Step 4: Clone Topology

Next step is to clone the topology which is required in order to make any changes to the search topology in a search installation that has items in the search index

As per Microsoft recommendation around this you need to modify this new topology object, which is a clone of the active topology, by adding or removing search components. After you have made the changes to the clone topology object, you have to activate this clone to have this topology in action.

$clone = $sa.ActiveTopology.Clone()

10

Step 5: Get Search Service Instance Server Name

This Server name is required in the upcoming steps to it is wise to make use of the following cmdlet to retrieve the server name

Get-SPEnterpriseSearchServiceInstance| Select Server

11

Step 6: Get Search Service Instance

Then we get handle over the search service instance running on the respective server using the following cmdlet

$si = Get-SPEnterpriseSearchServiceInstance | ?{$_.Server -match "SP-2016-Dev"}

This reference object will be used in the upcoming steps

12

Step 7: Provision Admin Component

Next step is to provision a new Admin Component for the given topology and search service instance using the following cmdlet. This cmdlet is using reference to search service instance we get in the earlier steps

New-SPEnterpriseSearchAdminComponent -SearchTopology $clone -SearchServiceInstance $si

13

Step 8: Provision Processing Component

Next step is to provision new content processing component for the given topology and search service instance using the following cmdlet

New-SPEnterpriseSearchContentProcessingComponent -SearchTopology $clone -SearchServiceInstance $si

14

Step 9: Provision Analytics Component

Then we have to provision new analytics processing component for the given topology and search service instance using the following cmdlet

New-SPEnterpriseSearchAnalyticsProcessingComponent -SearchTopology $clone -SearchServiceInstance $si

15

Step 10: Provision Crawl Component

Then we have to provision new crawl component for the given topology and search service instance using the following cmdlet

New-SPEnterpriseSearchCrawlComponent -SearchTopology $clone -SearchServiceInstance $si

16

Step 11: Provision Index Component

Then we have to provision new index component for the given topology and search service instance using the following cmdlet

New-SPEnterpriseSearchIndexComponent -SearchTopology $clone -SearchServiceInstance $si -IndexPartition 0 -RootDirectory C:\SearchIndex\

RootDirectory: Specifies the root directory that will hold the index location for the new search index component. If you plan to isolate the index on dedicated discs in order to avoid I/O contention that may leads to performance degradation as it might be a risk that index filling up the OS disk and ruin the overall server performance.

17

Step 12: Provision Query Component

And finally we have to provision new query processing component for the given topology and search service instance using the following cmdlet

New-SPEnterpriseSearchQueryProcessingComponent -SearchTopology $clone -SearchServiceInstance $si

18

Step 13: Activate New Topology

Once all the components has been added to the new topology, activate it by using following cmdlet

$clone.Activate()

19

Step 14: Clean Inactive Topologies

Finally we have to clean all the inactive topologies associated with a search service application. We can perform this clean by using the following the code using “Remove-SPEnterpriseSearchTopology” cmdlet

foreach($tp in (Get-SPEnterpriseSearchTopology -SearchApplication $sa | ?{$_.State -eq "Inactive"}))
{    
   Remove-SPEnterpriseSearchTopology -Identity $tp -Confirm:$false
}

20

Step 15: Change Default Content Access Account

And we can change the default content access account by using the following code

$sa = Get-SPEnterpriseSearchServiceApplication

$content = New-Object Microsoft.Office.Server.Search.Administration.Content($sa)

$content.SetDefaultGatheringAccount("<Enter User Name>", (ConvertTo-SecureString "<Enter Password>" -AsPlainText -Force))

21

Now it is time to validate if search service application has been provisioned correctly.

Go to Central Admin -> Manage Service Applications

We can see a new Search Application has been provisioned successfully by clicking the Search Service Application Link

22

And we can see all the component are provisioned and running as expected.

23

Also we can validate the SQL Databases that has been provisioned during creating new search service applications as shown in the following screenshots

242526

Hope you find it helpful.

SharePoint Developer Tools: How To Test & Debug SharePoint REST API Endpoints (POST Requests)

This is the second are article in the series of using Fiddler as Debugging & Testing Tool for SharePoint REST API EndPoints.

You can read the article on GET Request here:

SHAREPOINT DEVELOPER TOOLS: HOW TO TEST & DEBUG SHAREPOINT REST API ENDPOINTS (GET REQUESTS)

POST requests are different in nature than GET requests. They require more authentication layers to get through in order to push the data to SharePoint Lists and Libraries.

In order to run the POST request successfully we need an additional request header “X-RequestDigest” which is not but the User Authentication Token.

In order to request this token from SharePoint we need to make of “contextInfo” endpoint that will return the “FormDigestValue” containing the required user authentication token.

Now let see how we can request Authentication Token from SharePoint

Get Authorization Token

http://<Host Name>_api/contextinfo

1

2

Once we get the Authentication Token from SharePoint, we can add this token information in the Request Header of each of the POST requests

Request Headers

Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose
X-RequestDigest: 0xE1AE266A42214DA2940689826F68426D10620220CEDD3093CA2C234993E4ECA265BA57D357E8D3BD32F56660613CADBF72495F2C858B38F7C9B9C3CAD797F6D5,06 Feb 2017 01:22:08 -0000

Once we are ready with Request Headers we can start issuing POST Requests as shown below-

Add Data to List

Let’s consider we have a list called Categories as shown below-

3

First see the XML return based on querying schema for Categories List using following URL

http://<Host Name>/_api/Web/Lists/getByTitle('Categories')

4

Then we will see the XML return based on querying for Categories List Items using following URL

http://<Host Name>/_api/Web/Lists/getByTitle('Categories')/Items

5

Next step is to prepare the Request Body and we have to include following properties to add the items.

Please note that I am taking properties that are required for this list to add the category and add any desired number of properties to the Request Body as per the schema of the target list.

Request Body

"__metadata": { type: " SP.Data.CategoriesListItem" },
Title: "Category From Fiddler",
CategoryID: 9,
Description: “New Category Added from Fiddler”

6

Once we execute this request we can inspect the response to ensure that the request item has been added successfully to the Categories List.

7

Also we can validate this new item added by browsing Categories List

8

Update List Item

http://<Host Name>/_api/Web/Lists/getByTitle('Categories')/Items(9)

For update request you have to include “eTag” value that was returned with the item during the initial query to the Request Body. SharePoint uses this value to determine if there is any updates made to the item since it is last queried.

“If-Match: *” can be used to match any “eTag” value resulting in the operation being performed regardless of the actual value.

“X-Http-Method: PATCH” is to override the existing values

So the request body would be like this

IF-MATCH: *
X-Http-Method: PATCH
{
    "__metadata": {
    type: "SP.Data.CategoriesListItem"
},
Title: "Category From Fiddler - Updated",
Description: "New Category Added from Fiddler - Updated"
};

9

Once the request executed successfully we can see the item is updated in the Categories List

10

Delete List Item

http://<Host Name>/_api/Web/Lists/getByTitle('Categories')/Items(9)

Delete operation is more or less similar to Update operations.

11

In case of delete we will use of “X-Http-Method: DELETE” in the Request Body

Request Body

IF-MATCH: *
X-Http-Method: DELETE

12

Once the request executed successfully we can validate the item is deleted from the list.

13

Add New List

http://<Host Name>/_api/Web/Lists

Adding a new SharePoint List involve a little bit more of configuration information in Request body apart from request headers

Request Headers

Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose

Request Body

Content-Length: 0
{
"__metadata": { type: "SP.List" },
"AllowContentTypes":true,
"ContentTypesEnabled":true,
"Description":"This is Task List Created using Fiddler",
"BaseTemplate": 107,
"Title": "Task List By Fiddler"
}

14

Once this request has been executed successfully we can see the Response Body holding information about newly added SharePoint List

15

Also we can see this new list added to SharePoint by browsing the respective site

16

17

Also we can verify the “AllowContentTypes” & “ContentTypesEnabled” properties are configured as expected by browsing the Advanced Properties of the new List as shown below-

18

Delete List

http://<Host Name>/_api/Web/Lists/getByTitle('Task%20List%20By%20Fiddler')

Deleting a list is rather simpler than adding it. It takes “X-Http-Method: DELETE” to be added to the request header and rest will be done for you.

 Request Headers

Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose
Content-Length: 0
IF-MATCH: *
X-Http-Method: DELETE

19

Once the request has been completed, it will delete the required list from SharePoint Lists Collection.

20

Hope you find it helpful.