Here is another script that I created while trying to hone my PowerShell skills. It prompts you for the FQDN of the computer that you want to gather information on, asks you whether you want to see the output on the screen or dump it into a text file, and then runs a bunch of WMI queries to get a good general overview of the machine in question.
# Function Definition: fnCompInfoGatherExecute
Function fnCompInfoGatherExecute
{
# Prompt for FQDN of Computer to be Queried
"Enter the FQDN of the computer you wish to query."
$CompName = Read-Host " "
# Prompt for Output Method
" "
"How would you like the output to be handled?"
$MenuTopLevel =
'
[1] Display on Screen
[2] Output to File
[3] Exit
Choice'
# Define Logic for Top Level Menu
switch (Read-Host $MenuTopLevel)
{
1 {
# Define WMI Information to be Retrieved
Get-WmiObject Win32_BIOS -ComputerName "$CompName"
Get-WmiObject Win32_ComputerSystem -ComputerName "$CompName"
Get-WmiObject Win32_DiskDrive -ComputerName "$CompName"
Get-WmiObject Win32_DiskPartition -ComputerName "$CompName"
Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName "$CompName"
Get-WmiObject Win32_Processor -ComputerName "$CompName"
}
2 {
# Prompt for Output Path
" "
"Enter the path to the output file (i.e. C:\Output.txt)."
$OutputPath = Read-Host " "
# Define WMI Information to be Retrieved
Get-WmiObject Win32_BIOS -ComputerName "$CompName" >> "$OutputPath"
Get-WmiObject Win32_ComputerSystem -ComputerName "$CompName" >> "$OutputPath"
Get-WmiObject Win32_DiskDrive -ComputerName "$CompName" >> "$OutputPath"
Get-WmiObject Win32_DiskPartition -ComputerName "$CompName" >> "$OutputPath"
Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName "$CompName" >> "$OutputPath"
Get-WmiObject Win32_Processor -ComputerName "$CompName" >> "$OutputPath"
# Open Output File
C:\Windows\notepad.exe "$OutputPath"
}
3 {Exit}
default {"You have chosen an invalid option"; fnPause; fnADSearchMenu}
}
}
# Call fnCompInfoGatherExecute Function
fnCompInfoGatherExecute
Richard PowerShell, System Administration PowerShell
Here is a simple PowerShell script I wrote to gather either computer, group, or user information depending on your choices. This could easily be done with much simpler scripts, but I wanted to use it as more of a learning process in order to better understand AD queries, variable expansion, functions, and other PowerShell features.
# Function Definition: fnPause
Function fnPause ($message="Press any key to continue...")
{
Write-Host -NoNewLine $Message
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host ""
Main_Menu
}
# Function Definition: fnADSearchMenu
Function fnADSearchMenu
{
# Display Welcome Message
Clear-Host
"Active Directory Search Script v1.0"
"Created By: Richard Raseley (Richard@Raseley.com)"
" "
# Display Top Level Menu Question
"What type of Active Directory object would you like to search for?"
# Display Top Level Menu
$MenuTopLevel =
'
[1] User(s)
[2] Group(s)
[3] Computer(s)
[4] Exit
Choice'
# Define Logic for Top Level Menu
switch (Read-Host $MenuTopLevel)
{
1 {fnUserSearchMenu}
2 {fnGroupSearchMenu}
3 {fnComputerSearchMenu}
4 {Exit}
default {"You have chosen an invalid option"; fnPause; fnADSearchMenu}
}
}
# Function Definition: fnUserSearchMenu
Function fnUserSearchMenu
{
# Display User Search Menu Question
" "
"What type of user information would you like?"
# Display User Search Menu
$MenuUserSearch =
'
[1] Summary of all users in the current domain
[2] Return to the main menu
Choice'
# Define Logic for User Search Menu
switch (Read-Host $MenuUserSearch)
{
1 {
#Define LDAP Filter
$LDAPFilter = "(objectCategory=User)"
#Call fnADSearchExecute
fnADSearchExecute
}
2 {
fnADSearchMenu
}
default {"You have chosen an invalid option"; fnPause; fnADSearchMenu}
}
}
# Function Definition: fnGroupSearchMenu
Function fnGroupSearchMenu
{
# Display Group Search Menu Question
" "
"What type of group information would you like?"
# Display Group Search Menu
$MenuGoupSearch =
'
[1] Summary of all groups in the current domain
[2] Return to main menu
Choice'
# Define Logic for Group Search Menu
switch (Read-Host $MenuGoupSearch)
{
1 {
# Define LDAP Filter
$LDAPFilter = "(objectCategory=Group)"
#Call fnADSearchExecute
fnADSearchExecute
}
2 {
fnADSearchMenu
}
default {"You have chosen an invalid option"; fnPause; fnADSearchMenu}
}
}
# Function Definition: fnComputerSearchMenu
Function fnComputerSearchMenu
{
# Display Computer Search Menu Question
" "
"What type of computer information would you like?"
# Display Group Search Menu
$MenuComputerSearch =
'
[1] Summary of all computers in the current domain
[2] Return to main menu
Choice'
# Define Logic for Group Search Menu
switch (Read-Host $MenuComputerSearch)
{
1 {
# Define LDAP Filter
$LDAPFilter = "(objectCategory=Computer)"
#Call fnADSearchExecute
fnADSearchExecute
}
2 {
fnADSearchMenu
}
default {"You have chosen an invalid option"; fnPause; fnADSearchMenu}
}
}
# Function Definition: fnADSearchExecute
Function fnADSearchExecute
{
# Define AD Search Filter
$strFilter = "$LDAPFilter"
# Define AD Location for Search
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
# Define AD Search Parameters
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
# Define AD Properties Returned by Search
$colProplist = "name"
foreach ($i in $colProplist){$objSearcher.PropertiesToLoad.Add($i)}
# Execute AD Search
$colResults = $objSearcher.FindAll()
# Format AD Search Results
foreach ($objResult in $colResults)
{
$objItem = $objResult.Properties
"Name: " + $objItem.name
" "
}
}
# Call ADSearch Menu Function
fnADSearchMenu
Richard Active Directory, PowerShell Active Directory, PowerShell, Scripting
Recent Comments