Get Active Directory User Information in PowerShell

One feature that PowerShell will likely be missing when it first ships is solid support for ADSI and working with Active Directory. You can use .NET DirectoryEntry objects but it feels more like programming and less like scripting. Another option for working with Active Directory in PowerShell is to use WMI. PowerShell does have a great Get-Wmiobject cmdlet. We can use it connect to the LDAP WMI namespace on a domain controller and get information.

This script will prompt you, using the Read-Host cmdlet, for user credentials, domain controller and the sAMAccountname of a user object in Active Directory. With this information we use Get-Wmiobject and connect to the root\directory\ldap namespace. The output is filtered using Where to only return the user we’re seeking. The script will then display all all the WMI properties and their values for this object. This is a handy way of learning the attribute names so you can create more refined scripts. The script will prompt you for a filename. If you specify one, then the output will also be saved to the file as well as displayed on the screen.

#GetUserProperties.ps1

$user=read-host “What user credentials do you want to use for”
"authentication to the "
n”domain controller? Use format domain\username”
$cred=get-credential $user
$server=read-host “What domain controller do you want to connect to?”
$account=Read-Host “What is the sAMAccountname you want to find?”

$rc=read-host “Do you also want to save output to a text file? Enter Y to save”
if ($rc -eq “Y”) {
$file=read-host “Enter the filename and path”
write-host “Connecting to” $server “as” $user
get-wmiobject -class ds_user -namespace root\directory\ldap
-computername $server -credential $cred

|where {$_.DS_sAMAccountName -eq $account} | tee-object -file $file
}
else {
write-host “Connecting to” $server “as” $user
get-wmiobject -class ds_user -namespace root\directory\ldap
-computername $server -credential $cred

|where {$_.DS_sAMAccountName -eq $account}
}

Technorati Tags:




PowerShell podcast

Don Jones from ScriptingAnswers.com has a two part podcast with Jeffrey Snover, Microsoft’s PowerShell architect recorded at this year’s TechEd in Boston. You can hear the podcasts and subscribe to the ScriptingAnswers Audio Network feed at:

http://feeds.feedburner.com/ScriptinganswerscomAudioNetwork

Technorati Tags:


Printing from PowerShell

PowerShell has a slick feature that allows you to send the output from a cmdlet or expression directly to a printer. Pipe the output to the Out-Printer cmdlet and it will print out on the default printer:

get-process | out-printer

If you have other printers installed you can use the printer name. For example, I have Adobe Acrobat installed which installs a virtual printer. I can send the output of a command to that printer and generate a pdf:

get-service |where {$_.status -eq “stopped”} |out-printer “Adobe PDF”

If you want to print to a network printer, specify the printer UNC:

get-service |where {$_.status -eq “stopped”} |out-printer “\\Print01\HPLaserJ”

(By the way, you don’t have to wrap the printer name in quotes, but I find it a good practice to avoid any confusion, especially when the printer name might have quotes.)

This is also a quick way to print the contents of text files directly from within PowerShell:

get-content c:\boot.ini |out-printer “\\Print01\hplaserj”

There’s no page numbering or formatting options but it is a quick way to get a hard copy of you results.

Technorati Tags:


Introduction to WMIC

WMIC – An Introduction
Windows Management Instrumentation (WMI) is an extremely powerful technology that provides tremendous detail on how a computer system is configured and operating. What makes WMI so attractive to Windows administrators is that you don’t have to be a programmer to leverage it. WMI is available as a scripting option. There are many WMI VBScripts available online and as part of the Windows 2000/XP/2003 Resource Kits. With a little training and experience, you can develop your own WMI scripts. However all is not lost for those of you not inclined to learn the intricacies of developing a VBScript using WMI. XP includes a command line tool called WMIC which can execute WMI queries for you in a much simpler fashion. There is actually so much to explain about wmic that I can’t possibly review it all. I’m only going to discuss enough information to get you started. I urge you to look at the information for wmic in Help & Support. WMI support is included in Windows 2000 and later systems by default.

To use the tool, open a command prompt and type WMIC. The first time you run the program it does a mini-install of sorts. From there after, you will be presented with a wmic prompt (wmi:root\cli>). This is the utility’s interactive mode. To quit the program just type ‘exit’ (without the quotes). You can also execute a full query at the command line which I’ll cover at the end. Entering ‘/?’ at the prompt will display help information.

As you see, to get information, you just need to know an alias for the component you are interested in. Unlike scripting where you would need to know a specific class name, you can get by with a simple name. At the wmic prompt, type in ‘cpu’. You will get a report of cpu properties that scrolls off the screen. Typing ‘cpu list brief’ will return a subset of information. But it is still hard to read. For a better report, type ‘cpu list /format:textvaluelist’. Other formatting options include CSV (/format:csv),raw XML (/format:rawxml), an HTML table (/format:htable) or an HTML page (/format:hform). If you want to save the output to a file, you can use the /Output switch. You can send output to the Windows clipboard (/Output:clipboard) or a file (/Output:osreport.csv). Be aware that you must put the output switch first in the command. For example, at the prompt type ‘/output:osreport.htm os list full /format:hform’. You can then open the file osreport.htm in any browser. If you specify a file name with spaces, you must enclose the name in quotes ” “.

WMIC is nice but it’s real power comes in managing remote systems. At the beginning of your command, simply use /node:servername, like this:

/node:FileSrv01 /output:c:\reports\filesrv01-cpu.htm cpu list /format:hform.

If you want to query multiple remote systems, separate computer names with commas (/node:computer1,computer2,computer3). Depending on the query you might also need to pass alternate credentials. At the beginning of the command, use /User and /Password. You can’t use different credentials for local systems. Here is a more complete interactive example (this is one single line command from a wmic prompt):

/user:mydomain\Admin09 /password:[email protected] node:FileSrv01,FileSrv02
/output:c:\reports\filesrv01-cpu.htm cpu list /format:hform.

I mentioned that you can also run commands directly from the command line. All you need to do is type wmic and then the wmic command you tested interactively:

wmic /node:FileSrv01 /output:c:\reports\filesrv01-cpu.htm cpu list /format:hform.

That’s all there is to it. As I mentioned, WMIC can be a very useful utility and there is much more to it than I can cover here. In the mean time, open up a command prompt and try these commands to see for yourself. Don’t worry, no changes will be made to your system, just a report of some local resources and elements. Depending on your system, some commands will provide more values than others.

wmic cpu list /format:textvaluelist
wmic os list /format:textvaluelist
wmic /output:mypc.htm computersystem list /format:hform [open mypc.htm in browser after you exit wmic to see the results]
wmic logicaldisk list brief /format:textvaluelist
wmic csproduct list /format:textvaluelist

Technorati Tags:

wmic


Use Internet Explorer in PowerShell

Here’s a PowerShell Script that demonstates how to create COM objects in PowerShell, in this case an Internet Explorer instance. The script then takes the output of the Get-Service cmdlet and writes the results to the IE window.

# IEServiceList.ps1
# Jeffery Hicks
# http://jdhitsolutions.blogspot.com
# http://www.jdhitsolutions.com
# May 2006
#Display all running services in an Internet Explorer window

new-variable html
#create an object with the running services
$svc = get-service | where {$_.status -eq “running”}

#create a new COM object that is Internet Explorer
$oIE=New-object -COM InternetExplorer.Application

# If you want to see what Internet Explorer methods and
# properties exist, then run from within this script:
#$oIE |get-member

#Configure IE object
$oIE.navigate2(“about:blank”)
$oIE.width=400
$oIE.height=600
$oIE.Resizable=$True
$oIE.StatusBar=$True
$oIE.AddressBar=$False
$oIE.MenuBar=$False
$oIE.Toolbar=$False

#build the html code to display
foreach ($s in $svc) {$html=$html+”<font face=Verdana size=2>”+`
$s.Displayname+”: “+$s.status+”</font><br>”}

#set the body with our html code
$oIE.document.body.innerHTML=$html

#display a summary in the status bar
$oIE.StatusText=($svc.Count).ToString()+” running services”

#display the IE object
$oIE.Visible=$True

Technorati Tags: