PowerShell? Do you use it? Can you show me some cool system administration things I can do with it?

19,270

Solution 1

Microsoft is doing all it can to make PowerShell the choice of power-users and automation writers everywhere. Gone are the days of compiling code in .NET in order to do the same thing, now you just need notepad.exe and google. We're big fans of it in the office, especially since Exchange 2007's Management Console does NOT include everything that you can do in PowerShell. Microsoft deliberately failed to implement things that only get done once in a great while, easier to develop that way, which downright forces its use if you have anything resembling a complex environment.

Managing Microsoft's newer generation of products (Win7, Windows Server 2008, Exchange 2007/2010, SQL Server 2008) all have very rich PowerShell hooks. Once Remote Powershell (PowerShell 2.0 IIRC) gets deployed with Server 2008 R2, it'll become even MORE useful for automation writers.

What we've done with it:

  • Create a web-page to delegate certain admin tasks to helpdesk users. The web-page fires off commands that get executed in PowerShell. Things it does:
    • Create and delete user accounts, including provisioning Exchange 2007 mailboxes and home directories
    • Unlocks locked out accounts
    • Create/delete groups
    • Add/remove users from groups
    • Move users between mail-stores
    • Set passwords
  • Take extracts from the ERP system and push global-address-book data into Active Directory nightly.
  • Solve the LegacyExchangeDN problem that cropped up with our Exchange 2003 to Exchange 2007 migration. Had to add an X500 address to everyone that used to be on Exchange 2003. A fairly short PowerShell script fixed it.
  • Scripted creation of "group mailboxes" (shared mailboxes in Exchange where multiple users have access to the mailbox), an otherwise manual process due to the nature of the data we need before kicking it off. It greatly standardized the setup of these mailboxes.
  • Created a script that walked through all domained machines resetting a specific registry key and restarting a service. It took 18 hours to complete, but it got the job done.

So yes, PowerShell is going to be with us for quite some time.

EDIT: Adding a code-sample, since it was requested

$list=import-csv("groupusers.csv")
$lastseengroup=$list[0].group
$ADGroupPrefix="grp.netware."
$ADGroupSuffix="{redacted -- in the format of ,ou=groups,dc=domain,dc=domain,dc=domain}"
Clear-Variable memberlist
Clear-Variable unknownusers
foreach ($entry in $list) {
    if ($($entry.group) -ne $lastseengroup) {
        echo "stumbled across new group $($entry.group), committing changes to $lastseengroup"
        $newgroup=$ADgroupPrefix+$lastseengroup
        $newgroupdn='"'+"cn=$newgroup$ADGroupSuffix"+'"'
        echo "getting DN for $newgroup"
        $existinggroup=dsquery group domainroot -name $newgroup
        if (($existinggroup -ne $null)) {
            dsmod group $newgroupdn -chmbr $memberlist
        } else {
            dsadd group $newgroupdn -scope u -secgrp yes -members $memberlist -desc "Group imported from eDirectory"
        }
        Clear-Variable memberlist
    }
    $User=get-user $($entry.member) -ErrorAction SilentlyContinue
    if ($User.isvalid) {
        $UserDN=$User.distinguishedname
        $memberlist=$memberlist+'"'+"$UserDN"+'" '
    } else {
        $unknownusers=$unknownusers+$($entry.member)
    }
    $lastseengroup=$($entry.group)

}
dsadd group "cn=$ADGroupPrefix$lastseengroup$ADGroupSuffix" -scope u -secgrp yes -members $memberlist

This takes a CSV file created with a perl script and updates a set of groups. If the group already exists, it replaces the membership with that specified in the file. If the group does not exist, it creates it. This is a one-way sync. Also, not quite in production yet, but close.

Solution 2

Given that Microsoft's server products are going to be PowerShell-enabled from the outset (I believe the next version of Exchange has all config available through PowerShell), and that books like PowerShell in Practice describe some great ways to automate otherwise monotonous tasks, I think it's reasonable to suggest that PowerShell will be a prevalent technology in Windows serverland for a while yet.

Solution 3

I'd recommend Scott Hanselman's Podcast #162. It seems like all the Microsoft server teams are "forced" to provide PowerShell cmdlets, and also to follow a single, consistent syntax.

Also, third parties like VMWare are adopting it

In short, I believe that PowerShell starting with 2.0 is seriously in the business of replacing batch files for all but the most trivial tasks.

Solution 4

Ten PowerShell scripts I use as an SQL Server database administrator (all are described/posted on my blog):

  1. Check disk space on all SQL Servers and load data to an SQL table
  2. Run permissions reports on all production SQL Servers and load data to an SQL database
  3. Discover all Windows Server 2003 clusters, nodes, and virtuals on network and load to SQL tables
  4. Discover all databases on all SQL Servers as defined in SCCM and load to an SQL table
  5. Create an SQL Server backup scorecard by loading backup information across all SQL Servers to an SQL table
  6. Verify that TCP Offload Engine is disabled in Windows Server 2003 SP2 (this is a performance killer for many SQL Servers)
  7. Verify Disk Partition alignments (also a performance killer if disks are aligned improperly)
  8. Copy SQL tables from one server to another
  9. Recursivley copy all SSIS packages using an MSDB Storage from one server to another, including folder structure
  10. Create my own graphical object dependency viewer

Solution 5

What kind of admin tasks have you scripted with it?

application/service monitoring: get key performance numbers out of (remote) EventLog and Stored Procedures and display them from a single batch file

I'm a SQL Server DBA, show me some cool things to do with it?

automssqlbackup: daily backup for MSSQL with rotation (daily=incremental, weekly+monthly=full; keep 5 daily, 5 weekly backups), zipping, email notification

Share:
19,270

Related videos on Youtube

Nick Kavadias
Author by

Nick Kavadias

Updated on September 17, 2022

Comments

  • Nick Kavadias
    Nick Kavadias over 1 year

    I keep reading everywhere that PowerShell is the way of the future. When it was first released I did a whole bunch of virtual labs, but since then I still haven't used it in a production environment. I know the day will come when I'm dealing with OS's where it's already installed, so I want to be ready.

    I want to know:

    1. Do you use it?
    2. What has your 'bootstrapping' process been for using PowerShell?
    3. What kind of system administration tasks have you scripted with it?
    4. I'm an SQL Server database administrator. What are some cool things to do with it?

    It seems that everyone agrees that Microsoft is pushing this hard, but no one is actually using it yet. I want to hear from system administrators out there that are using it to do every day tasks and share some code samples.

    • Sophie Alpert
      Sophie Alpert almost 15 years
      JScript is dead?
    • Jaykul
      Jaykul almost 15 years
      Try not to be a troll with your misleading headlines next time.
    • Nick Kavadias
      Nick Kavadias almost 15 years
      Maybe it's because sometimes i wish it would go away like jscript. Is it just me or is it very ugly & verbose?
  • Nick Kavadias
    Nick Kavadias almost 15 years
    that book is still in beta/ being written
  • Nick Kavadias
    Nick Kavadias almost 15 years
    remote powershell!! you mean like ssh!?! about time!
  • moobaa
    moobaa almost 15 years
    Yes, it is - but you can still purchase it now & get early access to the contents. And there's tons of server-maintenance stuff in there :)
  • paulr
    paulr almost 15 years
    Exchange has been all PowerShell since Exchange 2007. OCS will be fully PowerShell-capable in its next release; most of the System Center products either support it fully or will in their next releases. PowerShell isn't going anywhere.
  • Deb
    Deb almost 15 years
    It's not the fastest thing in the world, but it can do just about everything. If all you need to do is map a few drives, then batch scripts are way faster.
  • ashiso
    ashiso almost 15 years
    Check out this little powershell script to ngen your assemblies and speed up all your powershell based consoles: msexchangeteam.com/archive/2008/08/01/449426.aspx
  • Brian
    Brian almost 15 years
    Got any sample code you're willing to share? I'm particular interested in reading and writing SharePoint lists.
  • Nicolas Dorier
    Nicolas Dorier almost 15 years
    There is PowerGUI, and in powergui use ScriptEditor.exe, I can't imagine how I can live without that...
  • Precipitous
    Precipitous almost 15 years
    Powershell 2 includes a great Powershell ISE (Integrated Script Editor). The CTP 3 is available, RTM is around now, final release in October 2009.
  • Nick Kavadias
    Nick Kavadias over 14 years
    hooray for powershell, too bad it isn't installed by default on a 2003 server. The registry location helped though!
  • Andrei Rînea
    Andrei Rînea over 14 years
    +1 for automating the whole MSSQL backup process.
  • jftuga
    jftuga over 13 years
    +1 Wow, this is pretty useful. Thanks!
  • Sam
    Sam about 9 years
    lookup psremoting