Home > Scripting > Powershell : create homedrive folder with correct permissions (Icacls) by using excel

Powershell : create homedrive folder with correct permissions (Icacls) by using excel


In my last article I spoke of adding student users to AD with an excel file by using Powershell. This script is a little addon to create the corresponding Homedrive folder with correct permissions. In the past I was using a VBS script that used xcacls to create the folder with permissions. In the Windows 2008 R2 with powershell era it would be cool to use icacls. Feel free to combine the two scripts into one script.

####################################################################
#
PowerShell Script add STUDENT homedrive Folder with permissions #
#
#
#
Version: 0.2 #
#
#
#
Requirements: #
#
1. Powershell Installed #
#
2. Permissions on the fileserver #
#
3. Excel 2007 Installed #
#
4. Excel file with the following Columns and data: #
#
Column 1 (A) : Last Name #
#
Column 2 (B) : Middle Name (like van, de, or blank) #
#
Column 3 (C) : First Name #
#
Column 4 (D) : Login Name/Student number (like 11122)#
#
Column 5 (E) : Expertise (Like Network Admin) #
#
Column 6 (F) : Sector (VMBO, HVWO, PRO) #
#
Column 7 (G) : Password #
#
#
#
Changelog: #
#
0.1 First version Richard #
#
0.2 Add dir existance check Richard #
#
#
#
###################################################################

$strExcelFile = "C:\Scripts\leerlingen.xlsx"
$strADUserHomeDrivePath = "\\servername\servershare\studentshomedrive\"
$strIcaclsPrms1 = "/inheritance:r"
$strIcaclsPrms2 = "<netbios domainname>\"
$strIcaclsPrms3 = ":(OI)(CI)F"
$strIcaclsPrms9 = ":(OI)(CI)M"
$strIcaclsPrms4 = "/grant:r"
$strIcaclsPrms5 = "SYSTEM:(OI)(CI)F"
$strIcaclsPrms6 = "<netbios domainname>\domain admins"
$strIcaclsPrms7 = "/T"
$strIcaclsPrms8 = "SYSTEM:F"

####################################################################
#
FUNCTIONS #
#
###################################################################
function addHomeDriveFolder {
#check if dir exists. If not then create dir and set permissions
if (!(Test-Path -path ($strADUserHomeDrivePath + $strADUserAlias)))
{
New-Item ($strADUserHomeDrivePath + $strADUserAlias) -type directory
Start-Sleep -Seconds 1
Icacls
"${strADUserHomeDrivePath}${strADUserAlias}" $strIcaclsPrms1 $strIcaclsPrms4 "${strIcaclsPrms2}${strADUserAlias}${strIcaclsPrms9}" $strIcaclsPrms4 "${strIcaclsPrms6}${strIcaclsPrms3}" $strIcaclsPrms4 $strIcaclsPrms5 $strIcaclsPrms7
}
}
####################################################################
#
MAIN SCRIPT #
#
###################################################################
#
Open COM object, start Excel, open Excel File with workbook
$objExcel = New-Object -Comobject Excel.Application
$objExcel.Visible = $True
$objExcelWorkbook = $objExcel.Workbooks.Open($strExcelFile)

#First Row contains header, so start with row 2
$objExcelCurrentRow = 2

#Loop through excel file and add folder with specific permissions
do
{
$strADUserSector = $objExcel.Cells.Item($objExcelCurrentRow,6).Value()
$strADUserAlias = $objExcel.Cells.Item($objExcelCurrentRow,4).Value()
if ($strADUserSector -ne $Null) {addHomeDriveFolder}
else {}
$objExcelCurrentRow++
}
until ($strADUserSector -eq $Null)

#Close Excel Workbook and Excel and cleanup with .Net Framework
$objExcelWorkbook.Close()
$objExcel.Quit()
$Null = & {
[
Runtime.Interopservices.Marshal]::ReleaseComObject($objExcel)
[
Runtime.Interopservices.Marshal]::ReleaseComObject($objExcelWorkbook)
}
GC]::Collect()
####################################################################
#
END MAIN SCRIPT #
#
###################################################################

Hope you found this script usefull somehow and feel free to comment.

About these ads
Categories: Scripting Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: