Advertisement
wetyukmnbxc

Give 3 professional examples of powershell scripts , with detail comments and code,  that shows "how

Mar 21st, 2023
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Give 3 professional examples of powershell scripts , with detail comments and code,  that shows "how to work with .NET Classes and GUI interactions".
  2.  
  3. # 1. Example 1: Creating a GUI form using .NET classes
  4.  
  5. #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.
  6.  
  7. # Load .NET assembly for creating GUI forms
  8. Add-Type -AssemblyName System.Windows.Forms
  9.  
  10. # Create a new form object
  11. $form = New-Object System.Windows.Forms.Form
  12.  
  13. # Set form properties
  14. $form.Text = "My Form"
  15. $form.Size = New-Object System.Drawing.Size(300, 200)
  16.  
  17. # Create label object and add to form
  18. $label = New-Object System.Windows.Forms.Label
  19. $label.Location = New-Object System.Drawing.Point(10, 10)
  20. $label.Text = "Enter text:"
  21. $form.Controls.Add($label)
  22.  
  23. # Create text box object and add to form
  24. $textbox = New-Object System.Windows.Forms.TextBox
  25. $textbox.Location = New-Object System.Drawing.Point(10, 30)
  26. $form.Controls.Add($textbox)
  27.  
  28. # Create button object and add to form
  29. $button = New-Object System.Windows.Forms.Button
  30. $button.Location = New-Object System.Drawing.Point(10, 60)
  31. $button.Text = "Display Text"
  32. $form.Controls.Add($button)
  33.  
  34. # Add event handler for button click event
  35. $button.Add_Click({
  36.     $label.Text = $textbox.Text
  37. })
  38.  
  39. # Show the form
  40. $form.ShowDialog()
  41.  
  42. 2. Example 2: Using .NET classes to interact with Windows Registry
  43.  
  44. This PowerShell script demonstrates how to use .NET classes to read and write values in the Windows Registry.
  45.  
  46. # Load .NET assembly for working with registry keys/values
  47. Add-Type -AssemblyName Microsoft.Win32.Registry
  48.  
  49. # Define registry key path and value name
  50. $keyPath = "HKCU:\Software\MyApp"
  51. $valueName = "MyValue"
  52.  
  53. # Check if registry key exists; create it if it doesn't exist
  54. if (!(Test-Path $keyPath)) {
  55.     [Microsoft.Win32.Registry]::CurrentUser.CreateSubKey("Software\MyApp")
  56. }
  57.  
  58. # Write value to registry key
  59. [Microsoft.Win32.Registry]::SetValue($keyPath, $valueName, "Hello World")
  60.  
  61. # Read value from registry key and display it in console output
  62. $value = [Microsoft.Win32.Registry]::GetValue($keyPath, $valueName)
  63. Write-Host "Value read from registry: $value"
  64.  
  65. 3. Example 3: Using .NET classes to interact with Active Directory
  66.  
  67. This PowerShell script demonstrates how to use .NET classes to query Active Directory for user information.
  68.  
  69. # Load .NET assembly for working with Active Directory objects/classes
  70. Add-Type -AssemblyName System.DirectoryServices.AccountManagement
  71.  
  72. # Define domain name and user name to search for
  73. $domainName = "mydomain.local"
  74. $userName = "jdoe"
  75.  
  76. # Create principal context object for domain authentication/authorization
  77. $contextType =[System.DirectoryServices.AccountManagement.ContextType]::Domain
  78. $principalContext =[System.DirectoryServices.AccountManagement.PrincipalContext]::new($contextType,$domainName)
  79.  
  80. if ($principalContext) {
  81.     # Find user by username
  82.     $userPrincipal =[System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($principalContext,$userName)
  83.  
  84.     if ($userPrincipal) {
  85.         # Display user properties in console output
  86.         Write-Host "User found: $($userPrincipal.Name)"
  87.         Write-Host "Email address: $($userPrincipal.EmailAddress)"
  88.         Write-Host "Last logon date: $($userPrincipal.LastLogonDate)"
  89.     }
  90.     else {
  91.         Write-Host "User not found."
  92.     }
  93. }
  94. else {
  95.     Write-Host "Unable to connect to domain."
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement