Created
September 1, 2016 14:55
-
-
Save Stephanevg/1ce99ba13da2e183541d168e6863790f to your computer and use it in GitHub Desktop.
Code for powershell classes tutorial part1 -> http://powershelldistrict.com/a-practical-case-for-using-powershell-classes/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class Computer { | |
[String]$Name | |
[String]$Type | |
[string]$Description | |
[string]$owner | |
[string]$Model | |
[int]$Reboots | |
[void]Reboot(){ | |
$this.Reboots ++ | |
} | |
Computer ([string]$Name){ | |
if ($comp = Get-ADComputer -filter "name -eq '$Name'" -Properties * -ErrorAction SilentlyContinue){ | |
$this.name =$Name | |
$this.Description = $Comp.Description | |
switch -wildcard ($comp.OperatingSystem){ | |
('*Server*') {$this.Type = 'Server';Break} | |
('*workstation*') {$this.Type = 'Workstation'} | |
('*Laptop*') {$this.Type = 'Laptop';Break} | |
default { $this.Type = 'N/A'} | |
} | |
$this.owner = $comp.ManagedBy.Split(',')[0].replace('CN=','') | |
}else{ | |
write-verbose "Could Not find $($this.name)" | |
} | |
} | |
Computer ([string]$Name, [String]$type,[string]$Description,[string]$owner,[String]$Model){ | |
if (!($comp = Get-ADComputer -filter "name -eq '$Name'" -Properties name)){ | |
if ($user = Get-ADUser -Filter "name -eq '$owner'"){ | |
try{ | |
New-ADComputer -Name $Name -Description $Description -ManagedBy $user -ErrorAction Stop | |
$this.Name = $Name | |
$this.Type = $type | |
$this.Description = $Description | |
$this.owner = $owner | |
}catch{ | |
$_ | |
} | |
}else{ | |
write-warning "the user $($Owner) is not existing. Please verify and try again." | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment