Guest User

Untitled

a guest
Sep 11th, 2019
88
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #create class and object
  2. Class Admailbox
  3. {
  4. [String]$name
  5. [int]$ID
  6.  
  7. }
  8. $AdmailboxObj= New-Object -TypeName Admailbox #1stway of creating object
  9. #2nd way --> [Admailbox]::new()
  10.  
  11. #3rd way is using hashtable -->[Admailbox]@{name="",Total=""}
  12. $AdmailboxObj.id= 23
  13. $AdmailboxObj.name= "Satish"
  14. $AdmailboxObj | Export-Csv C:\Users\Administrator\Desktop\Newfolder\classobj.csv
  15.  
  16. $AdmailboxObj.name | gm #It will return string as type as we defined it in the beginning
  17. $AdmailboxObj.ID | gm #It will return Int as type as we defined it in the beginning
  18.  
  19. ############################################################################################
  20.  
  21.  
  22.  
  23. #Two types of methods
  24. 1. Return some value(like above example)
  25. 2. Do not return any value (use void as return type)
  26. ##############################################################################################
  27. # Take multiple services in Array and try to stop it. Create two files with success and error message.
  28. $services = @("BITS","NG","Test")
  29. foreach($service in $services)
  30. {
  31. Stop-Service $service -ErrorAction Continue -ErrorVariable XYZ
  32.  
  33. if($XYZ -ne $NULL)
  34. {
  35. "Not able to stop the $service because $($XYZ.categoryinfo.Reason)" | out-file C:\Users\Administrator\Desktop\Newfolder\error.txt -Append
  36. }
  37. else
  38. {
  39. "$service was successfully stopped" | out-file C:\Users\Administrator\Desktop\Newfolder\success.txt -Append
  40. }
  41.  
  42. }
  43.  
  44.  
  45.  
  46. # Use try Catch block below:
  47.  
  48. try
  49. {
  50.  
  51.  
  52. Stop-Service -name dinesh -ErrorAction Stop
  53. }
  54. catch
  55. {
  56.  
  57. $_.exception.message | Out-File C:\catch.txt
  58.  
  59. }
  60.  
  61. Finally
  62. {
  63.  
  64. }
  65.  
  66. #####################################TASK#####################################TASK##########################
  67. Class Students {
  68. [string]$Fname
  69. [string]$Lname
  70.  
  71. [string]FullName()
  72.  
  73. {
  74. return "$($this.Fname)*$($this.Lname)"
  75. }
  76. }
  77.  
  78. $student1=[Students]::new()
  79.  
  80. $student1.Fname="Dinesh"
  81. $student1.Lname="Patil"
  82. #$student1.Lname=3
  83. #$student1.Fname=3
  84. $student1.FullName()
  85.  
  86.  
  87. Task1: what will be output for fullname for the below inputs? and why?
  88.  
  89. $student1.Fname="Dinesh"
  90. $student1.Lname=3
  91. $student1.FullName() = ?
  92.  
  93. $student1.Fname=3
  94. $student1.Lname="Patil"
  95. $student1.FullName() = ?
  96.  
  97.  
  98. ***Solution***
  99. $student1.Fname="Dinesh"
  100. $student1.Lname=3
  101. $student1.FullName() = Dinesh*3
  102.  
  103. As the return type of the class is string it will return string with * in between as it is part of the string.
  104.  
  105. $student1.Fname=3
  106. $student1.Lname="Patil"
  107. $student1.FullName() = 3*Patil
  108.  
  109. same as above.
  110.  
  111. Without "" in return statement it will give "Invalid cast from string to integer error".
  112.  
  113. Task:2----------
  114. Create class calculator which will accept two inputs
  115.  
  116. $firstnumber
  117. $secondnumber
  118.  
  119. add methods of add, subtract, multiply, divide .
  120.  
  121. make sure the input provided for the numbers should not be empty string or zero and should be integer only values of both should be between 1 to 10.
  122.  
  123. Check and select the attributes from the below help file
  124. get-help about_Functions_Advanced_Param* -ShowWindow
  125.  
  126.  
  127. Class calculator
  128.  
  129. {
  130. [int] $firstnumber
  131. [int] $secondnumber
  132.  
  133. [int]Add()
  134. {
  135. return $this.firstnumber + $this.secondnumber
  136.  
  137. }
  138.  
  139. [int]Sub()
  140. {
  141. return $this.firstnumber-$this.secondnumber
  142. }
  143.  
  144. [int]Mul()
  145. {
  146. return $this.firstnumber*$this.secondnumber
  147. }
  148.  
  149. [float]Div()
  150. {
  151. return $this.firstnumber/$this.secondnumber
  152. }
  153.  
  154. }
  155.  
  156. $calc= New-Object -TypeName calculator
  157.  
  158. $firstnumber= Read-Host "Enter a number between 1 to 10"
  159. $secondnumber= Read-Host "Enter a number between 1 to 10"
  160.  
  161. if($firstnumber.Length -gt 0 -and $firstnumber -gt 0 -and $firstnumber -lt 10 -and $secondnumber.Length -gt 0 -and $secondnumber -gt 0 -and $secondnumber -lt 10 )
  162. {
  163.  
  164. $calc.firstnumber= $firstnumber
  165. $calc.secondnumber= $secondnumber
  166. $calc.Add()
  167. $calc.Sub()
  168. $calc.Mul()
  169. $calc.Div()
  170.  
  171. }
  172.  
  173. else
  174. {
  175.  
  176. Write-Host "Enter numbers between 1 and 10 only"
  177.  
  178. }
RAW Paste Data