Cool Tool.bat

I made a batch script at work today to automate a few things as I was working on 15 PCs at once…

Thoguht I would share it here in case someone who happens to be in the same situation stumbles on it!

It has a number of features for example changing the PC name, changing the key for MS Office and then activating it, activating the local admin account and setting a password, auto logging a user in, and REBOOT =D

Its probably buggy, but worked 13 times out of 15 for me! so deal with it..

oh, install office doesnt work but meh. 😀

here she be:

 

@echo OFF
TITLE Cool Tool
color f0
:MENU
CLS
ECHO ============= COOLTOOL =============
ECHO ————————————-
ECHO 0. Install Office
ECHO 1. Activate Local Admin (Blank Password)
ECHO 2. Rename PC
ECHO 3. Auto Login “Administrator”
ECHO 4. Install office key and activate. (WARNING: INSTALL OFFICE x64 FIRST + CONNECT TO NET)
ECHO 5. Reboot PC To apply changes.
ECHO ==========PRESS ‘Q’ TO QUIT==========
ECHO.

SET INPUT=
SET /P INPUT=Please select a number:

IF /I ‘%INPUT%’==’0’ GOTO OFFICE
IF /I ‘%INPUT%’==’1’ GOTO ADMIN
IF /I ‘%INPUT%’==’2’ GOTO RENAME
IF /I ‘%INPUT%’==’3’ GOTO LOGIN
IF /I ‘%INPUT%’==’4’ GOTO OFFICEKEY
IF /I ‘%INPUT%’==’5’ GOTO REBOOT
IF /I ‘%INPUT%’==’Q’ GOTO Quit

:OFFICE
office\setup.exe

GOTO MENU

:LOGIN
echo 1st run.
REG ADD “HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon” /v AutoAdminLogon /t REG_SZ /d 1 /f
REG ADD “HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon” /v DefaultUserName /t REG_SZ /d User /f
REG ADD “HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon” /v DefaultPassword /t REG_SZ /d Password /f
echo 2nd run..
REG ADD “HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon” /v AutoAdminLogon /t REG_SZ /d 1 /f
REG ADD “HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon” /v DefaultUserName /t REG_SZ /d User /f
REG ADD “HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon” /v DefaultPassword /t REG_SZ /d Password /f
echo 3rd run…
REG ADD “HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon” /v AutoAdminLogon /t REG_SZ /d 1 /f
REG ADD “HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon” /v DefaultUserName /t REG_SZ /d User /f
REG ADD “HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon” /v DefaultPassword /t REG_SZ /d Password /f
echo 4th run….
REG ADD “HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon” /v AutoAdminLogon /t REG_SZ /d 1 /f
REG ADD “HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon” /v DefaultUserName /t REG_SZ /d User /f
REG ADD “HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon” /v DefaultPassword /t REG_SZ /d Password /f
done. =D
GOTO MENU

:RENAME
SET INPUT=
SET /P INPUT=Input Computer Number:

echo PCs new name will be: %INPUT%
pause
wmic path win32_computersystem where “Name=’%computername%'” CALL rename name=’%INPUT%’

GOTO MENU

:ADMIN
net user administrator /active:yes
net user administrator “Password”

GOTO MENU

:OFFICEKEY
cd “C:\Program Files\Microsoft Office\Office14\”
delay 3
cscript ospp.vbs /inpkey:xxxxx-xxxxx-xxxxx-xxxxx-xxxxx
echo I SET THE KEY FOR YA. Now to activate…
cscript ospp.vbs /act
echo DONE!

 

By the way,

The auto login runs a bunch of times because sometimes it works but doesnt…work..?
😀

Powershell For loops

For loops have three parts:

For ($1; $2; $3){

}

$1 is the initial value
$2 is the condition
$3 is what to loop

first, $1 is checked against the condition ($2) then, $3 is run and checked against the condition, if it still matches the condition, the loop continues.
if it no longer matches the condition, the loop exits.

refer to le dodgy diagram

dodgediagram

 

Broken Example Code:

 

#I couldnt figure out the factorials and it kept getting me confused and stuck so ill use addition instead. >dealwithit.gif

clear-host
$nums =@()
$array =@()

[int]$times = read-host “how many sums will you do”

For ($i=0; $i -lt $times; $i++)
{

$num1 = read-host “enter a number”
$num2 = read-host “enter a second number”

$num3 = [int]$num1 + [int]$num2

$array = @($num1,$num2,$num3)
write-host $num1″+”$num2″=”$num3
}
For ($i=0; $i -lt $times; $i++){
write-host “Nums”`t”Sum”
write-host $array[-$array.count]$array[-2] `t $array[-1]
write-host $array[$i]`t$
}

Arrays in powershell

Reference sheet for my powershell class

Arrays seem to be like small temporary databases ? 😀

 

Creating an Array:

C:\PS>$numbers = @(4,7,9,11,20)     //creates array and populates entries with data
C:\PS>write-host $numbers     //prints entire array

Number 9 can be retrieved with

C:\PS>$numbers[2]    //prints 3rd item in array (integers start at 0

similarly

C:\PS>write-host $numbers[2] “this is the second item in the array”    //includes text at the end and uses the write-host command

C:\PS>$names = @(“Martin”, “Emma”, “Alan”, “Laura”)
    //this is how words are stored into an array.

Or you can  can create an empty array and then add elements later:

C:\PS>$MoreNames = @() 
C:\PS>$MoreNames += “Emma” 
C:\PS>$MoreNames += “John” 
C:\PS>$MoreNames += “Gerald”

The size of the array can be retrieved with the commands

C:\PS>$ArrayName.count 

OR
C:\PS>$ArrayName.length

(Both these seem to return the same values)

An array can be auto populated with a number range by using double dots to tell powershell to fill in the gap. eg:

C:\PS>$range = @(10..50)

Will create an array containing the numbers 10, through to and including 50.

To retrieve the last item from the array, you can use either

C:\PS>$array[$array.Length-1]

OR

C:\PS>$array[-1]

To retrieve the FIRST item in an array, you first get the length, you substitute the [-1] with [-$array.count] . Or any variation of these to your desired item.

 To store the output from a command to an array, use cmdlets:

C:\PS>$array = Get-Process

To create an array that already has item spaces for a set amount of items, you can use:

C:\PS>$NameArray = New-Object String[] 8 
C:\PS>$IntArray = New-Object Int[] 12

Beginning Assignment:

CLS  #clears all the crap off the screen from the last thing i ran 😀

write-host “Press any key to Start”
$x = $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”) #waits for a key to be pushed before continuing
write-host “”
write-host “Generating random numbers”

$Item0 = Get-Random -minimum 0 -maximum 99 #Generates a random number between 0 and 99 and stores it in $Item0
$Item1 = Get-Random -minimum 0 -maximum 99
$Item2 = Get-Random -minimum 0 -maximum 99
$Item3 = Get-Random -minimum 0 -maximum 99
$Item4 = Get-Random -minimum 0 -maximum 99
$Item5 = Get-Random -minimum 0 -maximum 99
$Item6 = Get-Random -minimum 0 -maximum 99
$Item7 = Get-Random -minimum 0 -maximum 99
$Item8 = Get-Random -minimum 0 -maximum 99
$Item9 = Get-Random -minimum 0 -maximum 99

$RandomArray = @($Item0, $Item1, $Item2, $Item3,$Item4,$Item5,$Item6,$Item7,$Item8,$Item9) #stores the randomly generated numbers into $RandomArray
write-host “”
write-host “Now all the numbers are generated and stored in the `$RandomArray` array,”
write-host “we will use a sort that puts the largest into the”
write-host “variable until its reached the end of the array.”
write-host “”
[array]::sort($RandomArray) #sorts the array ?

write-host “”
write-host “The largest number in the array is” $RandomArray[-1] #writes out the last item of $RandomArray
write-host “”

 

update: next exercise involved those freaking factorials, i am once again lost and unsure how to continue <,<

WHY DO I GET STUCK ON THESE

also, i cant get loops into my head in this stupid language, why is C/C++ easier than this ?

isn’t powershell meant to be a baby language or something?