Archive

Posts Tagged ‘generate password’

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.

Advertisements

Generate passwords into an Excel file

October 18, 2010 Leave a comment

There are alot of user accounts mutations when a new school year starts. Creating hunderds of new user accounts can be a hassle and all those new accounts also need an password. This little script will generate passwords and place them into an excel file. This excel file can be used to create all those accounts with the generated password.

‘enable debugging
Option Explicit

‘create memory for objects
Dim objExcelRange, objExcelRange1, objExcelRange2, objExcel, objExcelWorkbook, objExcelCell
Dim objPasswordLoops, objRandomCharacter, objPasswordMFactor, objPasswordNumber, objPasswordChar, objPasswordNext

‘define globale constants
Const strExcelFileName = “D:\student.xls”
Const strExcelBoxTitle = “Password Generator for an Excelfile”
Const strPasswordLength = 6
Const  strPasswordCharacters = “abc1de2fg3hi4jk5lm6no7pqrs9tuvw0xyz”

‘asking for values
objExcelRange1=InputBox(“Enter value for the first cell” & VBCRLF & “(Example: G2) :”,strExcelBoxTitle,”G2″)
objExcelRange2=InputBox(“Enter value for the last cell” & VBCRLF & “(Example: G100) :”,strExcelBoxTitle,”G100″)
objExcelRange = objExcelRange1 & “:” & objExcelRange2

‘create an excel instance and open workbook
Set objExcel = CreateObject(“Excel.Application”)
Set objExcelWorkbook = objExcel.Workbooks.Open(strExcelFileName)

‘generate password for every cell entered into inputbox
For Each objExcelCell In objExcel.Range(objExcelRange)
 objExcelCell.Value = StrPassword(strPasswordLength,strPasswordCharacters)
Next

‘close and save excelfile
objExcelWorkbook.SaveAs strExcelFileName
objExcel.Quit

‘*************************************************************************
‘***********************      Functions       ****************************
‘*************************************************************************
Function GenIt(objPasswordMFactor)
 Randomize
 GenIt=INT(RND()*objPasswordMFactor)+1
end Function
‘*************************************************************************
Function StrPassword(strPasswordLength,strPasswordCharacters)

StrPassword = “”

objPasswordMFactor = Len(strPasswordCharacters)

For objPasswordNumber=1 to strPasswordLength
 objPasswordChar = GenIt(objPasswordMFactor)
 objPasswordNext = Mid(strPasswordCharacters,objPasswordChar,1)
 StrPassword = StrPassword & objPasswordNext
Next
End Function
‘*************************************************************************
‘*************************************************************************
‘*************************************************************************

%d bloggers like this: