PowerShell/Additional Topics

This lesson introduces additional PowerShell topics, including custom objects, web requests, email, and Active Directory users and groups.

Objectives and Skills

edit

After completing this lesson, you will be able to:

  • Describe basic PowerShell object concepts.
  • Create PowerShell scripts to work with .NET library objects.
  • Create PowerShell scripts to work with COM objects.
  • Create PowerShell scripts to retrieve web pages.
  • Create PowerShell scripts to send email.
  • Create PowerShell scripts to manage Active Directory users and groups.

Readings

edit
  1. Wikipedia: Object (computer science)
  2. Wikipedia: Hypertext Transfer Protocol
  3. Wikipedia: Simple Mail Transfer Protocol
  4. Wikipedia: Active Directory
  5. BonusBits: Mastering PowerShell Chapter 6 - Using Objects

Multimedia

edit
  1. YouTube: PowerShell Fundamentals - Working with Objects
  2. YouTube: PowerShell Filtering Functions and Custom Objects
  3. YouTube: Manipulate Excel Workbooks and Worksheets with Powershell
  4. YouTube: Logging Into webpage with Invoke-WebRequest
  5. YouTube: Send an Email From PowerShell
  6. YouTube: User Account Mgmt Using PowerShell

Examples

edit

New-Object

edit

The New-Object cmdlet creates an instance of a .NET Framework or COM object.[1]

$ping = New-Object System.Net.NetworkInformation.Ping
$ping.Send('8.8.8.8')

PowerShell custom objects are created using the .NET PSObject class.[2]

$bios = Get-WmiObject -Class Win32_BIOS
$computer = Get-WmiObject -Class Win32_ComputerSystem

$object = New-Object PSObject
$object | Add-Member -MemberType NoteProperty -Name 'Computer Name' -Value $bios.PSComputerName
$object | Add-Member -MemberType NoteProperty -Name 'Manufacturer' -Value $bios.Manufacturer
$object | Add-Member -MemberType NoteProperty -Name 'Model' -Value $computer.Model
$object | Add-Member -MemberType NoteProperty -Name 'BIOS Version' -Value $bios.SMBIOSBIOSVersion
$object | Add-Member -MemberType NoteProperty -Name 'RAM' -Value $computer.TotalPhysicalMemory

$object | Out-GridView

Microsoft Excel is accessed as a COM object using Excel.Application.[3]

$excel = New-Object -COMObject Excel.Application
$excel.Visible = $true
$excel.Workbooks.Add()
$excel.ActiveSheet.Cells.Item(1, 1) = 'Hello Excel!'

Invoke-WebRequest

edit

The Invoke-WebRequest cmdlet sends HTTP, HTTPS, FTP, and FILE requests to a web page or web service.[4]

$object = Invoke-WebRequest 'https://en.wikiversity.org/wiki/Windows_PowerShell'
$object.Content

Send-MailMessage

edit

The Send-MailMessage cmdlet sends an email message.[5]

$server = 'smtp.gmail.com'
$port = 587
$username = 'username'
$password = 'password'

$from = 'me@domain'
$to = 'you@domain'
$subject = 'PowerShell Email Test'
$body = 'Hello from PowerShell!'

$credential = New-Object System.Management.Automation.PSCredential($username, (ConvertTo-SecureString $password -AsPlainText -Force))

Send-MailMessage -From $from -To $to -Subject $subject -Body $body -SmtpServer $server -Port $port -Credential $credential -UseSsl

Import-Module

edit

The Import-Module cmdlet adds one or more modules to the current session.[6][7]

Import-Module ActiveDirectory

New-ADUser

edit

The New-ADUser cmdlet creates a new Active Directory user.[8]

$path = 'OU=test,DC=domain,DC=local'
$name = 'Display Name'
$username = 'username'
$password = 'password'

$password = ConvertTo-SecureString $password -AsPlainText -force
New-ADUser -Path $path -SamAccountName $username -Name $name -AccountPassword $password -Enabled $true

Get-ADUser

edit

The Get-ADUser cmdlet gets a user object or performs a search to retrieve multiple user objects.[9]

$user = Get-ADUser 'username'

Set-ADUser

edit

The Set-ADUser cmdlet modifies the properties of an Active Directory user.[10]

Set-ADUser -Identity 'username' -Enabled $false

Remove-ADUser

edit

The Remove-ADUser cmdlet removes an Active Directory user.[11]

Remove-ADUser -Identity 'username'

New-ADGroup

edit

The New-ADGroup cmdlet creates a new Active Directory group object.[12]

$path = 'OU=test,DC=domain,DC=local'
New-ADGroup -Path $path -Name 'Group Name' -GroupScope Global

Remove-ADGroup

edit

The Remove-ADGroup cmdlet removes an Active Directory group object.[13]

Remove-ADGroup -Identity 'Group Name'

Add-ADGroupMember

edit

The Add-ADGroupMember cmdlet adds one or more users, groups, service accounts, or computers as new members of an Active Directory group.[14]

Add-ADGroupMember -Identity 'Group Name' -Members 'username'

Remove-ADGroupMember

edit

The Remove-ADGroupMember cmdlet removes one or more users, groups, service accounts, or computers from an Active Directory group.[15]

Remove-ADGroupMember -Identity 'Group Name' -Members 'username'

Activities

edit
  1. Review Microsoft TechNet: Use PowerShell for Network Host and Port Discovery Sweeps. Use a for loop and the System.Net.NetworkInformation.Ping object to loop through your entire subnet and create an array of IP addresses on the subnet that respond to ping. Use Arp to identify hosts on the network that responded to the Arp request but blocked a ping response and add those hosts to the array. Then display the array.
  2. Review WindowsITPro: PowerShell Basics: Custom Objects. Create a script that uses Get-WmiObject to retrieve multiple computer properties and add those properties to a custom PSObject. Create an array of custom PSObject objects, with a separate object for each of several computers on the network. Pipe the array through Out-GridView to display the combined results.
  3. Review Learn PowerShell: PowerShell and Excel. Create a script that uses the Excel.Application COM object and nested for loops to generate a 10 x 10 multiplication table in the first worksheet in a new Excel workbook.
  4. Review Microsoft TechNet: Use PowerShell to Download Web Page Links from a Blog. Create a script that uses Invoke-WebRequest to download all links from https://en.wikiversity.org/wiki/Windows_PowerShell . Create an array of the links on the page. Use a recursive function so that the script also lists the links of any subpages of the page. Display the results using Out-GridView.
  5. Create a script to email all warning and error events from the previous 24 hours as a formatted list. Use the computer name in the from address, and include the current date in the subject. Use Task Scheduler to schedule the script to run daily at a given time, such as 6 a.m.
  6. Review Microsoft MSDN: Active Directory Module for Windows PowerShell – Quick start guide. Create a text file that lists 10 new users and the department they will work for in username, department format. Use Get-Content to read the file, New-ADUser to add the user, and Add-ADGroupMember to add the user to their department. Departments can be created manually or using New-ADGroup. Open Active Directory Users and Computers to confirm the users were created and added to their respective groups.

Lesson Summary

edit
  • An object is a location in memory having a value and possibly referenced by an identifier.[16]
  • The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems, and is the foundation of data communication for the World Wide Web.[17]
  • HTTP functions as a request-response protocol in the client-server computing model. The client submits an HTTP request message to the server. The server, which provides resources such as HTML files and other content, or performs other functions on behalf of the client, returns a response message to the client. The response contains completion status information about the request and may also contain requested content in its message body.[18]
  • Simple Mail Transfer Protocol (SMTP) is an Internet standard for electronic mail (e-mail) transmission.[19]
  • SMTP by default uses TCP port 25. The protocol for mail submission is the same, but uses port 587.[20]
  • While electronic mail servers and other mail transfer agents use SMTP to send and receive mail messages, user-level client mail applications typically use SMTP only for sending messages to a mail server for relaying. For receiving messages, client applications usually use either POP3 or IMAP.[21]
  • Active Directory (AD) is a directory service that Microsoft developed for Windows domain networks and is included in most Windows Server operating systems as a set of processes and services.[22]
  • An Active Directory domain controller authenticates and authorizes all users and computers in a Windows domain type network.[23]
  • An Active Directory structure is an arrangement of information about objects. The objects fall into two broad categories: resources (e.g., printers) and security principals (user or computer accounts and groups).[24]
  • The New-Object cmdlet creates an instance of a .NET Framework or COM object.[25]
  • PowerShell custom objects are created using the .NET PSObject class.[26]
  • Microsoft Excel is accessed as a COM object using Excel.Application.[27]
  • The Invoke-WebRequest cmdlet sends HTTP, HTTPS, FTP, and FILE requests to a web page or web service.[28]
  • The Send-MailMessage cmdlet sends an email message.[29]
  • The Import-Module cmdlet adds one or more modules to the current session.[30]
  • The New-ADUser cmdlet creates a new Active Directory user.[31]
  • The Get-ADUser cmdlet gets a user object or performs a search to retrieve multiple user objects.[32]
  • The Set-ADUser cmdlet modifies the properties of an Active Directory user.[33]
  • The Remove-ADUser cmdlet removes an Active Directory user.[34]
  • The New-ADGroup cmdlet creates a new Active Directory group object.[35]
  • The Remove-ADGroup cmdlet removes an Active Directory group object.[36]
  • The Add-ADGroupMember cmdlet adds one or more users, groups, service accounts, or computers as new members of an Active Directory group.[37]
  • The Remove-ADGroupMember cmdlet removes one or more users, groups, service accounts, or computers from an Active Directory group.[38]

Key Terms

edit
Component Object Model (COM)
A binary-interface standard for software components introduced by Microsoft in 1993, which is used to enable inter-process communication and dynamic object creation in a large range of programming languages.[39]
HTML (HyperText Markup Language)
The standard markup language used to create web pages.[40]
Post Office Protocol (POP)
An application-layer Internet standard protocol used by local e-mail clients to retrieve e-mail from a remote server over a TCP/IP connection.[41]
Internet Message Access Protocol (IMAP)
A protocol for e-mail retrieval and storage which specifically allows multiple clients simultaneously connected to the same mailbox.[42]

Review Questions

edit
Enable JavaScript to hide answers.
Click on a question to see the answer.
1. An object is _____.
An object is a location in memory having a value and possibly referenced by an identifier.
2. The Hypertext Transfer Protocol (HTTP) is _____.
The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems, and is the foundation of data communication for the World Wide Web.
3. HTTP functions as a _____ protocol in the client-server computing model. The client submits _____ to the server. The server _____ to the client.
HTTP functions as a request-response protocol in the client-server computing model. The client submits an HTTP request message to the server. The server, which provides resources such as HTML files and other content, or performs other functions on behalf of the client, returns a response message to the client.
4. The HTTP response contains _____.
The HTTP response contains completion status information about the request and may also contain requested content in its message body.
5. Simple Mail Transfer Protocol (SMTP) is _____.
Simple Mail Transfer Protocol (SMTP) is an Internet standard for electronic mail (e-mail) transmission.
6. SMTP by default uses TCP port _____. The protocol for mail submission is the same, but uses port _____.
SMTP by default uses TCP port 25. The protocol for mail submission is the same, but uses port 587.
7. While electronic mail servers and other mail transfer agents use SMTP to send and receive mail messages, user-level client mail applications typically use SMTP only for _____. For receiving messages, client applications usually use either _____ or _____.
While electronic mail servers and other mail transfer agents use SMTP to send and receive mail messages, user-level client mail applications typically use SMTP only for sending messages to a mail server for relaying. For receiving messages, client applications usually use either POP3 or IMAP.
8. Active Directory (AD) is _____ and is included in _____ as _____.
Active Directory (AD) is a directory service that Microsoft developed for Windows domain networks and is included in most Windows Server operating systems as a set of processes and services.
9. An Active Directory domain controller _____.
An Active Directory domain controller authenticates and authorizes all users and computers in a Windows domain type network.
10. An Active Directory structure is _____.
An Active Directory structure is an arrangement of information about objects.
11. Active Directory objects fall into two broad categories: _____.
Active Directory objects fall into two broad categories: resources (e.g., printers) and security principals (user or computer accounts and groups).
12. The New-Object cmdlet _____.
The New-Object cmdlet creates an instance of a .NET Framework or COM object.
13. PowerShell custom objects are created using _____.
PowerShell custom objects are created using the .NET PSObject class.
14. Microsoft Excel is accessed as _____.
Microsoft Excel is accessed as a COM object using Excel.Application.
15. The Invoke-WebRequest cmdlet _____.
The Invoke-WebRequest cmdlet sends HTTP, HTTPS, FTP, and FILE requests to a web page or web service.
16. The Send-MailMessage cmdlet _____.
The Send-MailMessage cmdlet sends an email message.
17. The Import-Module cmdlet _____.
The Import-Module cmdlet adds one or more modules to the current session.
18. The New-ADUser cmdlet _____.
The New-ADUser cmdlet creates a new Active Directory user.
19. The Get-ADUser cmdlet _____.
The Get-ADUser cmdlet gets a user object or performs a search to retrieve multiple user objects.
20. The Set-ADUser cmdlet _____.
The Set-ADUser cmdlet modifies the properties of an Active Directory user.
21. The Remove-ADUser cmdlet _____.
The Remove-ADUser cmdlet removes an Active Directory user.
22. The New-ADGroup cmdlet _____.
The New-ADGroup cmdlet creates a new Active Directory group object.
23. The Remove-ADGroup cmdlet _____.
The Remove-ADGroup cmdlet removes an Active Directory group object.
24. The Add-ADGroupMember cmdlet _____.
The Add-ADGroupMember cmdlet adds one or more users, groups, service accounts, or computers as new members of an Active Directory group.
25. The Remove-ADGroupMember cmdlet _____.
The Remove-ADGroupMember cmdlet removes one or more users, groups, service accounts, or computers from an Active Directory group.

Assessments

edit

See Also

edit

References

edit
  Type classification: this is a lesson resource.
  Completion status: this resource is considered to be complete.
  1. Microsoft TechNet: New-Object
  2. WindowsITPro: PowerShell Basics: Custom Objects
  3. Learn PowerShell: PowerShell and Excel
  4. Microsoft TechNet: Invoke-WebRequest
  5. Microsoft TechNet: Send-MailMessage
  6. Microsoft TechNet: Import-Module
  7. Microsoft TechNet: Active Directory Administration with Windows PowerShell
  8. Microsoft TechNet: New-ADUser
  9. Microsoft TechNet: Get-ADUser
  10. Microsoft TechNet: Set-ADUser
  11. Microsoft TechNet: Remove-ADUser
  12. Microsoft TechNet: New-ADGroup
  13. Microsoft TechNet: Remove-ADGroup
  14. Microsoft TechNet: Add-ADGroupMember
  15. Microsoft TechNet: Remove-ADGroupMember
  16. Wikipedia: Object (computer science)
  17. Wikipedia: Hypertext Transfer Protocol
  18. Wikipedia: Hypertext Transfer Protocol
  19. Wikipedia: Simple Mail Transfer Protocol
  20. Wikipedia: Simple Mail Transfer Protocol
  21. Wikipedia: Simple Mail Transfer Protocol
  22. Wikipedia: Active Directory
  23. Wikipedia: Active Directory
  24. Wikipedia: Active Directory
  25. Microsoft TechNet: New-Object
  26. WindowsITPro: PowerShell Basics: Custom Objects
  27. Learn PowerShell: PowerShell and Excel
  28. Microsoft TechNet: Invoke-WebRequest
  29. Microsoft TechNet: Send-MailMessage
  30. Microsoft TechNet: Import-Module
  31. Microsoft TechNet: New-ADUser
  32. Microsoft TechNet: Get-ADUser
  33. Microsoft TechNet: Set-ADUser
  34. Microsoft TechNet: Remove-ADUser
  35. Microsoft TechNet: New-ADGroup
  36. Microsoft TechNet: Remove-ADGroup
  37. Microsoft TechNet: Add-ADGroupMember
  38. Microsoft TechNet: Remove-ADGroupMember
  39. Wikipedia: Component Object Model
  40. Wikipedia: HTML
  41. Wikipedia: Post Office Protocol
  42. Wikipedia: Internet Message Access Protocol