New PowerShell features in Windows Server 2016

Good day% HabraUser%! Not so long ago, I was lucky enough to become the owner of VDS with preinstalled Windows Server 2016 to familiarize myself with this operating system and its new features. Due to the fact that for the past several years I have been a fan of administration using PowerShell, it was he who first of all interested me, since I use it in my work daily to automate routine tasks. In the corporate environment, by far the most commonly used version of the operating systems is Windows 8.1 and Windows Server 2012 R2, respectively, I did not pay attention to the changes that Windows 10 brought to me on my home computer, and as it turned out in vain. I missed the updated tool, which became much better, more convenient and faster than previous versions, I’d like to talk about these major changes. Welcome to cat.

At the top of the list will be the most minor changes that are designed to make daily use of this product more comfortable for the administrator:

  • The PowerShell editor window can now be resized, a dubious achievement, but now the window can be expanded to full screen;

    Let's compare:


    PowerShell 2.0


    PowerShell 5.1

  • The next obvious difference that is visible in the screenshots is the syntax highlighting - this is already a very big plus;

  • It is followed by another innovation - from now on, modules and cmdlets of different versions should be supported;

    This example is best seen in the screenshots:


    PowerShell 2.0.


    PowerShell 5.1.

    As you can see from the screenshot, a new command appeared, which, like the better-known Update-Help for updating help, serves to update versions of Update-Module modules.

  • Classes have appeared ; there is a separate article on this subject ;

  • The speed of working with com objects has increased.

I would like to talk about the last point separately. The size of Active Directory in organizations is different, there were cases when it was 20-60 users, and there were also when more than several tens of thousands, and if in the first case you can do only with a graphical interface, then in the second it can be done, but it’s rather difficult. Few of the administrators imagine Active Directory as a data-rich database from which you can get the data you need to work in a matter of minutes, applying the right approach to this.

Some advertising
The image of Windows Server 2016 only appeared yesterday for download in the Microsoft partners directory, and today you can get acquainted with all its features for only 250 rubles on vpsville.

Lyrical digression...
Microsoft originally presented Active Directory as a tool not only for system administrators, but also for personnel. The former should contain everything in working order from the point of view of server and software architecture, the latter, in turn, are responsible for the correct filling of the directory. Therefore, as is customary to understand from all sources from which information can be gleaned on this issue, the administrator should not create user accounts, and create groups according to the ideology of Active Directory, too, but in our harsh Russian realities, everything is a little bit different.

Instead of technical specifications.
Let's imagine that we do not work in the smallest organization and have several domain controllers in the branches scattered throughout our vast homeland under control, respectively, for example, the number of employees we have will be at least one thousand people. Recall that our country is divided into many time zones and when some just come to work, others already go to bed, so the Active Directory management model will be partially centralized, which implies the presence of administrators in the regions, and not just at the central office. We also have internal technical documentation with the requirements for maintaining Active Directory, so that everything is uniform and convenient, but we remember that this is a cool database, not a blunder.

And so we have the knowledge and task that needs to be solved, let’s do just that.

Due to the fact that according to our terms of reference, the data we have is very well structured, we can contact them using standard queries, one of such examples is the directory of employees used at each enterprise.

This is how it looks on PowerShell:

Get-ADUser -Filter * -SearchBase "OU=Users,OU=Main Office,DC=MyCompany,DC=ru" -Server 'Domain Controller Name' -Properties displayName, description, physicalDeliveryOfficeName, telephoneNumber, mail, title, department, company, manager |
Select displayName, description, physicalDeliveryOfficeName, telephoneNumber, mail, title, department, company, manager |
Export-CSV "C:\Export\MainOffice.csv" -NoType -UseCulture -Encoding Unicode

Code Comments
Get-ADUser - a cmdlet that gets an array of users from the search area (-SearchBase) with properties specified by a comma after the -Properties parameter;
Export-CSV - exports this array to a file.

As a result, we get a ready-made table with the required fields: Name, Cabinet number, Phone number, Mail address, Position, Department name, Branch name, Name The head of this employee. This script works for several seconds and allows us to change only two (file name) one parameter in the search area (-SearchBase) and get a reference for any branch at the current time. Next, we open the file we created in Excel, change the column names, format it as we like and save it in the native format for Excel. Having realized that this is pampering and you can do something more serious by yourself, the option to work with a COM object, namely Excel, came.

$Template_Excel = "C:\PS\Шаблон.xlsx"
$SaveAs = "C:\PS\ЗаполненыйШаблон.xlsx"
$AllExcel = @(Get-Process [e]xcel | %{$_.Id})
$MyExcel = New-Object -ComObject Excel.Application
$ExcelId = Get-Process excel | %{$_.Id} | Where {$AllExcel -notcontains $_} 
$MyExcel.Visible = $False
$WorkBook = $MyExcel.workbooks.open($Template_Excel)
$WorkSheet = $WorkBook.sheets.item("Шаблон")
$Users = Get-ADUser -Filter * -SearchBase "OU=Users,OU=Main Office,DC=MyCompany,DC=ru" -Server 'Domain Controller Name' -Properties displayName, 
description, physicalDeliveryOfficeName, telephoneNumber, mail, title, department, company, manager |
Select sAMAccountName, displayName, description, physicalDeliveryOfficeName, telephoneNumber, mail, title, department, company, manager
For($x = 0; $x -le $Users.count; $x++)
{
    $WorkSheet.Rows.Item($x+2).Columns.Item(1) = $Users[$x].displayName
    $WorkSheet.Rows.Item($x+2).Columns.Item(2) = $Users[$x].description
    $WorkSheet.Rows.Item($x+2).Columns.Item(3) = $Users[$x].physicalDeliveryOfficeName
    $WorkSheet.Rows.Item($x+2).Columns.Item(4) = $Users[$x].telephoneNumber
    $WorkSheet.Rows.Item($x+2).Columns.Item(5) = $Users[$x].mail
    $WorkSheet.Rows.Item($x+2).Columns.Item(6) = $Users[$x].title
    $WorkSheet.Rows.Item($x+2).Columns.Item(7) = $Users[$x].department
    $WorkSheet.Rows.Item($x+2).Columns.Item(8) = $Users[$x].company
    $WorkSheet.Rows.Item($x+2).Columns.Item(9)= $Users[$x].manager
}
$Workbook.SaveAs($SaveAs)
$MyExcel.quit()
Stop-Process -Id $ExcelId -Force -ErrorAction SilentlyContinue

Code Comments
Get-Process - gets a list of processes, in our case [e] xcel;
New-Object - create a new COM object, start your Excel process;
Stop-Process - delete the created object.

The code in PowerShell has become a little more, but at the output we got a ready-made file in which nothing needs to be fixed. There was only one big thing, BUT this code in Windows 8.1 runs ~ 25-40 minutes depending on the number of objects being processed, and most of the time it takes to work with a COM object. Accordingly, this approach to use before the advent of PowerShell 5 in my life was inconvenient because of the time it took to create the file. In Windows 10 or Windows Server 2016, this script works out in a couple of minutes, which allows you to expand the scope of capabilities.

*
* - in the comments to the code, the names of the cmdlets are links to the official documentation.

Thank you for reading to the end. Chukchi is not a writer, Chukchi is a reader.

Also popular now: