SCCM, SQL and PowerShell

December 27, 2013 – 9:16 pm

I also got involved with pretty much every aspect of the new environment (it’s a consolidation project), with Active Directory, SQL Server, SCCM 2012, etc…

Having last worked with SMS 2003 R2, I decided to start playing with SCCM 2012. Foundations are the same as SMS so far, so nothing too complicated (other than the whole management console itself has changed 🙂 ). But being in a scripting mood, I decided to automate the whole installation process.

Start from a new VM, with 4 virtual disks (C:, D:, E:, and … F: )

  • C: = OS Only
  • D: = Applications
  • E: = Data
  • F: = Backups (SQL Exports)

Vmware_4Disks

Which means I have to rename the virtual CD drive to something else (my standard is R: for … Reader…).

So my initial build looks like this:

  • Change the Optical Drive letter to R:
  • Initialize all the virtual disks
  • Set all the volume labels

And while at it, clear all the event logs (I like starting with clean logs before installing new apps. Easier to spot the issues…)

Here’s the code for this script, which I call “Prep-New-Server.ps1” (Again, I didn’t reinvent the wheel, just learning and having fun with Powershell)

#################################################
# Function: ChangeDriveLabel                    #
# Params:                                       #
# - Drive Letter                                #
# - New Drive Label                             #
#################################################

Function ChangeDriveLabel([string]$DriveLetter, [string]$NewLabel)
{
If ((New-Object System.IO.DriveInfo($driveletter)).DriveType -ne 'NoRootDirectory')
    {
    $drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = '$DriveLetter'"
    Set-WmiInstance -inputobject $drive -Arguments @{DriveLetter="$DriveLetter"; Label="$NewLabel"}
    }
}

#################################################
# Function: ChangeOpticalDriveLetter            #
# Params:                                       #
# - Drive Letter                                #
#################################################

Function ChangeOpticalDriveLetter([string]$DriveLetter)
{
    (gwmi Win32_cdromdrive).drive | %{$a = mountvol $_ /l;mountvol $_ /d;$a = $a.Trim();mountvol $DriveLetter $a} | Out-Null
}

#################################################
# Function: InitNewDisks                        #
#################################################

Function InitNewDisks
{
    Get-Disk | Where PartitionStyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR
    $NewDisks=Get-Disk | Where NumberOfPartitions -eq 0 | sort Number
    Foreach ($Nb in $NewDisks)
    {
        $NewPartition=New-Partition -DiskNumber $Nb.Number -AssignDriveLetter -UseMaximumSize
        Sleep 2
        $NewPartition | Format-Volume -FileSystem NTFS  -Confirm:$false
    }
}

#################################################
# Function: ClearAllEvents                      #
#################################################

Function ClearAllEvents
{
    wevtutil.exe el | ForEach-Object {wevtutil.exe cl "$_"}
}

#################################################
# Main Program                                  #
#################################################
ChangeOpticalDriveLetter "R:" | Out-Null
InitNewDisks
ChangeDriveLabel "C:" "OS" | Out-Null
ChangeDriveLabel "D:" "Apps" | Out-Null
ChangeDriveLabel "E:" "Data" | Out-Null
ChangeDriveLabel "F:" "Backups" | Out-Null
ClearAllEvents

#################################################

After running this, I’m ready for the SCCM installation (or anything else for that matter… This is pretty much standard VM deployment configuration (with or without the F: drive if there are no SQL servers on the box).

To keep this short, I will post the SCCM script in a future post!

Post a Comment