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 | edit source]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 | edit source]- Wikipedia: Object (computer science)
- Wikipedia: Hypertext Transfer Protocol
- Wikipedia: Simple Mail Transfer Protocol
- Wikipedia: Active Directory
- BonusBits: Mastering PowerShell Chapter 6 - Using Objects
Multimedia
[edit | edit source]- YouTube: PowerShell Fundamentals - Working with Objects
- YouTube: PowerShell Filtering Functions and Custom Objects
- YouTube: Manipulate Excel Workbooks and Worksheets with Powershell
- YouTube: Logging Into webpage with Invoke-WebRequest
- YouTube: Send an Email From PowerShell
- YouTube: User Account Mgmt Using PowerShell
Examples
[edit | edit source]New-Object
[edit | edit source]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 | edit source]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 | edit source]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 | edit source]The Import-Module cmdlet adds one or more modules to the current session.[6][7]
Import-Module ActiveDirectory
New-ADUser
[edit | edit source]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 | edit source]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 | edit source]The Set-ADUser cmdlet modifies the properties of an Active Directory user.[10]
Set-ADUser -Identity 'username' -Enabled $false
Remove-ADUser
[edit | edit source]The Remove-ADUser cmdlet removes an Active Directory user.[11]
Remove-ADUser -Identity 'username'
New-ADGroup
[edit | edit source]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 | edit source]The Remove-ADGroup cmdlet removes an Active Directory group object.[13]
Remove-ADGroup -Identity 'Group Name'
Add-ADGroupMember
[edit | edit source]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 | edit source]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 | edit source]- 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.
- 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.
- 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.
- 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.
- 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.
- 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 | edit source]- 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 | edit source]- 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 | edit source]Assessments
[edit | edit source]- Flashcards: Quizlet: Windows PowerShell - Additional Topics
- Quiz: Quizlet: Windows PowerShell - Additional Topics
See Also
[edit | edit source]References
[edit | edit source]- ↑ Microsoft TechNet: New-Object
- ↑ WindowsITPro: PowerShell Basics: Custom Objects
- ↑ Learn PowerShell: PowerShell and Excel
- ↑ Microsoft TechNet: Invoke-WebRequest
- ↑ Microsoft TechNet: Send-MailMessage
- ↑ Microsoft TechNet: Import-Module
- ↑ Microsoft TechNet: Active Directory Administration with Windows PowerShell
- ↑ Microsoft TechNet: New-ADUser
- ↑ Microsoft TechNet: Get-ADUser
- ↑ Microsoft TechNet: Set-ADUser
- ↑ Microsoft TechNet: Remove-ADUser
- ↑ Microsoft TechNet: New-ADGroup
- ↑ Microsoft TechNet: Remove-ADGroup
- ↑ Microsoft TechNet: Add-ADGroupMember
- ↑ Microsoft TechNet: Remove-ADGroupMember
- ↑ Wikipedia: Object (computer science)
- ↑ Wikipedia: Hypertext Transfer Protocol
- ↑ Wikipedia: Hypertext Transfer Protocol
- ↑ Wikipedia: Simple Mail Transfer Protocol
- ↑ Wikipedia: Simple Mail Transfer Protocol
- ↑ Wikipedia: Simple Mail Transfer Protocol
- ↑ Wikipedia: Active Directory
- ↑ Wikipedia: Active Directory
- ↑ Wikipedia: Active Directory
- ↑ Microsoft TechNet: New-Object
- ↑ WindowsITPro: PowerShell Basics: Custom Objects
- ↑ Learn PowerShell: PowerShell and Excel
- ↑ Microsoft TechNet: Invoke-WebRequest
- ↑ Microsoft TechNet: Send-MailMessage
- ↑ Microsoft TechNet: Import-Module
- ↑ Microsoft TechNet: New-ADUser
- ↑ Microsoft TechNet: Get-ADUser
- ↑ Microsoft TechNet: Set-ADUser
- ↑ Microsoft TechNet: Remove-ADUser
- ↑ Microsoft TechNet: New-ADGroup
- ↑ Microsoft TechNet: Remove-ADGroup
- ↑ Microsoft TechNet: Add-ADGroupMember
- ↑ Microsoft TechNet: Remove-ADGroupMember
- ↑ Wikipedia: Component Object Model
- ↑ Wikipedia: HTML
- ↑ Wikipedia: Post Office Protocol
- ↑ Wikipedia: Internet Message Access Protocol