Tag: Windows 2016

30 basic Powershell commands to start with Windows Server

Getting started with Windows 2019 nano server can be very challenging especially if you are not used to CLI on Linux servers. I decided to install Windows 2019 server core only i.e; without GUI and take it as a challenge to learn more about it. Since I’m mostly from a Unix/Linux background, I decided to dive a little bit more in the Windows Operating system. In this article, I’m sharing some commands to start with. However, the Microsoft website covers enough part which will lead to the Windows 2016 MCSE certification. My goal in this article is to get PowerShell beginners on track and paint an idea of what Windows Powershell is capable of.

Windows Updates

1. Some modules are not available by default on the PowerShell. So you will need to manually download it. I downloaded the module PSWindowsUpdate which will enable me to update the OS from PowerShell.

Get-Module PSWindowsUpdate

2. One of the first things you might want to do is to get the updates that need to be installed on  the Operating System first which I did with the following command:

Get-WindowsUpdate

3. Now you can install the updates using the following command. Once installed, reboot the server which might take some time.

Install-WindowsUpdate

4. You can also find the list of updates installed on the machine using the following command:

Get-Hotfix

OS basic verification

5. To get the version of the PowerShell, use the following command:

  • Get-host | select Version
  • $PSVersionTable

6. To get the reboot history :

Get-EventLog system | where-object {$_.eventid -eq 6006} | select -last 10

7. List of services running:

Get-Service

8. List of Installed programs:

Get-Module PSWindowsUpdate
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize

9. Get the process running on Windows

Get-process

Networking

10. Finding the rules on the firewall:

Get-NetFirewallRule

11. IP configuration:

Get-NetIPConfiguration

12. To verify if IPv4 or IPv6 is enabled or not

Get-NetAdapterLso

13. To get the routing table

Get-NetRoute

14. Source and remote IPs on listening mode

Get-NetTCPConnection | ? {$_.State -eq "Listen"}

Storage

15. Get the disk space information

Get-WmiObject win32_logicaldisk
Get-PhysicalDisk

16. To get the health status of the disk:

Get-PhysicalDisk | Sort Size | FT FriendlyName, Size, MediaType, SpindleSpeed, HealthStatus, OperationalStatus -AutoSize

17. Getting the used/free space:

Get-PSDrive C | Select-Object Used, Free

PowerShell Modules and repository

18. The PowerShell Gallery is the central repository for PowerShell content. You can find new PowerShell commands or Desired State Configuration (DSC) resources in the Gallery. To check which repository you are using use the following :

get-PSRepository

19. By default, PowerShell modules are installed in several directories. You can download certain modules in any directory you want. To execute it, you need to append the environment. To see the module paths of your environment, launch the following command:

$Env:PSModulePath

20. To add a path to the environment’s module path, use the following command:

$env:PSModulePath = $env:PSModulePath + ";C:\ModulePath"

21. To get a list of installed modules and the directories in which it has been installed:

Get-Module -ListAvailable

22. To install a module, you can search for it and install it directly:

find-module -Name PendingReboot | install-module 

23. After installing a module, it is always a good practice to import it using the following:

Import-Module PendingReboot

24. To get information which command to use for a pending reboot use:

Get-Command -Module PendingReboot

Downloads and unzip

25. Recently, I saw a link where having a script on Technet. You can use the following command to download the zip file.

Invoke-WebRequest https://gallery.technet.microsoft.com/scriptcenter/Get-RebootHistory-bc804819/file/130620/2/Get-RebootHistory.zip -outfile Get-RebootHistory.zip

26. To unzip a file:

Expand-Archive -path '.\Get-RebootHistory.zip' -DestinationPath 'C:\Users\Administrator\Documents\WindowsPowerShell\Modules'

Getting Help

27. Getting help with commands arguments are pretty easy. Let’s say you want to know the possible arguments of the command Get-Command, simply do:

Get-Command -?

28.  You can also use the Get-Help module. For example, consider the Compress-Archive module:

Get-Help -name Compress-Archive

29. Consider that you need an example of a specific module:

Get-Help -name Compress-Archive -examples

30. The help/man command is also interesting to get fast syntax about a command.

man Compress-Archive
help Compress-Archive

One interesting source of information is gallery.technet.microsoft.com/scriptcenter where you can access several scripts for Microsoft Windows Server Administration. Personally, Windows PowerShell commands seem to be more complexed compared to Linux commands. Probably, its a matter of getting used to it. I will try my best to update this article. Don’t forget to comment below if needed. My last article on Windows was on the installation of SSH on Windows server through Powershell.

Installing OpenSSH on Windows 2012 R2 through PowerShell

This is probably my first article on Microsoft Windows. Some days back, I was asked to perform some tasks on Windows, though I’m not really a big fan of Windows, I managed to do it. Prior to the tasks, I wanted to have my usual SSH capabilities to log on the server, so I decided to install OpenSSH on the Windows 2012 R2 server. Microsoft has a repository for OpenSSH on Github. An interesting thing about Windows is that SSH has now been brought to Windows 2016. Well, I decided to add a new category on tunnelix.com about ‘Windows‘. Comment below if you find this weird!


Please credit tunnelix.com for using the picture
Please credit tunnelix.com for using the picture

 

1. Point yourself into the directory where you want the file to be downloaded. In my case, it is the directory: C:\Users\Administrator\Desktop :


PS C:\Users\Administrator> cd C:\Users\Administrator\Desktop

2. The installation is pretty simple. You will need to download the .zip file from the Github repository using the Invoke-WebRequest command. By default, Invoke-WebRequest command supports TLS 1.1 and same has been deprecated. So you might need to change the security protocol to TLS1.2 or TLS1.3 using the following command:

PS C:\Users\Administrator\Desktop> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

3. Then download the binary using the Invoke-WebRequest:

PS C:\Users\Administrator\Desktop> Invoke-WebRequest -Uri "https://github.com/PowerShell/Win32-OpenSSH/releases/download/v7.7.2.0p1-Beta/OpenSSH-Win64.zip" -OutFile "powershell.zip"

4. On a fresh installation, Windows 2012 R2 does not have the Expand-Archive command, so we will use .NET directly. Add-Type loads a .dll with the necessary .net functions in your current session. then [io.compression.zipfile] is a reference to that loaded .dll and ::ExtractToDirectory is the way to call a function from that dll :

PS C:\Users\Administrator\Desktop> Add-Type -assembly "system.io.compression.filesystem"

5. Now, we can unzip the file:


PS C:\Users\Administrator\Desktop> [io.compression.zipfile]::ExtractToDirectory( 'C:\Users\Administrator\Desktop\powershell.zip','C:\Users\Administrator\Desktop' )

6. After unzipping the file, get into the directory that has been unzipped and launch the installation:

PS C:\Users\Administrator\Desktop> cd .\OpenSSH-Win64

PS C:\Users\Administrator\Desktop\OpenSSH-Win64> .\install-sshd.ps1

7. The output should look as follows:

**** Warning: Publisher OpenSSH resources are not accessible.

[SC] SetServiceObjectSecurity SUCCESS
[SC] ChangeServiceConfig2 SUCCESS
[SC] ChangeServiceConfig2 SUCCESS
sshd and ssh-agent services successfully installed

8. The following command will show the status of the SSHD service:

PS C:\Users\Administrator\Desktop\OpenSSH-Win64> get-service | findstr ssh
Stopped ssh-agent OpenSSH Authentication Agent
Stopped sshd OpenSSH SSH Server

9. Launch the service with the following command:

PS C:\Users\Administrator> Start-Service sshd

10. You might need to add firewall rules to allow port 22 on the machine

PS C:\Users\Administrator> netsh advfirewall firewall add rule name=SSHPort dir=in action=allow protocol=TCP localport=22

11. You can also configure OpenSSH server to start automatically after the server reboot.


PS C:\Users\Administrator> Set-Service -Name sshd -StartupType "Automatic"

OpenSSH must be ready by now. You can SSH on your Windows server now. In future articles, I will blog more about Windows system administration, LDAP on Windows and more about Windows 2016 server. Enjoy 🙂