Sunday, February 26, 2012

Logic outline for rename server script

Via Jeff Hicks on Twitter (https://www.twitter.com/jeffhicks), I found this post on renaming a computer using Powershell

http://newdelhipowershellusergroup.blogspot.in/2012/02/set-computer-name-using-powershell.html

I need to compare this on Monday to my work-in-progress script.  This script above is probably the same as what I had found, but lacks the error correction I am still working on.  What if someone enters the name '&laptop #1'?  Plus, I want to account for someone entering a FQDN or just the hostname.  Some ideas I was working on:

1. Check if it's a fully qualified domain name (regular expression check for hostname.something.something)
2. Quick check the hostname is valid for Windows
3. Quick check the hostname meets our server naming standards
4. Check if the DNS suffix meets our naming standards
5. Show the hostname and DNS suffix and prompt for confirmation before renaming the server

Another idea:
Require the hostname but make the DNS suffix optional, defaulting to our 'normal' DNS suffix. Similar to this:

Rename-Server -newname ServerA

would result in this type of output from the script:
Assuming default DNS suffix of mydomain.local. Please confirm is ServerA the correct hostname?
[Y] [N] [?]

But Rename-Server -newname ServerA -dnssuffix myotherdomain.local would also work.
Please confirm ServerA is the correct hostname and myotherdomain.local is the correct DNS suffix.
[Y] [N] [?]

Lots of high standards here for this script, I must remember the adage 'done is better than perfect' and have a working script that we can start using soon.  I could probably spend 8 hours perfecting the regex for the Quick check if hte hostname is valid for Windows.

Friday, February 24, 2012

I was interrupted in my script that I was working on yesterday, so maybe I'll get back to that this weekend.

The interruption today was to enumerate all of the servers in a particular OU that is going to have a Group Policy change coming up. We need to document all of our changes in a Change Management application, and one of the required fields is 'Affected Systems'.  I figured the easiest way to get a text list of the servers affected would be to run a Powershell script, and it took all of about 30 seconds to find something that would work.

http://social.technet.microsoft.com/Forums/en-US/winserverManagement/thread/a0cebabf-3fc9-49b0-be55-5e4ff3232b7b/

The code I used wasn't from the correct answer, but from this guy's answer:


$strComputers = @()
$ou = [ADSI]"http://www.blogger.com/ EB,DC=EB-MILLIMAN,DC=COM"
foreach ($child in $ou.psbase.Children) {
 if ($child.ObjectCategory -like '*computer*') { $strComputers += $child.Name }
}


This code puts the Computers into the string $strComputers, which in turn can be called...

I simply modified the code to reflect my own AD structure and then did a write-host $strComputers.

P.S.  My first professional job out of college was as a consultant with Milliman & Robertson (now Milliman, Inc.).  Whoever posted the code should be advised not to give away too much information about their internal AD structure on the Internet.


Update:  I've found that using variable names with Hungarian Notation ($strComputers) is not a best practice for Powershell.  http://www.powershellcommunity.org/Wikis/BestPractices/tabid/79/topic/Naming/Default.aspx

 

Thursday, February 23, 2012

A girl's gotta have choices!

*yawn* It's bedtime but I had to jot something down tonight. I know when I read this tomorrow I'm going to be embarassed by the lack of a cohesive thought process here.

I've been working on a script that will require the user to choose something.  Think this is outdated? http://blogs.technet.com/b/jamesone/archive/2009/06/24/how-to-get-user-input-more-nicely-in-powershell.aspx . 

I think what I'm looking for is more along these lines:
http://technet.microsoft.com/en-us/library/ff730939.aspx

More work on this tomorrow.  I have a lunch presentation to go to,  so no scripting over lunch again (that's three times this week, no wonder I'm behind on my studying!). The lunch presentation is about our industry and competitors, though, so it is good for learning the big picture.

Thursday, February 16, 2012

What process is listening on a port on a remote server?

Today's inconvenience involved wanting to query a list of servers to find out what process was listening on a particular port. I'd like to be able to use a txt file or csv as input. 

serverA,443
serverB,8888
serverC,11008

So, I need to figure out how to find out the full path to the process that's listening on port 443 on serverA, port 8888 on serverB, etc. 

I didn't write a script to do so because I didn't have time.  But I am putting the question out here because someday I should be able to script this.  My quick search online revealed many simple scripts that I could cut and paste on a local server, but I want something that will query a remote server (difficulty: I don't have Powershell remoting enabled, so it would have to run on my workstation and remotely query the servers).

Wednesday, February 15, 2012

Needed to quickly check disk space on a bunch of servers today and I found this one-liner via Bartvdw (http://bartvdw.wordpress.com/2008/06/19/powershell-how-to-retrieve-disk-size-free-disk-space-for-a-list-of-computers-input-file/)


Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" -computer (Get-Content c:\scripts\computers.txt) | Select SystemName,DeviceID,VolumeName,@{Name="size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="freespace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}} | Out-GridView

Perfect? No.  But it did the trick and it did it quickly.  And it was a nice reminder to me that if you cut and paste a script from the web, sometimes you need to find and replace all the quotation marks because blog sites like to turn them into pretty quotes instead of the plain straight quote marks that Powershell needs. 

If you get this error:

Unexpected token ':N1' in expression or statement.
At :line:1 char:179
+ Get-WMIObject Win32_LogicalDisk -filter "DriveType=3″ -computer (Get-Content c:\scripts\computers.txt) | Select SystemName,DeviceID,VolumeName,@{Name="size(GB)";Expression={"{0:N1} <<<< " -f($_.size/1gb)}},@{Name="freespace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}} | Out-GridView

Then check your quotes.