For most people you aren’t an admin for your office 365 site. As a Non admin for this site I needed a way to list – list items. I started working on a set of scripts based on a post I found on GitHub gist. Since then I discovered that there is a set of modules written for just that purpose. In this article I’ll demonstrate some of the things I was able to do with the module.
I have a list that I designed in SharePoint to list my application names:
These two lists work together to produce a list where I have many servers associated to an application or applications.
Rule for each list is there can only be one server and one application but there can be a many to each. Now I want to use this list to allow for using it with my automation for use to educate my users etc. Kind of like the Poor mans Configuration management database.
Here is the module (PnP-PowerShell) I chose to use since it had many more features than I could write.
To start with I had to figure out how to create the proper credentials object to connect to my corporate instance. This cmdlet Connect-SPOnline requires a Credentials object.
Connect-SPOnline
SYNOPSIS
Connects to a SharePoint site and creates an in-memory context
SYNTAX
Connect-SPOnline [-Credentials <CredentialPipeBind>] [-CurrentCredentials [<SwitchParameter>]] [-UseAdfs [<SwitchParameter>]] -Url <String> [-MinimalHealthScore <Int32>] [-RetryCount <Int32>] [-RetryWait <Int32>] [-RequestTimeout <Int32>]
I found that the credentials I use for my connection to our office online tenant was federated so I just simply connected with the credentials I use to connect to my corporate network.
$credsFile = 'c:\cred.txt'
$cred = Get-Content $credsFile | ConvertTo-SecureString
$spCredentials2 = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'Myusername@mydomain.com', $cred
With this small bit of code that I normally have in my profile. The Connect-SPOnline requires the credential object I passed the $spCredentials2 to it, and Walla I’m connected to my sharepoint site.
import-module OfficeDevPnP.PowerShell.V16.Commands
Connect-SPOnline –Url 'https://my.sharepoint.com/departments/configmgmt' -Credentials $sp2credentials
Now that I have the connection in memory now I can use PowerShell to query office 365 sharepoint. The first cmdlet I’ll call is Get-SPOList
As you can see my applications list and servers list is listed under this site. Now all I need to do is find out what is in that return set and if it has what I’m looking for:
I found that there are 78 different property items I can look at for each site. After searching each one of the 78 property items I didnt’ find the one if I’m looking for that gives me the items in a list so then I tried this cmdlet Get-SPOListItem:
The arrows show the data that I’m interested in. The property field values returns a hashtable. So to go from that hashtable into an object here is what I did:
$serverList = (Get-SPOListItem -List 'servers' ).fieldvalues
$serverListObj = @()
foreach($i in $serverList)
{
$slist = new-object psobject -Property $i
$serverlistobj += $slist
}
Looped through each item in the serverlist and added it to my serverlist object. Now I can select and get the results I’m looking for:
Now that I have that list from sharepoint in a Variable I can use it to create a CSV file or input to another object for creating an RDP file or a myriad of other uses.
$myServers = $serverListObj | Where-Object{($_.application.lookupvalue -like 'myApp*')} | Select-Object title, environment
2 thoughts on “Using OfficeDevPnP.Powershell Module”