Archive

Posts Tagged ‘scripting’

Powershell : Generate passwords into an excel file

February 18, 2011 1 comment

Some months ago a posted a VBS script that generated passwords into an excel file (found here). This time I wrote one that can do this by using powershell. This scipt can be used with other powershell scripts I wrote in combination with excel. Very handy if you need to create passwords for hunderds of new student accounts.

####################################################################
#
PowerShell Script to generate a password into an excel file #
#
#
#
Version: 0.1 #
#
#
#
Requirements: #
#
1. Powershell #
#
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 (like 22222) #
#
Column 5 (E) : Class (like 4K1) #
#
Column 6 (F) : Sector (VMBO, HVWO, PRO) #
#
Column 7 (G) : Password #
#
#
#
Changelog: #
#
0.1 First version Richard #
#
#
#
###################################################################

$strExcelFile = "C:\Scripts\students.xlsx"

####################################################################
#
FUNCTIONS #
#
###################################################################
function Release-Ref ($ref) {
([
System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
[
System.GC]::Collect()
[
System.GC]::WaitForPendingFinalizers()
}
function CreatePassword([int]$length){
$specialCharacters = "_!+"
$lowerCase = "abcdefghijklmnopqrstuvwxyz"
$upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$numbers = "1234567890"
$res = ""
$rnd = New-Object System.Random

do
{
$flag = $rnd.Next(4);
if ($flag -eq 0)
{
$res += $specialCharacters[$rnd.Next($specialCharacters.Length)];}
elseif ($flag -eq 1)
{
$res += $lowerCase[$rnd.Next($lowerCase.Length)];}
elseif ($flag -eq 2)
{
$res += $upperCase[$rnd.Next($upperCase.Length)];}
else
{
$res += $numbers[$rnd.Next($numbers.Length)];}
}
while (0 -lt $length--)
return $res
}
####################################################################
#
MAIN SCRIPT #
#
###################################################################
#
Get First Cell and last Cell value (using VBS for this)
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$strFirstCell = [Microsoft.VisualBasic.Interaction]::InputBox("First Cell value (like G2)", "Password Generator for Excel")
$strLastCell = [Microsoft.VisualBasic.Interaction]::InputBox("Last Cell value (Like G100)", "Password Generator for Excel")
$objCellRange = $strFirstCell + ":" + $strLastCell

#Open COM object, start Excel, open Excel File with workbook
$objExcel = New-Object -Comobject Excel.Application
$objExcel.Visible = $True
$objExcel.DisplayAlerts = $False
$objExcelWorkbook = $objExcel.Workbooks.Open($strExcelFile)

#loop through CellRange and create password with 6 chars (change 5 to something else if more required)
ForEach ($objExcelCell In $objExcel.Range($objCellRange)) {$objExcelCell.Value() = createpassword 5}

#Save and close Excel Workbook, close Excel, cleanup and kill EXCEL32 process
$objExcelWorkbook.SaveAs($strExcelFile)
$objExcel.DisplayAlerts = $True
$objExcelWorkbook.Close()
$objExcel.Quit()
Start-Sleep -Seconds 2
Release-Ref $objExcelWorkbook
Release-Ref $objExcel
spps -n excel
####################################################################
#
END MAIN SCRIPT #
#
###################################################################

Feel free to comment.

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

February 16, 2011 Leave a comment

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.

Categories: Scripting Tags: , , ,
%d bloggers like this: