Thursday, January 10, 2019

PowerShell: Remove Items & Document Sets from Library

Here are the basics of the script:
- Get your web
- Get your list, you can also use a foreach and iterate through every list
- From the list get your root folder
- The root folder has multiple sub folders, which are the document sets
- Iterate through each document set / sub folder and delete the items
- Delete the sub folder
- Move to the next sub folder
- Important to note: I excluded the "Forms" folder, because you can't delete that
I just threw this together in a couple of minutes, so expect errors. I'm also redoing the whole "foreach" thing, because I'm changing the collection with every delete.

Wednesday, January 2, 2019

PowerShell: Create a Password file

In my Azure Functions posts, I'm using a Password file which contains the password a service account. So here's how I'm creating the files needed:

Azure Functions, PowerShell and how to add users from a SharePoint List

In posts before (see here and here) I explained how to create a HTTPTrigger PowerShell Azure Function. How can we use that now? Well, one thing could be to create users in Azure AD from a SharePoint list. We need to create a SharePoint list with a couple of fields and also create the Azure Function for that. And here's how we will be doing this.

You can either click in the UI until you have a list with these fields:
  • Internalname: Type
  • UPN: Text
  • UserManager: User
  • Department: Choice
  • GivenName: Text
  • SurName: Text
  • Jobtitle: Choice
  • UsageLocation: Choice
  • License: Choice
  • MailAddress: Text

OR! We can use SharePoint PnP PowerShell and do this quicker:

Now that we have a list, we can put in some entries. You don't need to do that now, we'll have to enter some people later on. We will create create a Flow that can trigger our Azure Function:

Click on "Platform features" and select "API definition"

On the next screen click on "Generate API definition template":

Azure will create the definition, as can be seen here:

Click on "Save" and then on "Export to PowerApps + Flow":

Configure the custom API:
After that, switch over to Flow, create a new template and search for your API:

After we did this, we can go back to the good stuff: PowerShell. Our header of the function will look like this:

The Flow will provide us with an ItemID ($itemID), a URL($url) and the list title (listTitle). We will need them to get the user from the list and pull information from the list.
In this next part we will define a lot of things:
  • $FunctionName: This needs to be name you were giving the function, in this example it will be "AddAzureADUser"
  • Define modules: We need three modules (SharePointPnPPowerShellOnline, AzureAD, MSOnline), change the version numbers to your versions
  • Define username and password: in this example they are stored as environmental variables in the Azure Function
  • Importing PowerShell modules: the modules are stored in "bin", see this blog post
  • Build credentials: from the stored information, we're creating the credential object
  • Tenant ID: You will have to provide your tenant ID
  • Conect via MSOL Service
  • Connect via PnP Online
  • Connect via Azure AD

During this part, we are creating password profile so that the user has to change the password on the first login.
If there is a MailAdress definied, we will split that and create a MailNickName.
And lastly we are creating the user.

If the user has an e-Mail address in the user list, we are storing the e-Mail address in the Azure AD and the SharePoint User Profile Service
With the created user, we can also set some information for that person.
In this part we are changing the department, the given name, the surname, the jobtitle and the usage location. You have to set the usage location! Also we are doing this for Azure AD and the SharePoint User Profile Service

Finally we are setting the license! It is important that you replace the "TENANTNAME:" with your own tenant name.

Here are the final scripts:

Wednesday, December 5, 2018

SharePoint: Get all Site Collections, Lists, Libraries, Folders and Items

Back in 2015 I wrote a script to get all document libraries from all site collections of a web application. I went back to the script to update it a bit. The updates version gets all lists & libraries, searches for all folders and items and gives you a count of folders, items an the size of the site collection.

Wednesday, November 28, 2018

Azure Functions, FTP and PowerShell Modules

I already showed you how to get a PowerShell in an Azure Functions off the ground. How about we actually use it with some modules that are not included in Azure Functions? This post will explain how to get FTP going and upload a module and how to import that module into your script.

We start at the "Overview" of our Azure Function, it's the same one I created a couple of weeks ago for another post. It looks like this:

Click on "Platform features" and there select "Deployment Center":

This is also new in Azure Functions, it was updated along side the Azure Functions v2. But don't worry, FTP is still here! You might have to scroll on the next screen, because it's at the bottom of the list. At least in my case.

Click in "FTP" and then select "Dashboard" right below it.

You know have to two options: "App Credentials" or "User Credentials". With "User Credentials" you can create an Azure Functions FTP user, so you can have one FTP account for all your apps. Of course we don't do that. I'm using "App Credentials", because I rarely log into my App with FTPS.The credentials are automatically created and can be reset by a push of a button. You can also see the FTPS endpoint above the credentials.

But you can also check under "Platform features". Click on "Properties" to get a list of information you might need. In our case we only need the "FTP/Deployment User" and the "FTP Host Name":

Now we just connect via Windows Explorer to the FTPS Server. You will see a couple of messages to provide the user and the password.

If you succeeded, you will see a screen like this:

Because I was super smart when creating this test function, I decided not to give it a proper name but instead stick with the default name. The default name is "HTTPTriggerPowerShell1". Open that folder to see "run.ps1" and "function.json". We will place our module in here, somewhere.

You know what? We forgot to get our PowerShell modules! Welp, we have to do that now, I guess. Open your PowerShell window and enter this command: Save-Module msonline -Repository PSGallery -Path "C:\temp". I will store the "MSOnline" module that you have installed on your computer in "C:\temp\MSOnline\VERSIONNUMBER". Like this:

I have multiple version, because over time you have to update your modules in Azure Functions. So I'm collection them by now. Go back to the FTPS in your Windows Explorer and create a new folder called "bin". You can name it whatever you want, "bin" is just an example. I you give the folder a different name, you will have to change the script later on.

Upload the "MSOnline" folder, so it looks like this:

Finally we can import the module in our Azure Function:

And here's the script. You might have to change the folder name "bin", depending on your setting. Also after each change to the module "MSOnline", f. ex. using a newer version, you will have to change the version number in the script. In the example it's called "$MSOLVersion". Also: The function name! Never keep the default names, you will get crazy trying to fix issues. It's fine if you are just testing out a system, but please change the names with descriptive names!