As menetionned earlier, we are planning to add a static method that will help us to generate a computer name based on the type of server we want to provision. According to the type of server, the prefix of the server name will be different.
These will be the prefixes of the servers that we will implement in our environment, and use as an example to show case the use of powershell static methods:
- “HyperV” -> “HYPE-“
- “Exchange” -> “EXCH-“
- “ConfigMgr” -> “CONF-“
- Any other server -> “SERV-“
Each time we add a new server, we will simply use the prefix, and add a 3 digits number behind it. For each new server we simply increment the sufix by one, until we find the next number available.
example: CONF-002 would be the second installed configmgr server.
The logic to get the the results from what we have defined above, can be resumed in the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
$PrefixSearch = “”
switch($type){
“HyperV” {$PrefixSearch = “HYPE-*”;break}
“Exchange” {$PrefixSearch = “EXCH-*”;break}
“ConfigMgr” {$PrefixSearch = “CONF-*”;break}
default { $PrefixSearch = “SERV-*”;break}
}
$AllNames = Get-ADComputer -Filter {name -like $PrefixSearch} | select name
$Prefix = $PrefixSearch.Replace(“*”,“”)
[int]$LastUsed = $AllNames | % {$_.name.trim(“$Prefix”)} | select -Last 1
$Next = $LastUsed+1
$nextNumber = $Next.tostring().padleft(3,‘0’)
write-verbose “Prefix:$($Prefix) Number:$($nextNumber)”
$Return = $prefix + $nextNumber
$Return
|
These few lines of code will return us the new computer name according to the value of $type (which must be of type [ServerType]).
Also, you could very easily, convert this code into a reusable function, by wrapping it within the function {} keyword
Now that we have our main code, we will add this to our class as a new method.
We don’t need much to transform this into a powershell static method and integrate that powershell static method into our main powershell class.
The rules to follow are simple:
|
[return type] static methodName (Potential parameters){
#code here
}
|
If we apply this rule to our use case, we will have the following:
|
[string] static GetNextFreeName ([ServerType]$type) {
#code here
}
|
now that we have covered the basics, we can go and create our static function
For the ones in doubt, a powershell static method can only be located in a class. Don’t try to use it as a function, it will not work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[string] static GetNextFreeName ([ServerType]$type) {
$PrefixSearch = “”
switch($type){
“HyperV” {$PrefixSearch = “HYPE-*”;break}
“Exchange” {$PrefixSearch = “EXCH-*”;break}
“ConfigMgr” {$PrefixSearch = “CONF-*”;break}
default { $PrefixSearch = “SERV-*”;break}
}
$AllNames = Get-ADComputer -Filter {name -like $PrefixSearch} | select name
$Prefix = $PrefixSearch.Replace(“*”,“”)
[int]$LastUsed = $AllNames | % {$_.name.trim(“$Prefix”)} | select -Last 1
$Next = $LastUsed+1
$nextNumber = $Next.tostring().padleft(3,‘0’)
write-verbose “Prefix:$($Prefix) Number:$($nextNumber)”
$Return = $prefix + $nextNumber
return $Return
}
|
What we want to do, is to use this logic in our constructor, to get the next available computer name immediately.
A static method allows us to call a method without having to create an instance of that class. The only ‘extra’ thing that we need to do, in order to transform a regular method into a static method is to specify the keyword ‘static‘ in front of the method, and to NEVER make a reference to the $this variable in that specific static method.
If you ask me, the power of classes resides in their constructors. You can add extra logic that will be evaluated during the instantiation of your object. Adding a few checks in the constructor, will allow you to guarantee that the object cannot be created if the constructors doesn’t valid the parameters within it’s inner logic. You can make your class self sufficient with powershell static methods, since these ones can be called in your constructor, and can help to validate some prerequisites before your instance is created. I’ll show case that in a few lines.
So, having explained that, and having written our new static method, we can add it to our existing class. It will end up looking like this.
.gist table { margin-bottom: 0; }
Our static method has been added on line 75. The logic inside the constructor has also been added on line 55 (the call to the static method from within the powershell class constructor).
Leave A Comment