Scripting

Office 365 : Reclaiming licenses

Migrated to Office 365? Running low on licenses and want to do a review of existing license assignments?

In work, we recently had to do a reclamation of licenses from users who had left the company but had somehow managed to retain their licensing status in Office 365.

To help with this I created a powershell script that would get a list of disabled Active Directory accounts who had a UPN (UserPrincipalName) that was formatted for use in Office 365 and cross reference that with the list of licensed users in Microsoft Online Services. Surprisingly Microsoft haven't included something like this in the existing views. I'm sure it'll be added in an upgrade...

Behold the results! Make sure you change the UPN value being queried below as I'd be surprised if ye're using DOMAIN.COM! All comments and suggestions on improvements are welcome. I'm relatively new to Powershell.

Import-Module ActiveDirectory
Import-Module msonline

Connect-MsolService -Credential (Get-Credential)

#$ErrorActionPreference = $SilentlyContinue

# Create counter and results array
[int]$i = 0
$results = @()

# Query AD for all accounts with UPN needed for Office365 that are disabled and save to array
$disabledUsers = Get-ADUser -Filter ({UserPrincipalName -like "*@DOMAIN.COM" -AND enabled -eq $false}) | select UserPrincipalName

# For all entries in $disabledUsers, query their licensing status on MS Online and save to $results
for ($i=0; $i -le ($disabledUsers.count-1); $i++) {
	$results += get-msoluser -UserPrincipalName $disabledUsers[$i].UserPrincipalName -ErrorAction SilentlyContinue
}
# For all $results where isLicensed is TRUE, output the DisplayName and License status
for ($i=0; $i -le ($results.count-1); $i++){
	if ($results[$i].isLicensed -eq "True"){
		$results[$i] | select DisplayName, IsLicensed 
	}
}

Clean out old items from your Downloads folder using find & crontab

I got a bit lazy when it came to cleaning out my downloads folder. Old .DMG files, installer files and other detritus were bloating my Download folder so that the stack view in my Dock was becoming a pain to use.

I decided to adopt a bash script we run in the office at home to aid me in my quest for a tidy Downloads folder! By leveraging the find command in bash, a user can create a script that can find any files in a directory that match certain criteria, e.g. type, file name and date created/modified.

In my case, once found, the files will be deleted. Instead of using -mtime to determine age, you can also use -atime or -ctime. I'll leave it up to yourselves to read about that though! For a downloads folder and maybe for different file types,  -atime may be more appropriate for some of you. However, since I'm dealing with installer files, -mtime will suffice!

Once the script has been created, apply to your user crontab at whatever interval you so desire!

#!/bin/bash
#
#       Delete files older than 21 days in Downloads.
#
echo "Deleting (M)PKG, DMG and ZIP files that are older than 21 days in Downloads."
 
# Delete all (M)PKG's older than 21 days
find ~/Downloads/ -name "*.pkg" -type f -mtime +21 -delete
find ~/Downloads/ -name "*.mpkg" -type f -mtime +21 -delete
 
# Delete all DMG's older than 21 days
find ~/Downloads/ -name "*.dmg" -type f -mtime +21 -delete
 
# Delete all ZIP's older than 21 days
find ~/Downloads/ -name "*.zip" -type f -mtime +21 -delete

Ruby Script: Remaining Mobile BB Data Allowance

I recently changed to Three.ie as my mobile broadband provider. They provide a nice handy way of checking remaining monthly data allowance via their "my3.three.ie" service. Signing in is handled automatically on their side based on the network you're using. Handy!

Since I've been messing around with Ruby a bit I figured I'd try and make a ruby script that would check my remaining data for me! My efforts are below!

Please comment on any improvements that you think I can make. Especially relating to the instance vs. class variables!

I use this script in combination with a bash script to display data on my desktop using GeekTool. The bash script attempts to ping the mobile gateway's IP address. If it is available, the ruby script is executed. Simples!

 

 

#!/bin/bash
#
#	Pings once to see if my Huawei B260a mobile gateway is reachable
#	if yes, execute Ruby data allowance checker
#	else, exit
#
ping -c 1 -t 2 192.168.4.1 >/dev/null
if [[ $? == 0 ]]; then
	ruby ~/Programming/Ruby/MyStuff/threeMobileBroadband.rb
else
	exit
fi

 

 

#!/usr/bin/env ruby
#
require 'rubygems'
require 'mechanize'

class ThreeMobileBroadband

@@baseURL = "https://my3.three.ie"
  
def login
  @agent = Mechanize.new

  # First Single Sign On page
  @landing1 = @agent.get 'https://my3.three.ie/myaccount/postPayFreeUnits.do'
  @landing2 = @agent.get @landing1.links[0].uri
  # Then click the continue button presented to get to account options section
  @@usagePage = @agent.post("#{@@baseURL}#{@landing2.forms[0].action}")
end

def getRemainingData
  # Parse for remaining data table
  @usageValue =  @@usagePage.parser.xpath('//div/div/div/div/table/tbody/tr/td').to_s
  # Get the data, and sub out HTML, then sub out whitespace, then split into array at a close bracket
  @data = @usageValue.gsub(/<\/?[^>]*>/, "").gsub(/\s+/, "").split(/\)/)
  return @data[1]#.inspect
end

def remainingDataAsOf
  # Parse for caption containing the date & time the remaining data was checked
  @asOfDate = @@usagePage.parser.xpath('//div/div/div/caption/span').to_s.gsub(/<\/?[^>]*>/, "").gsub(/\*\s+/, "")
  return @asOfDate
end

end

three = ThreeMobileBroadband.new
three.login

puts "3 Mobile Broadband Pro"
puts "-----------"
puts "#{three.getRemainingData} MB remaining\n"
print "#{three.remainingDataAsOf}\n"
puts "-----------"

OpsView/Nagios check for Blacknight.ie Status Page

As part of my placement, we monitor websites hosted by Blacknight.ie.

If Blacknight are having issues we tend to get alerts messages about "Connection Refused" which tend to cause some consternation!

To cut down on the amount of clicking, I've wrote a small bash script that can be run by OpsView/Nagios that will check the Blacknight Status Page and return a count of the number of times that the "NOTOK" image is shown!

 

If that number is more than one, Blacknight's servers are reporting problems!

#!/bin/bash
#
#       Nagios Check Plugin for Blacknight.ie
#       Displays message if Blacknight are having technical issues with any of their servers
#
NOTOK=`curl --silent "http://www.blacknightstatus.com/server_monitor.php" | grep -E "http://blacknightstatus.com/statusmon/images/notok.gif" | wc -l`

if [ "$NOTOK" -eq "0" ] ; then
{
echo "Plugin failure: Information not found, log incident with N&S";
exit 1;
}
elif [ "$NOTOK" -eq "1" ] ; then
{
echo "All OK";
exit 0;
}
elif [ "$NOTOK"  -ge "2" ] ; then
{
echo "Blacknight Status Page reporting issues";
exit 2;
}
else
{
echo "Plugin failure: Unexpected output!";
exit 2;
}
fi