Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Give 3 professional examples of powershell scripts , with detail comments and code, that shows "how to work with .NET Classes and GUI interactions".
- # 1. Example 1: Creating a GUI form using .NET classes
- #This PowerShell script demonstrates how to create a simple GUI form using .NET classes. The form contains a label, text box, and button. When the button is clicked, the text entered in the text box is displayed in the label.
- # Load .NET assembly for creating GUI forms
- Add-Type -AssemblyName System.Windows.Forms
- # Create a new form object
- $form = New-Object System.Windows.Forms.Form
- # Set form properties
- $form.Text = "My Form"
- $form.Size = New-Object System.Drawing.Size(300, 200)
- # Create label object and add to form
- $label = New-Object System.Windows.Forms.Label
- $label.Location = New-Object System.Drawing.Point(10, 10)
- $label.Text = "Enter text:"
- $form.Controls.Add($label)
- # Create text box object and add to form
- $textbox = New-Object System.Windows.Forms.TextBox
- $textbox.Location = New-Object System.Drawing.Point(10, 30)
- $form.Controls.Add($textbox)
- # Create button object and add to form
- $button = New-Object System.Windows.Forms.Button
- $button.Location = New-Object System.Drawing.Point(10, 60)
- $button.Text = "Display Text"
- $form.Controls.Add($button)
- # Add event handler for button click event
- $button.Add_Click({
- $label.Text = $textbox.Text
- })
- # Show the form
- $form.ShowDialog()
- 2. Example 2: Using .NET classes to interact with Windows Registry
- This PowerShell script demonstrates how to use .NET classes to read and write values in the Windows Registry.
- # Load .NET assembly for working with registry keys/values
- Add-Type -AssemblyName Microsoft.Win32.Registry
- # Define registry key path and value name
- $keyPath = "HKCU:\Software\MyApp"
- $valueName = "MyValue"
- # Check if registry key exists; create it if it doesn't exist
- if (!(Test-Path $keyPath)) {
- [Microsoft.Win32.Registry]::CurrentUser.CreateSubKey("Software\MyApp")
- }
- # Write value to registry key
- [Microsoft.Win32.Registry]::SetValue($keyPath, $valueName, "Hello World")
- # Read value from registry key and display it in console output
- $value = [Microsoft.Win32.Registry]::GetValue($keyPath, $valueName)
- Write-Host "Value read from registry: $value"
- 3. Example 3: Using .NET classes to interact with Active Directory
- This PowerShell script demonstrates how to use .NET classes to query Active Directory for user information.
- # Load .NET assembly for working with Active Directory objects/classes
- Add-Type -AssemblyName System.DirectoryServices.AccountManagement
- # Define domain name and user name to search for
- $domainName = "mydomain.local"
- $userName = "jdoe"
- # Create principal context object for domain authentication/authorization
- $contextType =[System.DirectoryServices.AccountManagement.ContextType]::Domain
- $principalContext =[System.DirectoryServices.AccountManagement.PrincipalContext]::new($contextType,$domainName)
- if ($principalContext) {
- # Find user by username
- $userPrincipal =[System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($principalContext,$userName)
- if ($userPrincipal) {
- # Display user properties in console output
- Write-Host "User found: $($userPrincipal.Name)"
- Write-Host "Email address: $($userPrincipal.EmailAddress)"
- Write-Host "Last logon date: $($userPrincipal.LastLogonDate)"
- }
- else {
- Write-Host "User not found."
- }
- }
- else {
- Write-Host "Unable to connect to domain."
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement