Quick and Dirty Scripting

A blog that focuses on automating system administration tasks for Linux, Windows, and VMware ESX

Thursday, December 27, 2007

 

Update to VMware's Powershell Toolkit

 

Just wanted to throw this out there for anyone using VMware's Powershell toolkit. They released a new CTP on 12/26. The file version is 68764.  They've added a bunch of new commands such as Get-Inventory, New/Get-OSCustomizationSpec, Add-VMHost, Move-Datacenter, Move-Cluster, Find-EntityViews, etc . .

I need to find some time to play with these new commands. I do know that the base Get-VIServer and Get-VMHost do work with ESX 3.5 which is nice to see as well.

Until I have more scripts to share. . .

Labels: ,


Sunday, December 2, 2007

 

How to Find all Domain Computers Using Powershel

I'm asked a lot to find out something from all the servers in my Active Directory domain. Like "Tell me the free space on all servers in the domain" or "List all of the error events from Microsoft's Event Viewer for servers in the domain." These two questions can be answered very easily using WMI and Powershell,namely the get-WMIobject function. But how to get all of the server names? Using VBScript, this can be accomplished by using ADSI. But the code is hard to follow and I never really got a hang of using the ADSI calls. But why uses that when there is a great tool made already. dsquery. dsquery is tool written by Microsoft to query Active Directory (http://technet2.microsoft.com/WindowsServer/en/Library/46ba1426-43fd-4985-b429-cd53d3046f011033.mspx). Using this and combined with Powershell is Quick and Dirty but as always gets the job done.

So how do you use it? First the basic . . .

dsquery computer "ou=Domain Controllers,DC=example,DC=com" will find all servers in the Organizational Unit Domain Controllers. dsquery has a option 'server' which will do the same thing (dsquery servers)

The return result is . . .
"CN=DC03,OU=Domain Controllers,DC=example,DC=com"
"CN=DC02,OU=Domain Controllers,DC=example,DC=com"
"CN=DC01,OU=Domain Controllers,DC=example,DC=com"

To return all computers, just do ....
dsquery computer -limit 0 - The limit 0 tells dsquery not to limit the number of systems it returns. I believe the default is 100.

dsquery can also be used to find servers in the domain . . . .
dsquery computer -name "*WEB*" will return
"CN=DRWEB01,OU=Web,OU=Disaster Recovery,OU=Machines,OU=Shared Applications,DC=example,DC=com"
"CN=DEV-WEB01,OU=Development,OU=Machines,OU=Shared Applications,DC=example,DC=com"
"CN=STG-WEB01,OU=Stage,OU=Machines,OU=Shared Applications,DC=example,DC=com"
"CN=STG-WEB02,OU=Stage,OU=Machines,OU=Shared Applications,DC=example,DC=com"
"CN=PRD-WEB01,OU=Production,OU=Machines,OU=Shared Applications,DC=example,DC=com"


So can this be used with Powershell. As long as dsquery is located in the path where powershell can call it from, just write a function such as this

function GetDomainServers {
return @( dsquery computer -limit 0 -o rdn | % { $_.Split("`"")[1] } )
}
The -o rdn means to just return the server name. The server name will be in quotes which is why the return values are piped into Split("`""). This will split the server name into 3 parts. We just want the second value. This is combined to form an array which is returned.

In other words,
dsquery computer -limit 0 -o rdn returns "PRD-WEB01".
The powershell command Split("`"") will split the value on ".
This returns
"
PRD-WEB01
"

To call this function, its as easy as $servers = GetDomainServers.

Well that's it. As always, I would love to hear feed back from anyone who finds these posts interesting and helpful.

Archives

October 2007   November 2007   December 2007   January 2008   February 2008   April 2008   July 2008  

View Brian Denicola's profile on LinkedIn

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]