I have been building and setting up some new Windows Server 2022 RDS farms recently and ran in to an issue when adding a second RDS connect broker after configuring High Availability.
The server would install the broker role and then fail to configure. This would return the below error.
The list of joined nodes could not be retrieved on servername. Privilege not held.
I was also getting EventID 32814 and 4119 in the Microsoft-Windows-Rdms-UI/Admin event logs.
I enabled the debug logging also but this only returned the same error as where showing in the event logs.
In this post we will be going through the use of variables, arrays and hash table.
Variables are object that are created temporarily in the PowerShell console or created as part of script that are removed once the console is closed. There are system variables that are part of PowerShell but we will just be covering user variables in this post.
Variables are $ sign with a name.
We can then call the variable by using the name
There are two type of quotation marks single will only display the text between the quotes. We can use these for variable that have text data.
With double quotation marks we can passthrough text but also passthrough a variable. If we try to add a variable without double quotes it will just write the variable name and not the text in the variable.
There are some instance where we need to create a subexpression using $() around the variable to pass through specific data.
In the below I want to return just the first folder. The first command return all the folder names.
The second command is using a subexpression, we are then able to return the first folder name.
An array is pretty similar to a variable the only really difference is that it has multiple objects broken up by using comma’s.
We can also use @() to create any array if there is text other than number we have to use quotes.
We can also create a blank array, I use these when doing reports and want to output data from a script.
We will go more in depth on using the blank array in a future post but I mostly use them with hash tables.
Next we will go through using a hash table, these are like array but there is key / value pair.
Below we have a table where we created a name, surname and role heading and set a value for each.
We can also use array or variables in hash tables
This is where hash tables can come in hand for doing reports as we can output the results of multiple commands using a blank array and a hash table, then export the results to a CSV, text or html file.
We will cover both these in later post.
In this post we covered create variables, types of quotes and there uses, array’s and hash tables with some examples of each.
In this post we are going to go through sorting, selecting and formatting object data.
To be able to get the right information back in both the PowerShell console and to output to files like txt, csv, html…, it’s import to be able to format the data correctly.
We can sort by using sort-object command, in the below example we can see that the process are sorted by process name by A to Z.
Now we can use sort-object to sort the processes by ID instead of name.
We can added -descending to sort from other way either.
There can be hidden properties that can be useful, to view these we can use the Get-Member and use member type property.
Get-Process | Get-Member -MemberType Property
We can now use the property names with select-object to return the additional properties.
Select-Object has a first and last parameters that let you skip everything but the specified number of objects.
Using the below will only return the first 20 processes.
Get-Process | Select-Object -First 20
Using calculated property in Select-Object is a good way to rename a properties or use math’s to work out a value like the size of a folder or memory used by a process.
To create a calculated property we use @{Name=”Name of property”;Expression={$_.propertyname}}.
In this next series of post we will go through getting start with basic commands, more advances commands and then going through how to use these to creating more complex scripts.
First cmdlets in PowerShell are named according to a verb-noun naming (mostly, there are times when people create there own command that don’t use the verb-noun naming). This pattern is to help give an understanding of what the command does and how to search for them.
Sometimes finding the specific command you want is time consuming in PowerShell. This is where the Get-Command command can come in handy
If we want to find a command by name we can use the full name
If we don’t know what the command is we can use wildcard (*) to return all command that have a conmen name.
Below example returns all command that have network in the name.
Once we have a command we can either look up the documentation or use Get-Help to view how to use the command and examples.
To view the full help with examples on how to use.
Get-Help Get-Service -Full
To view just the examples
Get-Help Get-Service -Examples
To open the webpage for the command use -Online
Get-Help Get-Service -Online
Below is a quick overview of what each verb function should be.
Verb
Function
Get
Return values (object, properties….)
New
Create new objects (File, object….)
Set
Set object (value, properties….)
Out
Output to (Console, file…..)
Start
Start service, process’s ….
Stop
Stop service, process’s…..
To return a list service’s we can run Get-Service.
If we want to filter to only return service with specific status like running we can use where-object and the status property.
If we wanted to only return the display name and status we would pipe the result using | and then use Select-Object.
If we know there is a certain restart order for an application or a restart of a service will fix a specific issue on server or endpoint device, we could then use the name and status to start or restart the service.
To run remotely (if the command supports remote calls) we an use the ComputerName (sometimes its Server, depends on the command).
In the next post we will look at sorting, selecting and additional properties of Objects in PowerShell.