Friday, 7 May 2010

Guessing the Election

For those who read this blog from the UK you may know there was a general election yesterday.  Having stayed up all night watching  BBC1 (while nodding off regularly) it was seeming like they were taking wild guesses about who would win the seats before they were announced.

After playing Rock Paper Scissors with the computer for a bit (Code below) I thought I could probably use powershell to guess at the election.  Firstly I will show you Rock, Paper, Scissors.

get-random “Rock”,”Paper”,”Scissors”

After that I thought to the election and how it would be useful for whose who didn’t know which way to vote

get-random “labour”,”Lib Dem”,”Conservitives”,”other”

And then I thought to how to predict the election results.  For this we are going for more than a one liner although it could be done in one line.  For this I have created a folder in the root of C: called election and made a list of constituenies in a csv file and a file with a list of parties

$Locations=import-csv C:\Election\Constituenies.csv
foreach ($Location in $Locations) {write-output $Location,(Get-Random(get-content C:\Election\Parties.csv))}

This is only a basic script at the moment but it is something that could be built on to to make it more use.

Monday, 5 April 2010

Windows Server 2008 R2 Core

Ok, so you might be wondering what that has to do with Powershell.  Hopefully by the end of this post you might know why I am posting this.  I am writing about Microsoft Windows Server 2008 R2 core as this has the ability to install Powershell (some hacks have been preformed to get it onto 2008 original but these aren’t supported)
Firstly I’ll let Microsoft introduce what Server Core is for if you are unaware:
The Server Core installation option for the Microsoft Windows Server 2008 R2 operating system provides a minimal environment for running specific server roles that reduces the maintenance and management requirements and the attack surface for those server roles.
http://www.microsoft.com/windowsserver2008/en/us/r2-compare-core-installation.aspx
So with being cut back the main thing to go is the GUI leaving you with only a command prompt window to work with.  You will find you need a couple of commands to get you to the point of where you can install Powershell and get back to a more sensible console.  A quick reference sheet is available here.  What I plan to do here is go through what you have to do to get from a clean install to be working with Powershell.  I’m starting from the point of the installation finishing and having chosen my password.
  • When you log in you will be presented with a cmd.exe, this is it, don’t expect any more as it’s not coming.
  • At this stage the name of the computer is fairly random. I’m just on a workgroup for now so to rename the computer I need to type:


    • netdom renamecomputer %computername% /NewName:**
    • After a reboot the name will be changed
    • you can check the name with the “echo %computername% command
  • Many services require a static IP address, however I am not setting up anything that is dependent on that and as the quick reference sheet is clear on how to do that I’ll skip over it for here.
  • Next up we’ll look at what roles and services are enabled and disabled, to do this we use the Dism command as follows:


    • dism /online /get-features /format:table
  • If you look up the list you will find a service called “MicrosoftWindowsPowerShell” and this is set to disabled by default.  When you find what you want installed type the following:


    • start /w ocsetup name_as_above
(Thanks to Jeffrey Hicks for letting me know an easier way)
  • To get Powershell you will need to first have .net framework 2.0 running (NetFx2-ServerCore)then turn on powershell as follows (please note names are caps sensitive):


    • start /w ocsetup NetFx2-ServerCore
    • start /w ocsetup MicrosoftWindowsPowerShell
  • you can now start Powershell by going to:
    • %systemroot%\system32\WindowsPowerShell\v1.0 then running Powershell.exe
  • Now you can get a list of Roles and Features by:
    • Import-Module ServerManager
    • Get-WindowsFeature
      • You can also add and remove using the Add-WindowsFeature and Remove-WindowsFeature comlets.
Jeffrey Hicks has a great blog post about setting Powershell as your default shell here: PowerShell Quick Start on Server Core R2 - The Lonely Administrator – Do this on a test server to start with so you don’t end up breaking your server
I hope this is a good start and to find out more check out the http---powerscripting.net- Podcast Episode 110 when they are talking to “The Server Core Guy”.

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