One of the first things managers of large scale datacenter do is to produce a regular naming scheme for their servers. These often embed the function, location, and then an integer. e.g.
IIS-West-001
IIS-West-002
…
IIS-West-234
or
ShareP-Tuk-001
ShareP-Tuk-002
…
ShareP-Tuk-010
PowerShell’s range operator is awesome for working with these environments. If you haven’t already discovered it, the Range Operator is .. – it takes any 2 integers and generates all the numbers between them (including them). An example is worth a thousand words:
PS>1..3
1
2
3
PS> # Notice that it can work backwards as well
PS>3..1
3
2
1
PS> # You can start/stop anywhere you want
PS>33..35
33
34
35
So that is ALMOST useful for this environment because you can do things like:
PS>1..5 |%{"IIS-West-{0}" -f $_}
IIS-West-1
IIS-West-2
IIS-West-3
IIS-West-4
IIS-West-5
Is that cool or what!
Well yes AND no. Notice that in the original example, the integers where padded with 0s. Admins do this so that the width of the server names is fixed so it makes it easy to do reporting and have everything line up correctly. Well .NET formating strings come to the rescue here. Kathy Kam has a great .Net Format String 101 blog entry with lots of examples HERE . You can zero pad numbers this way:
PS>1..5 |%{"IIS-West-{0:000}" -f $_}
IIS-West-001
IIS-West-002
IIS-West-003
IIS-West-004
IIS-West-005
Man I love this stuff!
Here is my new motto: Buy as many Windows Servers as you like, we’ll make it easy to manage them. 🙂
Enjoy!
Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
0 comments