Monday, 15 March 2010

Powershell With Microsoft Exchange

This is just a quick post for those wanting to use Powershell with Microsoft Exchange and there is some coverage of Powershell with Active Directory in server 2008 R2 – Free eBook for download here: http://www.red-gate.com/specials/Exchange/the_sysadmin_handbook_ebook.htm?utm_source=simpletalkExchange&utm_medium=email&utm_content=handbookebookmarch10

Looks good from a quick scan and I hope it’s of use to you

Sunday, 28 February 2010

VMware ESX and Powershell

I had planned to explain a bit about using Powershell and VMware ESX together but having just seen a post from @AlanRenouf I through I would leave it to him to explain with his presentation from the London VMware User Group (VMUG).  To see a little of what you can do and some example scripts, head over to the link below.

http://www.virtu-al.net/2010/02/26/london-vmug-my-presentation/

When I know more about what to do with VMware and Powershell I will do an in-depth starter blog on it but for now Alans presentation is a great start.

Tuesday, 23 February 2010

Active Directory in Server 2008R2 – Navigation and Basics for starters

Something Microsoft missed out on in Powershell v1 was active directory cmdlet, however for v1 Quest came to the rescue with their AD commands available free at http://www.quest.com/powershell/activeroles-server.aspx.  Now for the release of Windows Server 2008r2 the Active Directory guys have finally developed full set of cmdlets in a module for working with Active Directory out of the box.  Once Active Directory is installed onto a 2008R2 Domain Controller you will be able to do either “Import-Module ActiveDirectory” or you can just load from the shortcut in the Administrative Tools area on the start menu (but this is something you will want every day if your an AD Admin so right click the shortcut and say ‘pin to taskbar’.  Or if you want to manage your servers from your workstation you’ll require the Remote Server Administration Tools for Windows 7.  Which ever way you enter you will need to know what to do when you get in – below is a poster for your bedroom wall (or somewhere more useful) with your day to day commands, the image will link you to the Active Driectory Powershell Blog from where you can download it.

AD powershell cmdlet poster

So, Let get started… Once you are in powershell and have the ActiveDirectory module loaded type “Get-PSDrive” and you will see AD as one of the options – Yes this means you can “Set-Location AD:” and work your way through just like it was Windows Explorer (Thank you MS).  When you get the prompt of “PS AD:\>” try Dir and see where you can go.

image

As my server is just a test domain awaiting expansion my FQ domain name is just called Forest.Local .  To get into my active Directory I would just type “Set-Location ‘\DC=Forest,DC=Local’”.  Dir again and you will see your OU’s and Containers and you can keep going though using the Set-Location command or its’ alias CD to navigate to where you want to go.  If you go “Set-Location .\CN=Users" you can then try out some of the new cmdlets such as New-ADUser, Get-ADUser, Remove-ADUser – should all be self explanatory what they do (in case you’re wondering though New-ADUser will create a user, Get-ADUser will get you the users info of a specified user, and Remove-ADUser will Remove the users), if you need to know more use the Get-Help command.

The same applies to ADComputer, ADGoup, ADOrganisationalUnit, Etc.  This should cover you for all basic tasks you want to carry out in Active Directory from Powershell.

Something to be aware of is when you are using the Set-Location cmdlet you will need to know whether you are going to a container (CN) or OrganisationalUnit (OU) and when navigation to the domain you will need the DC.

You can tell what you need by using dir or the cmdlet Get-ChildItem and looking at the DistinguishedName.  So for my domain I start with “Set-Location ‘\DC=Forest,DC=Local’” then from there you can go into a container with “Set-Location .\CN=Users", go back up a level with “Set-Location ..”, and go into an Organisational Unit with “Set-Location .\OU=MyOrgUnit”

I know this is very basic stuff but it’ll get you by if you’re just starting out, so I hope it was a valuable read for someone.

Thanks for reading and please leave feedback,

Dave

Monday, 22 February 2010

What Can I Use PowerShell For?

In this post I am after help from everyone who is already using powershell to help everyone who is learning.  I’m not just being lazy, however having been on holiday for a week I haven’t had a chance to update the blog so this should (if it works as planned) help get some good content onto the site quickly.

What I want to have a people using and with expertise (especially other blog/resource writers) write in the comments section of this post just a quick “mini blog post” with the following (if you can’t post a comment then please send an email with the following and I’ll upload on your behalf.):

  • Name of a useful cmdlet your find useful or you think is important to someone learning powershell
  • A short explanation how it’s used and why powershell is your choice of weapon for the task
  • Who you are and where you can be found for twitter/blogs/podcasts -  please also state the intended reader level of your blog (beginner, intermediate, expert, Snover)
  • Area(s) of expertise (i.e. Active Directory, Exchange, VMware, etc.)

Hopefully if readers like the style of your comment they will head towards your blog.  I hope for this to be a good way to get some useful scripts and one liners onto the site from people of different use areas, help readers find other blogs for learning from and to get a good collection of resources.

I aim to keep this post alive and keep it near the top.  So all that left for me to do now is thank everyone for the help and for those learning, I hope this helps.

Thursday, 4 February 2010

Remove-Item

This is just going to be a quick post, it’s a quick bit about what I did earlier at work.  As a bit of background, we have lots of users logging onto lots of computers and profiles don’t currently exist in the roaming variety.  At times the profiles that create on the desktops around the place mess up and have to be deleted.  Doing this manually claims it takes between hours and days to complete.  Before Powershell I made a .bat file that had a list of usernames prefixed by rmdir.  Earlier I had to do the task again and I thought, how will I do this in powershell?

As a start I just went to the Documents and Settings and typed “Remove-Item .\04* –force” (for student users, usernames are prefixed with year of entry), then I though, Can I do more than one at a time so I tried “Remove-Item .\05*,.\06* –force”.

After being happy with that I thought, ‘what more can I do?’, so after “get-help remove-item” I noticed an exclude option, that got me thinking.  All I want to keep in is “All Users”,“Default User”, and any users starting with admin.  I ended up with the script as follows:

c:

cd '\documents and settings'

Remove-Item .\* –Exclude “All Users”,”Default User”,Admin* -force

That’s all there was to deleting everything but what I wanted in that directory – also completes quickly – no waiting for a few hours for the deletes to go though.

This is such a basic task made better with powershell.  Whatever you are trying to do, think, What Can Powershell Do?

Quick warning: DO NOT type “Remove-Item \*” without the . \ for current directory or you may wipe the c: drive

Wednesday, 27 January 2010

PS1 – the scripting side of Powershell

OK… Powershell effectively has two ways of being used.  You can type straight onto the command line or you can script.  This is similar to your traditional command prompt (cmd) where you could open cmd and type your command you want or you can open notepad and write your command into a .bat script file.  I have given a few commands in previous posts  but I haven’t explained how to make an actual script yet, or even what a script is.

If you have been working with computers for a while (especially if your in an admin/tech person role) you will have come across .bat, .vbs, .kix or possibly Perl or Java script files.  Good new for everyone is that we can say bye bye to them and say hello to the .ps1 file.

Something to be aware of before trying to run a .ps1 file in powershell is that you will need to sign your script or make sure that powershell has it’s execution policy set to remote signed.  To check what your current execution policy is set to, in powershell console type: get-executionpolicy .  To view available options type get-help set-executionpolicy and that will give you more details on this.  If you don’t want to open up your script execution policy then your can sign your scripts, I won’t go into that on this blog as I haven’t done this yet, when I do give it a go (sometime soon) I will blog how to do it (or how I did it – hopefully that should be like how you are meant to do it?).

Anyway I have waffled around the subject of this post for long enough so I will get straight onto it. 

You can make a .ps1 file in many ways, the most basic way being opening notepad, writing some Powershell commands, and saving with the extension .ps1 at the end of the file name.  If you would prefer to have Powershell on tap whilst writing your script then try the Windows Powershell ISE.  This will give you a tabbed scripting window, a Powershell command input console and a shared output window.  This is a useful environment  to script in because it allows you to write and test either sections or the whole of your script from the same window you are working in.  The Powershell command input console also allows you to run get-member and help on anything you are putting into your script.

Now we know what the script is when should we write one and for what?

When to write a script seems to be a common question, generally if you are planning to use a command regularly then why not make a script and save wearing out your keyboard?  In my last post I gave the script for a making a powershell logo, it wasn’t very useful but it’s a script and a good example for this post.  Many tutorials you find online will be working with making a script for a task, by starting with the basics you can be ready to work on anything you find online.  You can use a script for many a task, think what you do regularly and think, “Can I script this?”

One last point that I won’t really go into on this post (big topic so needs it’s own but it ties in with this) is your profile.  Your profile is something that loads when you open up powershell, you can set lots of useful stuff in there and it’s editable by you.  I currently have a very basic profile which I have set to change my default directory, load my powershell logo script (it calls in a ps1 file to do that) then clears my screen ready for use.  It’s not much but it’s all I need so far.  As I head off onto a future post I will steer back to the current one but pointing out that your profile is basically a ps1 file that loads when powershell loads.

At this point I think we now a bit about scripts and I’ll hopefully be guiding you through more of what to do with them in up coming posts so check back again and thanks for reading.

Dave.

Tuesday, 19 January 2010

A Cool but pointless script

Ok, Powershell may be the best thing to happen to windows (and other) administrators since the slicing of bread… or more likely since the mmc console.  But for everything useful you can find a completely pointless task for fun… this is what I have be spending a few hours doing recently.

I had seen some ones script to make a Powershell Christmas tree about a month back and thought that was quite cool, then having started this blog I created a twitter account to go along side.  I was happy with the little bird type icon that you get as a default Profile Picture but my wife told me I had to do something more interesting.  I didn’t want to have the standard Powershell picture that most Powershell tweeters use so I thought I could do one in the same style as the Christmas tree I had seen before.  Not knowing where to start I searched for the code of the Christmas Tree (can be found here: http://dnn.itprotoronto.ca/Blogs/tabid/62/EntryId/80/Powershell-Christmas-Tree-ndash-Again.aspx ) and I got to editing.  What I ended up with was great in a massively geeky way to the point I had to show it off.  having shown it to my wife and in-laws where better to next show it off than here.  I will attach the script in a quote below this paragraph and also show off the screen shot.

clear-host
write-host

$Rows = 15
$Slash = 1
    $oldpos = $host.ui.RawUI.CursorPosition

# > sign

    Foreach ($r in ($Slash ..2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14)){
        write-host $("   " * $r) -NoNewline
        1..((($rows +$r) * 0)+1) | %{
                write-Host " ***" -ForegroundColor White  -nonewline
       }
        write-host ""
    }        
    Foreach ($r in ($rows..2)){
        write-host $("   " * $r) -NoNewline
        1..((($rows -$r) * 0)+3) | %{ 
                write-Host "*" -ForegroundColor White  -nonewline
       }
        write-host ""
    }          

    # Underscore

    write-host $("{0}*************************" -f (' ' * ($Rows +10) ))  -ForegroundColor White
    write-host $("{0}*************************" -f (' ' * ($Rows +10) ))  -ForegroundColor White
    $host.ui.RawUI.CursorPosition = $oldpos
    sleep .05

I have to say it’s not the cleanest of scripts possible but it’s a good start.Powershell Logo This blog post has overtaken my last post for what will be published first as I am still writing my other one which is about the PS1 file that is for Powershell scripts but it will be ready soon.   Now you may wonder if this may ever be of use at all but I have a use for it already (besides my profile picture on twitter).  Something else I will cover in the near future is your profile.  In brief your profile is a script that runs when you open Powershell, it is customisable and so as I could I customised it.  I have my profile set so it changes directory to where my scripts live, runs this script to make the logo, then clears the screen again.  It’s not of much use but it’s fun and make Powershell look like it has a boot screen like that of windows when it boots.

So there you have it.  A very useless script but it has a point – I learned about doing something in Powershell whilst having fun.  I am not alone on making pointless scripts for the sake of it and if it’s something you want to do then check out the Scripting Guys scripting games – an annual event for scripters of all levels to try out making a script to for fill a given requirement.

For now anyway it’s time for bed, so have fun learning Powershell and don’t forget to check back another day for more blogs on learning Powershell.