Tag: PowerShell

Automate tasks with use of XenServer Powershell Module

Working with backups of your virtual machines is obviously essential. Working with exports in XenServer can some times be time consuming, particularly with bigger virtual disks attached to your virtual machine. In this scenario I will show you an alternative to manually export via XenCenter, by doing it with Powershell to an remote server using XenServer Powershell module.

Use PowerShell & Windows Update to force drivers to be downloaded from the Internet in a Task Sequence

Working with client driver packages for me is related to a never-ending story. Drivers are frequently being updated and results in manually handling updates of Driver Packages in Configuration Manager. But since some computer manufacturers are releasing updates through Windows Update, so we thought; What if you can use a Task Sequence to force Windows Update to look for updates and drivers over the Internet instead of using manually handled Driver Packages? So I decided to try with a Surface Book.

With help from the PowerShell Module PSWindowsUpdate, created by Michal Gajda (downloaded from TechNet), and with a post from Waingrositblog, I had all the necessary bits forcing a Surface Book to download drivers from Windows Update, over the Internet, while running a Task Sequence. I started by modifying the steps, created by Waingrositblog, in the Task Sequence steps a bit. I found having one step running a PowerShell script instead of three steps, two of which was running cmd lines, more suitable.

This image illustrates the Task Sequence step.

I added the update step just after applying Windows- and Network Settings, where we usually apply driver packages.

The RPS (Run PowerShell) – Microsoft Update step is running the following script:

To verify the success of the script I went through the WindowsUpdateLog.Log and found that during the Task Sequence, a lot of drivers were installed. Here I would like to use PCI drivers as an example. As shown in the image below, the WindowsUpdateLog successfully downloaded and applied the drivers.

This is the WindowsUpdateLog.Log generated after successfully running the Update Drivers sequence.

I also tried the running the Task Sequence without the Windows Update / Driver script and found out the device had conflicts with the PCI drivers. These drivers is just used as an example in this process, there are several conflicts and other drivers missing as shown in the image below.

This image illustrates conflicts with, among others, the PCI drivers after not running the Update Drivers sequence.

This image illustrates when the drivers are applied.

As shown in these images, the Install Driver step running in the Task Sequence finds the correct and necessary drivers. After a Task Sequence successfully has gone through no exclamation marks are found in the Device Manager.

Some computer manufacturers are using Windows Update as a secondary source for updates, and because of this some drivers can be out of date. This is a reason why the Surface is a great example of using Windows Update for drivers since Microsoft release their updates, up to date.

If you have any questions, opinions or improvements, feel free to email me at Johan.Nilsson@Xenit.se

Sending CSS formatted tables in Outlook

If you’ve ever used Powershell to send HTML tables in Outlook containing CSS you’ve probably been disappointed of the outcome.
There is some archived documentation for Outlook 2007 that is still viable for Outlook 365 (https://msdn.microsoft.com/en-us/library/aa338201(v=office.12).aspx).

Basically the function accepts a csv and css file, hardcodes the css into the table and outputs a formatted HTML table that is compatible with Outlook.

Example table sent using the function and send-mailmessage
The css has odd/even for readability, bolded column 1/4 and red text for column 3.
This is by default impossible to achieve using just css in outlook.


HTML output


Since the CSS does not work perfectly the style.css file imported needs some specific configuration..

  • classes has some specific name structure”
    • columns are named .coln
      • n is the number of the column starting with 1 to infinity. .col1 .col2 and so on
    • one whitespace is required between class name and the curlybrackets.
      • Curlybrackets must be on the same row as class name
      • Ending curlybrackets must be on a separate line
    • Data must be on separate rows
  • Odd/even css is the only tr handled code.
    • Must be named exactly
      • tbody tr:nth-child(odd) {
      • tbody tr:nth-child(even) {

Example style.CSS



Azure Automation – Running scripts locally on VM through runbooks

I was tasked to create a powershell script to run on a schedule on a Azure VM. Normally this would be running as a scheduled task on the VM but seeing as we’re working with AzureVM and schedule tasks are legacy I wanted to explore the possibilities of running the schedule and script in Azure to keep the VM clean and the configuration scalable.

After some research the best option would be running the powershell script as a CustomScriptExtension on the VM, and the schedule would be handled by a Process Automation Runbook (using Automation Accounts).

What I ended up with is the script below. It’s fairly easy to configure and contains almost all the required configuration in the parameters.

Recursively search Azure Ad group members

When working with on-premise Active Directory an administrator often has to recursively search AD groups, this is easy using the ActiveDirectory module with cmdlet ”Get-AdGroupMember <Group> -Recusive”.
For the AzureAD equivalent this is no longer an option, the cmdlet Get-AzureADGroupMember has three parameters.

-All <Boolean>
If true, return all group members. If false, return the number of objects specified by the Top parameter
-ObjectId <String>
Specifies the ID of a group in Azure AD.
-Top <Int32>
Specifies the maximum number of records to return.

Just enough Administration & RDS

The Problem?

Microsoft RDS has limitations when delegating access, in fact there is no built-in access delegation whatsoever!

The solution? Powershell!

A Just enough Administration (JEA) endpoint, also known as a Constrained Powershell Endpoint.

I’ve created a powershell app to list and logoff users I also created a simple tool in powershell to connect to connection brokers and search users. All without delegating users access to any RDS servers.

  1. Connection broker/Endpoint connection
  2. Collection field, used to search and filter. Multiple collections can be selected at once
  3. Wildcard search field, can be blank (list all users)
  4. User information & selection are
  5. Press to logoff all selected users

How does this work? Constrained Powershell Endpoint.

The endpoint is restricted to only access List collection, list users and subsequently force logoff users. The endpoint configuration to only accept users from a certain AD group.

To configure endpoint, run the following code on all connection brokers. The script is somewhat generalized for easy adaptability. The main parts are in the parameters where we configure who can run, using which account and also what cmdlet are available in the constrained session.

The frontend application code is not posted in the blog.

Convert SCCM schedule to readable format

Reading SCCM Maintenance Windows is not an easy thing to do from WMI as they are stored as 16 char hex values

To get the schedule hex values simply run the below line, but the result might not be easy to understand

A server might return the following values.

Now, how do we read this? The easiest thing is to run Convert-CMSchedule but that unfortunately only works if the configuration manager console is installed.
Another way to do is to invoke a wmi method from a site server using the SMS_ScheduleMethods class.

None of these are portable, so I created my own function to decode these. The below function returns the same data as Convert-CMSchedule but is free from any module as it converts the hex to a readable format.

The function also accepts valuefrompipeline


The script is reverse engineered from https://msdn.microsoft.com/en-us/library/cc143505.aspx

SCCM source cleanup inventory

As applications are retired in SCCM the source tend to build up due to human error or retention policy. Either way admins should perform cleanup of the source directory from time to time.
The below script lists all files and folders in the source directory that does not have any active connection in SCCM.

To run the script first open Configuration Manager Console and press ”Connect via Windows Powershell”


Skapa VM med flera NIC i Azure ARM

Jag har suttit med ett antal virtual appliances från exempelvis Citrix (NetScaler), Cisco (CSR) och Palo Alto Networks (VM-series) och började fundera kring om jag kan ersätta exempelvis CSR med en billigare variant – exempelvis CentOS – som kan ge högre bandbredd utan några krav på licenser och liknande.

Efter att ha suttit i portalen och skapat VM fram och tillbaka kom jag fram till att även om exempelvis DS2_v2 har stöd för flera NIC så kan man lägga till dem där, så satte mig och gjorde det via powershell.

Värt att notera kring ovan är:

  • Jag hade redan skapat min resource group, virtual network, subnets och storage
  • Eftersom jag kör premium storage skapas en standard storage för diagnostics med ovan, vilket jag inte hade
  • Public IP kan endast bindas mot det NIC som är märkt som primary

Vill du byta primary NIC är det möjligt, jag gjorde det med tog för lång tid att slå igenom så valde att skapa om VM:et istället:


Hämta Office 365 IP-adresser via PowerShell

I vissa Office 365 uppsättningar, främst Hybrid behöver man tillåta att Office 365’s tjänster får kommunicera emot sina interna resurser.

Exempelvis behöver man i Exchange Hybrid uppsättningar tillåta IP-adresserna som Exchange Online använder sig utav i en Receive Connector lokalt för att få skicka mail inom samma organisation mellan Office 365 och on-premise. För detta har Microsoft ett inbyggt kommando Get-HybridMailflowDatacenterIPs i PowerShell-modulen för Exchange Online som hämtar ut nödvändiga IP-adresser.

För att automatisera processen med Get-HybridMailflowDatacenterIPs och uppdatera det dynamiskt finns det en bloggpost från Microsoft här som går igenom stegen:
Exchange Online: Keeping your ‘Inbound From Office 365’ Receive Connector Current with Microsoft Public IP’s

Det kan däremot finnas andra tillfällen när man vill ha ut samtliga IP-adresser som Office 365 använder sig utav, t.ex om man har ett eget antispam-system eller låsa ner i en brandvägg vilka IP-adresser som får kommunicera med sina interna resurser.

Dessa IP-adresser har Microsoft släppt i en publik lista där samtliga URL:er och IP-adresser som Office 365 och respektive tjänst använder sig utav, Office 365 URLs and IP address ranges som också finns i XML-format, O365IPAddresses.xml

Då adresserna finns tillgänlgigt i XML-format kan man via PowerShell läsa ut dessa och få samtliga adresser i en variabel som kan användas för att automatiskt och dynamiskt uppdatera sina regler. I exemplet nedan läser jag ut samtliga IPv4-adresser som Exchange Online Protection använder sig utav:

Resultatet blir:

$address kan jag sedan använda mig för att göra olika API-anropp till min brandvägg eller tredjepartstjänst och dynamiskt uppdatera mitt regelverk.