Quick Full-Disk Backups with Disk2VHD

Microsoft Sysinternals have free utility available to make full VHD backups from your hard drive. Using a VHD backup you can mount it in Windows and also import it into virtualisation software such as VirtualBox or VMWare and Hyper-V.

You can download the software from here:http://technet.microsoft.com/en-gb/sysinternals/ee656415.aspx

Below is a simple batch script to create automated backups using the command line; which can be set on a schedule.

Save the following as VHD.bat. Edit the backup_path variable with the location to store the backup. By default DISK2VHD will select all drives on the machine.

@ECHO OFF
ECHO Starting DISK2VHD Backup
SET starttime=%time%
SET backup_path=\\share\backups\
ECHO Backup started at: %starttime% > log.txt
IF NOT EXIST %backup_path%\%computername% MKDIR %backup_path%\%computername%
disk2vhd.exe * %backup_path%\%computername%\%computername%.vhd -accepteula
SET endtime=%time%
ECHO.
ECHO Backup completed at: %endtime% > log.txt
PAUSE
Posted in Programming | Tagged , , , | Leave a comment

VBScript – Check User is a member of AD Group

Just a quick snippet of VBScript – To check whether or not the current user is a member of a specific Active Directory group. This could be useful when deploying scripts via GPO or using it in a netlogon script.

The example below checks to see if the user belongs to “Domain Admins” – Replace this with whatever group you would like to check.

Option Explicit
Dim objShell,grouplistD,ADSPath,userPath,listGroup
On Error Resume Next
set objShell = WScript.CreateObject( "WScript.Shell" )
  
If isMember("Domain Admins") Then
       MsgBox("Is member")
End If
  
Function IsMember(groupName)
    If IsEmpty(groupListD) then
        Set groupListD = CreateObject("Scripting.Dictionary")
        groupListD.CompareMode = 1
        ADSPath = EnvString("userdomain") & "/" & EnvString("username")
        Set userPath = GetObject("WinNT://" & ADSPath & ",user")
        For Each listGroup in userPath.Groups
            groupListD.Add listGroup.Name, "-"
        Next
    End if
    IsMember = CBool(groupListD.Exists(groupName))
End Function
  
Function EnvString(variable)
    variable = "%" & variable & "%"
    EnvString = objShell.ExpandEnvironmentStrings(variable)
End Function

Set objShell = Nothing
Posted in Programming, VBScript | Tagged , , , , | Leave a comment

Powershell – List and Assign Office 365 Licenses

Microsoft are adding more and more functionality for administrators in Office 365 – Unfortunately, there is still no ‘easy’ and quick way to assign a new license, in bulk, to a lot of existing users. Specifically, the OFFICESUBSCRIPTION_STUDENT that was introduced to provide Office 365 licenses to Students was the one I was looking for.

Below are some quick Powershell commands to get you started (assuming that you know how to connect to Office 365 Exchange Online via Powershell)

List SKUs / Licenses Available
This will list your organisation before the SKU name, e.g. YOUR_ORG:OFFICESUBSCRIPTION_STUDENT

Get-MsolAccountSku

Add Single License to User
You can pipe this | with Get-MsolUser to set the license on a single user.

Set-MsolUserLicense -AddLicenses YOUR_ORG:OFFICESUBSCRIPTION_STUDENT

Bulk Add by Domain Name
If you have multiple domains, or sub-domains you can assign licenses in bulk easily. Replace the ‘your org’ strings below to match your setup though.

Get-MsolUser -DomainName staff.yourorg.com -All | Set-MsolUserLicense -AddLicenses YOUR_ORG:OFFICESUBSCRIPTION_STUDENT

You can use multiple filters on the last command and just pipe the second command using the |. This could be by name, by group or domain as show above.

Posted in Powershell | Tagged , , | Leave a comment

HVBackup – FREE Hyper-V Backup Solution (Modifications)

There is a free Microsoft Hyper-V backup solution called HVBackup – Website Link ; this is open-sourced under the LGPL license and is free to download and modify to suit your needs.

You can download and use this on your Hyper-V servers now by using the link above. I have been making changes to the HVBackup source to add a few new features, mainly…

  • Added GZIP Support (added by Pablo, on the HVBackup Forums)
  • Added GZIP Password (added by Pablo, on the HVBackup Forums)
  • HVBackup no longer responds to Command Line parameters, instead it reads all information from Settings.ini located in the same folder.
  • A GUI has been created to configure the settings, add servers, change formats, zip type, compression etc.
  • A Scheduler has been added to the GUI which allows you to Schedule HVBackup’s via the Windows Task Scheduler.

The new GUI I’ve written has been made in VB.NET and it manipulates the .ini settings file, allowing you to query your Hyper-V instance for server names and providing a quick interface for creating scheduling options via the Windows Task Scheduler.

This will be available for download shortly, if anyone wants to try this out feel free to contact me and I’ll send it over.

A couple of screenshots of the new GUI are below:

HBC2     HBC1

 

Posted in Programming | Tagged , , , , , | Leave a comment

Powershell – Update Active Directory Users Profile Data

Here is a small snippet to bulk update an OU with user profile data. Recently used to fill in default values for AD.

Change the OU and Domain details to suit; Requires full LDAP path (e.g. YOURDOMAIN.LOCAL would be DC=YOURDOM,DC=LOCAL)

Import-Module ActiveDirectory 
$Users = Get-ADUser -Filter * -SearchBase "OU=Users,DC=YOURDOM,DC=LOCAL"

foreach($User in $Users) {
    # Update properties.
    $User.postalAddress = "Address Line 1"
	$User.street = "Address Line 2"
	$User.telephoneNumber = "00000 00000000"
	$User.department = "Your Department"
	$User.wWWHomePage = "www.website.com"
	$User.postOfficeBox = "England"
	$User.city = "City"
	$User.state = "County"
	$User.postalCode = "Post Code"
	
    Set-ADUser -Instance $User
}

Always be careful bulk updating users via script. More profile values can be found in the Advanced Attribute Editor in the Active Directory Snap-In.

Posted in Powershell | Tagged , , , | Leave a comment

Powershell – Export All Email Addresses from Office 365 Exchange Online

See below to export all E-Mail addresses from Office 365 Exchange Online. If you have never connected to Powershell before then you need to download the Office 365 Powershell Cmdlets from Microsoft.

Connecting to Powershell is easy: Installing Windows PowerShell cmdlets only sets the stage.

This does nothing for actually connecting them to the Microsoft Office 365 cloud. Connecting to the cloud requires a cumbersome—but straightforward—five-step process:

1. Import your newly installed Online Services module
2. Enter Office 365 credentials
3. Create a remote session
4. Import that session’s commands into your local Windows PowerShell session
5. Connect to Office 365 services After installing the Cmdlets, open up Powershell and type the following:

$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection

This will prompt for a login username and password; use your administrator login for Office 365. You next need to import the session:

Import-PSSession $session

This should connect you to Powershell. If you cannot execute commands you need to set the execution policy to Remote-Signed, as below:

Set-ExecutionPolicy RemoteSigned

After this is done you’ll be able to run your command to export data. Running the following snippet will export ALL email addresses in your organization to a CSV file called addresses.csv.

Get-Recipient -ResultSize Unlimited | select DisplayName,RecipientType,EmailAddresses | Export-Csv addresses.csv

Have fun.

Posted in Blog, Powershell | Leave a comment

PHP – Read CSV Data into PHP Array

First post for a while! The website will be updated more regularly now though. Here is a quick PHP snippet that will read CSV data from a file specified and return a Multidimensional Array with keys based on column names.

	private function __readCSV($fileName) {
			$file = fopen($fileName, 'r'); 
			$fields = array();
			$CSVdata = array();
			
			if ($file) {
				while (($data = fgetcsv($file)) !== false) {
					if(empty($fields)) {
						$fields = $data;
						continue;
					}

					$row = array_combine($fields, $data);
					$CSVdata[] = $row;
				}
				fclose($file);
				unset($file, $row, $data, $fields);
				
				return $CSVdata;
			}
		}

Just call the function with the full path to the file. Any questions then leave a comment below. Enjoy.

Posted in PHP | Tagged , , , | Leave a comment

PHP – Custom MVC Framework with Smarty3 and ActiveRecords

After using a few different PHP MVC’s I decided to work on my own framework when I had some spare time. Over the past few days I’ve put together a lightweight and feature rich small PHP MVC Framework. Note that it should currently be considered beta software, It should be useful for anyone wanting to develop their own MVC framework, feel free to use this as a base for your own framework or projects.

Continue reading

Posted in PHP, PHP frameworks | Tagged , , | 4 Comments