Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #create class and object
- Class Admailbox
- {
- [String]$name
- [int]$ID
- }
- $AdmailboxObj= New-Object -TypeName Admailbox #1stway of creating object
- #2nd way --> [Admailbox]::new()
- #3rd way is using hashtable -->[Admailbox]@{name="",Total=""}
- $AdmailboxObj.id= 23
- $AdmailboxObj.name= "Satish"
- $AdmailboxObj | Export-Csv C:\Users\Administrator\Desktop\Newfolder\classobj.csv
- $AdmailboxObj.name | gm #It will return string as type as we defined it in the beginning
- $AdmailboxObj.ID | gm #It will return Int as type as we defined it in the beginning
- ############################################################################################
- #Two types of methods
- 1. Return some value(like above example)
- 2. Do not return any value (use void as return type)
- ##############################################################################################
- # Take multiple services in Array and try to stop it. Create two files with success and error message.
- $services = @("BITS","NG","Test")
- foreach($service in $services)
- {
- Stop-Service $service -ErrorAction Continue -ErrorVariable XYZ
- if($XYZ -ne $NULL)
- {
- "Not able to stop the $service because $($XYZ.categoryinfo.Reason)" | out-file C:\Users\Administrator\Desktop\Newfolder\error.txt -Append
- }
- else
- {
- "$service was successfully stopped" | out-file C:\Users\Administrator\Desktop\Newfolder\success.txt -Append
- }
- }
- # Use try Catch block below:
- try
- {
- Stop-Service -name dinesh -ErrorAction Stop
- }
- catch
- {
- $_.exception.message | Out-File C:\catch.txt
- }
- Finally
- {
- }
- #####################################TASK#####################################TASK##########################
- Class Students {
- [string]$Fname
- [string]$Lname
- [string]FullName()
- {
- return "$($this.Fname)*$($this.Lname)"
- }
- }
- $student1=[Students]::new()
- $student1.Fname="Dinesh"
- $student1.Lname="Patil"
- #$student1.Lname=3
- #$student1.Fname=3
- $student1.FullName()
- Task1: what will be output for fullname for the below inputs? and why?
- $student1.Fname="Dinesh"
- $student1.Lname=3
- $student1.FullName() = ?
- $student1.Fname=3
- $student1.Lname="Patil"
- $student1.FullName() = ?
- ***Solution***
- $student1.Fname="Dinesh"
- $student1.Lname=3
- $student1.FullName() = Dinesh*3
- As the return type of the class is string it will return string with * in between as it is part of the string.
- $student1.Fname=3
- $student1.Lname="Patil"
- $student1.FullName() = 3*Patil
- same as above.
- Without "" in return statement it will give "Invalid cast from string to integer error".
- Task:2----------
- Create class calculator which will accept two inputs
- $firstnumber
- $secondnumber
- add methods of add, subtract, multiply, divide .
- 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.
- Check and select the attributes from the below help file
- get-help about_Functions_Advanced_Param* -ShowWindow
- Class calculator
- {
- [int] $firstnumber
- [int] $secondnumber
- [int]Add()
- {
- return $this.firstnumber + $this.secondnumber
- }
- [int]Sub()
- {
- return $this.firstnumber-$this.secondnumber
- }
- [int]Mul()
- {
- return $this.firstnumber*$this.secondnumber
- }
- [float]Div()
- {
- return $this.firstnumber/$this.secondnumber
- }
- }
- $calc= New-Object -TypeName calculator
- $firstnumber= Read-Host "Enter a number between 1 to 10"
- $secondnumber= Read-Host "Enter a number between 1 to 10"
- 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 )
- {
- $calc.firstnumber= $firstnumber
- $calc.secondnumber= $secondnumber
- $calc.Add()
- $calc.Sub()
- $calc.Mul()
- $calc.Div()
- }
- else
- {
- Write-Host "Enter numbers between 1 and 10 only"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement