Created
September 1, 2016 20:45
-
-
Save Stephanevg/c1ff041b608fafe286e9502db18ef396 to your computer and use it in GitHub Desktop.
Contains powershell class example with enum, constructor overloads, logic using enum types.
This file contains hidden or 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
| Enum ServerType { | |
| HyperV | |
| Sharepoint | |
| Exchange | |
| Lync | |
| Web | |
| ConfigMgr | |
| } | |
| Class Computer { | |
| [String]$Name | |
| [String]$Type | |
| [string]$Description | |
| [string]$owner | |
| [string]$Model | |
| [int]$Reboots | |
| [void]Reboot(){ | |
| $this.Reboots ++ | |
| } | |
| #constructors | |
| 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 ([ServerType]$type,[string]$Description,[string]$owner,[String]$Model){ | |
| if ($user = Get-ADUser -Filter "name -eq '$owner'"){ | |
| $ou = "" | |
| switch($type){ | |
| "HyperV" {$ou = 'ou=HyperVHosts,OU=Servers,OU=HQ,DC=District,DC=Local';break} | |
| "Exchange" {$ou = 'ou=ExchangeHosts,OU=Servers,OU=HQ,DC=District,DC=Local';break} | |
| "ConfigMgr" {$ou = 'ou=ConfigMgrHosts,OU=Servers,OU=HQ,DC=District,DC=Local';break} | |
| default { $ou = 'OU=Servers,OU=HQ,DC=District,DC=Local'} | |
| } | |
| $ServerName = [computer]::GetNextFreeName($type) | |
| try{ | |
| New-ADComputer -Name $ServerName -Description $Description -ManagedBy $user -path $ou -ErrorAction Stop | |
| $this.Name = $ServerName | |
| $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