Tuesday, 9 October 2012

Powershell: Adding users to active directory with a csv


Creating users in AD from a csv file using Powershell

Using Import-Csv and New-ADUser

Some examples in the links
this is good:
http://gallery.technet.microsoft.com/scriptcenter/ed20b349-9758-4c70-adc0-19c5acfcae45
better than this:
http://social.technet.microsoft.com/Forums/en-US/winserverDS/thread/374dca0b-6b93-4a77-b53b-51602b2b4544/

And worth a look? simple automation straight from a csv, and  adding to a group also
http://www.simple-talk.com/sysadmin/exchange/active-directory-management-with-powershell-in-windows-server-2008-r2/

Having problems with execution? see:
http://technet.microsoft.com/en-us/library/ee176949.aspx

Import-Module ActiveDirectory
$Users = Import-Csv ".\myusers.csv" 
foreach ($User in $Users) 
{ 
    $OU = "OU=MyUsers,OU=MyStuff,DC=mydomain,DC=local,DC=com"
    $Detailedname = $User.firstname + " " + $User.lastname
    $Firstname = $User.Firstname
    $FirstLetterFirstname = $Firstname.substring(0,1) #not used this but left it in
    $SAM =  $User.Firstname.tolower() + "." + $user.lastname.tolower()
    $userprinci = $SAM + "@mydomain.local.com"
    $logonscript = "logscript.vbs"
    $homedir = "\\server\myarea\" + $SAM + "\My Documents"
    #tried this alternative
    #$homedir = "\\server\myarea\%username%\My Documents"
    New-ADUser -Name $Detailedname -SamAccountName $SAM -UserPrincipalName $userprinci -DisplayName $Detailedname -GivenName
$user.firstname -Surname $user.lastname -Path $OU -HomeDrive "H:" -HomeDirectory $homedir -scriptpath  $logonscript    -
PasswordNeverExpires $True -PassThru
#the password is blank on this example
}



Still issue with creating home directory for user, seems that i have to create this after by using the %username%, then apply this, then add My Documents.

I dont know if this is due to the user not being in a group(i.e. with correct privileges) or what. But cannot have the user in a group till user exists anyway!!??

So therefore I also needed to manually add to group.

This is the part mentioned earlier that adds users to groups, a bit pointless for now
add users to group"OU=MyUsers,OU=MyStuff,DC=mydomain,DC=local,DC=com"  |  ForEach-Object {Add-ADGroupMember -Identity 'ggGroupName' -Members $_}



Thursday, 20 September 2012

Twitter widget customization

http://www.1stwebdesigner.com/css/customize-twitter-search-widgets/

Twitter widget customization, after copying the code from a currently embedded widget like the one on here ;)

Tuesday, 18 September 2012

Awk, Sed and TR

I used Awk, but had issues with removing whitespace and inserting commas.

I had a poorly written Awk text file script, that filled out 19 fields with info. Done in thee style of a none-programmer.

{ FS = "," } ; # comma-delimited fields
{ sub($1,"")}
{firstname=$3}
{surname = $4}
{group = $8}
{username =tolower( $3"."$4)}
.....
#more fields here zzzzzzzzz
.....
{$16=" "}
{$17=" "}
{$18=" "}
{$19=" "}
{print}

which was invoked like this:

awk -f mybadscript.awk mydata.csv > newdata.csv

Then I had to run Sed

sed 's/[:space:]+/,/g' newdata.csv > afterseddata.csv
http://stackoverflow.com/questions/8766165/using-awk-to-remove-whitespace

Then ran TR

tr ' ' ',' < afterseddata.csv > aftertrdata.csv
http://stackoverflow.com/questions/1271222/replace-white-spaces-with-a-comma-in-a-txt-file-in-linux

Phew. Perl might be easier next time.


Monday, 17 September 2012

Awk substitute and concatenate columns

Using Awk


awk -F, '{ sub($1,"");temp = $3"."$4; $2 = temp; print}' myfile.csv

set delimiter as comma, then removes column 1 sub from "", takes column 3 and 4 and concatenates with a full stop in between them, and puts that in column 2


This was used so i could take a name in a file with a first and last name and create a username like joe.bloggs


Another useful Awk feature is sub strings


awk -F, '{ sub($1,"");temp = $3"."$4; $2 = temp; first= substr($3,1,1); second = substr($4,1,1); $5=first second  ; print}' myfile.csv

substr($3,1,1) takes first char of column 3 puts it in variable first
substr($4,1,1) takes first char of column 4 puts it in variable second
$5 = first second prints these 2 chars together, no spaces

Use on Joe Bloggs to create new column  with contents - JB


This used the substr function

Every good boy.

awk '{print substr($1,1,1)}' temp returns E
awk '{print substr($1,3)}' temp returns ery
awk '{print substr($2,3)}' temp returns od
awk '{print substr($0,7,2)}' temp returns go
 
 






Friday, 31 August 2012

mysql joins

a mysql join

ties 2 tables.

shows all of stockbook, and using a non-descriptive index within stockbook table i.e a number, it displays the more descriptive column from the other table

This works even if the relationship is not enabled, but enabling the relationship prevents incorrect entries by forcing integrity with the drop down list

SELECT `stockbook`.*, `categories`.`type`
FROM stockbook, categories WHERE `stockbook`.`categories` = `categories`.`category_id`

example from

http://www.tizag.com/mysqlTutorial/mysqljoins.php

and statement and ordering with AS command for column ambiguity resolution

SELECT `stockbook`.*, `categories`.`type`,`locations`.`notes` AS 'Location Notes'
FROM stockbook, categories,locations WHERE `stockbook`.`categories` =  `categories`.`category_id` && `stockbook`.`location` = `locations`.`location_id` ORDER BY `stockbook`.`product_id`

Mysql phpmyadmin search replace

phpmyadmin syntax, note no speech marks on tablename or field name

UPDATE tablename SET field = REPLACE ( 
field, 
'HLLOWRDCHG', 
'HELLOWORLDCHANGE');

Tuesday, 28 August 2012

VI editor tricks

Word count


http://vim.wikia.com/wiki/Word_count

find text, then replace part of text

There is another form of line addressing called global addressing. It is similar to the % (all lines) address, but allows you to limit the search and replace action by specifying certain text that must appear in a line before the search and replace action is applied to it. An example is show below. The syntax shown below would read "for all lines containing `some text', search for `search text' and replace any instances with `replacement text.'"

:g/some text/s/search text/replacement text/

other stuff


:%s/test/mytest/gIc

arguments g=global, I=dont ignore case, c=confirmation required

regex

:%s/[A-Z]', '2'//gn

count occurences of: letter then comma then space then apostrophe then 2 apostrophe