Category: Windows

Windows Administration Blog Archives, by Peter Whyte (Database Administrator).
Includes: Database Admin, Windows Admin, PowerShell, WSL & more…

  • How to Check What Features are Installed in SQL Server

    How to Check What Features are Installed in SQL Server

    This is a quick guide on how to check which features are installed on a SQL Server.


    Open your SQL Server installation files. You may find them within the Setup Bootstrap folder (“C:\Program Files\Microsoft SQL Server\150\Setup Bootstrap\SQL2019”) or you might already have the ISO handy.

    SQL Server Installation Files

    We can open cmd.exe from the top navigation bar.

    Open CMD from Folder

    Then run the Setup.exe with the /ACTION=RunDiscovery parameter (Microsoft Docs – Installation Parameters).

    SQLSetup RunDiscovery

    The SQL Server discovery report is saved to %ProgramFiles%\MicrosoftSQL Server\nnn\Setup Bootstrap\Log\<last Setup Session>.

    ‘nnn’ is whatever version of SQL Server you have:
    # SQL Server 2012 (110)
    # SQL Server 2014 (120)
    # SQL Server 2016 (130)
    # SQL Server 2016 (130)
    # SQL Server 2017 (140)
    # SQL Server 2019 (150)

    SQLSetup Summary File

    If you’re using PowerShell, Get-Content will work… tab your way through the sub-folders.

    SQLSetup Summary File PowerShell

    The highlighted area above shows only the Database Engine Services are installed of SQL Server 2019 Developer Edition.


  • Enabling TCP Connections in SQL Server

    Enabling TCP Connections in SQL Server

    Some network protocols are not enabled by default in SQL Server. We need to enable the option and restart the SQL Service to allow TCP connections.

    This post is a guide on how this is done:
    # On Windows via SQL Server Configuration Manager.
    # On Windows using PowerShell.


    Enable TCP/IP SQL via Server Configuration Manager

    1. Open SQL Server Configuration Manager.

    SQL Server 2019 Configuration Manager Start Menu

    2. Navigate to TCP/IP Protocols as highlighted and open Properties.

    SQL Server Configuration Manager Protocols

    3. Enable the highlighted option within the Protocol tab.  

    SQL Server Configuration Manager TCP Properties

    4. Enable what you need within the IP Addresses tab.

    SQL Server Configuration Manager Enable IPv4

    5. Click Apply.

    SQL Server Changes on Next Service Restart

    6. Navigate to SQL Server Services within the Configuration Manager and restart the SQL Server Service.

    SQL Server Configuration Manager Services

    Enable TCP/IP SQL Server using PowerShell

    I’m using a quick script gained from Microsoft Docs, tweaking to run locally as instructed and I added some visibility of the service restart.

    This script requires sqlps, just run Import-Module “sqlps” for the script functions to work.

    ## Enable/Disable MSSQL TCP & Named Pipes
    $smo = 'Microsoft.SqlServer.Management.Smo.'  
    $wmi = new-object ($smo + 'Wmi.ManagedComputer').  
    
    # Enable TCP Protocol for the locl machine  
    $uri = "ManagedComputer[@Name='" + (get-item env:\computername).Value + "']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']"
    $Tcp = $wmi.GetSmoObject($uri)  
    $Tcp.IsEnabled = $true
    $Tcp.Alter() 
    
    # Enable Named Pipes Protocol for the local machine
    $uri = "ManagedComputer[@Name='" + (get-item env:\computername).Value + "']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Np']"  
    $Np = $wmi.GetSmoObject($uri)  
    $Np.IsEnabled = $true  
    $Np.Alter()  
    
    # Restart the SQL Services
    $DfltInstance = $Wmi.Services['MSSQLSERVER']  
    $DfltInstance.ServiceState
    $DfltInstance.Stop(); 
    Start-Sleep 1
    $DfltInstance.Refresh();
    $DfltInstance.ServiceState
    Start-Sleep 1
    $DfltInstance.Refresh();
    $DfltInstance.ServiceState
    $DfltInstance.Start();
    Start-Sleep 1
    $DfltInstance.Refresh();
    $DfltInstance.ServiceState
    PowerShell MSSQL TCP & Named Pipes Change

    I was able to verify by looking at SQL Server Configuration Manager, but we can also do with the script info above.

    SQL Server Configuration Manager Protocols

  • Domain Join Sanity Checks

    Domain Join Sanity Checks

    In order to join a Windows Server to a domain, you may need to request that the network team open certain firewall rules. The network requirements for this process can be complex and may vary depending on your specific environment.

    After joining a domain, especially in a new environment, it is important to perform some checks to ensure everything is working correctly. These checks may include using GPResult and GPUpdate to update and verify Computer and User Policies, using Nltest to perform network administrative tasks such as querying domain controllers and trust relationships, and reviewing the Windows Event Viewer for any issues.

    This post shows examples of performing such checks post joining a Domain for a Windows Server:
    # GPResult / GPUpdate
    # Nltest
    # Windows Event Viewer

    GPUpdate / GPResult

    After joining a Windows Server to a domain, you can use the gpupdate command to check if the domain join is healthy. This command updates Computer and User Policies on the server, and can help to ensure that the server is properly configured and communicating with the domain controller.

    Here is an example of running gpupdate:

    Windows Server GPUpdate

    We can also have a look at applied Computer policies using the /v parameter when running gpresult.

    Windows Update GPResult

    Nltest

    Nltest.exe is a command-line tool that allows you to perform network administrative tasks, including querying and testing the status of domain controllers and trust relationships. Some examples of the tasks that you can perform with Nltest include:

    nltest /dclist:<domain> lists all the domain controllers in the domain.

    nltest dclist

    nltest /dsgetdc:<domain> queries DNS and returns a list of domain controllers (with IPs).

    nltest dsgetdc

    nltest /dsgetsite returns the site name of the domain controller.

    nltest getsite

    nltest /sc_query:<domain> reports the state of the secure channel for when it was last used.

    nltest scquery

    Windows Event Viewer

    The Windows Event Viewer is a useful tool for viewing and managing events that are recorded by Windows operating systems. While not all events recorded in the Event Viewer require investigation, it is important to pay attention to errors and critical events, especially during the domain join process.

    Windows Event Viewer

  • How to Create MySQL Databases & Tables

    How to Create MySQL Databases & Tables

    This blog post is a guide on how to create databases and tables in MySQL, from a Windows and Linux computer. The information included here should help beginners who are new to MySQL.

    To create a new database in MySQL, and many other relational database systems we use the CREATE DATABASE SQL Statement.

    The meaning behind the word ‘database’ can vary, and often be referred to the same thing as a Schema. In MySQL, Databases and Schemas are both one of the same things.

    The following MySQL tips are covered in this post:
    # Create a MySQL Database
    # The USE DATABASE Command
    # Close MySQL Queries with Semicolons
    # How to Exit from MySQL CLI

    Create a MySQL Database

    We can create a MySQL Database by running CREATE DATABASE databaseName

    The create database example below is being done using the GUI option, MySQL WorkBench. For more information on SQL syntax and parameters of the CREATE DATABASE statement in MySQL, we can refer to the MySQL Dev Docs.

    Create Database MySQL WorkBench

    After running the above, refresh Schemas within the Navigator to the left and open the new database.

    If you’re instance running on Linux, database names are case-sensitive, whereas on Windows this isn’t an issue. Below, I’m creating a database on a local MySQL install (Windows 10) and then creating a MySQL table within it using a Capital letter on the schema/database name.

    MySQL Create Table and Insert Data

    Below is an example of doing the same on Linux using the MySQL Command-Line Client.

    SSH Connect to Amazon Linux

    The above is us logging in with the ec2-user to an Amazon Linux EC2 instance. Then we create a new table using a Capital letter on the database schema name.

    MySQL Create Table and Insert Data

    A common best practice is to stick to the one case for object naming in SQL for this reason. For example, use finance_database instead of Finance_Database, or use FINANCE_DATABASE.

    The USE DATABASE Command

    The USE DATABASE command in SQL is used to set your statements to use the specified database as your default (current) database.

    To show an example of USE in SQL, I create a new database and table MySQL Workbench below. The CREATE TABLE and INSERT INTO statements include the schema name (butter), and then I attempt to run a SELECT statement on the table without including the schema name.

    MySQL WorkBench Create Table and Insert Data (without schema)

    The returns an error – Error Code: 1046. No database selected. Select the default DB to be used by double-clicking its name in the SCHEMAS list in the sidebar.

    As described in this error message, we can set our default schema in the MySQL Workbench Navigator.

    MySQL WorkBench Set Default Database

    That’s one solution. The other is to use the USE DATABASE statement. I’m adding USE butter; to the TSQL in the MySQL Workbench query window.

    All queries executed after running the USE command will assume the database specified when running queries without a schema name.

    MySQL WorkBench Create Table and Insert Data (with USE)

    Close MySQL Queries with Semicolons

    Semicolons are a way to determine when your SQL statement ends in MySQL.

    A query does not run by hitting return in a MySQL terminal, it requires a semicolon to know when the SQL statement is complete.

    The screenshot below shows a Linux client connected to MySQL. The first command includes a semicolon and returns a list of databases. The second command (DROP DATABASE) displays the example of a command not being executed.

    MySQL Drop Database Delayed Exit

    The command does not run until we add a semicolon and hit return, as marked with a red cross on the screenshot.

    To further help understand semicolons in MySQL I also will show an example in MySQL Workbench. I’m creating a new table and inserting a row of data into the table on the next row.

    MySQL WorkBench Create Table and Insert Data (no exit)

    The CREATE TABLE statement returns an error message – Error Code: 1064: You have an error in your SQL syntax; check the manual that the corresponds to your MySQL...

    The table did not get created here. If I add the semicolon at the end of the CREATE TABLE line the table will get created and a row will be inserted.

    MySQL WorkBench Create Table and Insert Data (with exit)

    If you’re running queries line-by-line then the semicolon isn’t as much required. For more information, see this MySQL documentation link.

    How to Exit MySQL CLI

    This part shows you how to bug out of MySQL CLI.

    To exit MySQL CLI, type QUIT and hit return in your terminal window.

    How to exit out of MySQL in Terminal

  • Delete Windows.old Files

    Delete Windows.old Files

    When your machine is upgraded to a newer version of Windows, it leaves behind a C:\Windows.old directory which can be quite large.

    windows.old Properties

    It contains the backup of the previous Windows version, so if you plan on deleting it you’re saying goodbye to the rollback option.

    If do you try delete it, you’ll likely encounter the following…

    Windows.old unable to delete
    Delete Windows.old try again

    Try again please…

    We can remove this by running Disk Cleanup.

    Disk Cleanup in Windows
    Disk Cleanup

    Click Clean up system files.

    Cleanup System Files Loading

    (might take a sec)

    Disk Cleanup System Files Previous Windows Version

    Select Previous Windows installation(s) and click OK.

    Disk Cleanup Confirmation Prompt

    You are sure, so click to Delete Files on the above prompt.

    As mentioned, if you delete these files you’ll lose the ability to restore back to the previous version of Windows. You’d peer check before going ahead with this in the workplace.

    Disk Cleanup Previous Windows Version Delete Warning

    It might take a good 5 minutes or so.

    Disk Cleanup Happening

    When it finishes you should see the Windows.old files disappear and the disk space be freed!

    Windows.old Removed

  • Installing MySQL 8.0 on Windows

    Installing MySQL 8.0 on Windows

    A simple run-through of installing MySQL Community Server 8.0, including MySQL Workbench on Windows.

    Don’t forget to checkout the MySQL Installation documentation, and here’s a link to prerequisites.


    Installing MySQL

    Open the MySQL installer exe as downloaded from the link above.

    MySQL Installer File

    Accept License Agreement and Next.

    MySQL License Agreement

    I would say always choose a Custom install – only ever install what you know you need.

    MySQL Install Choose Setup

    I’m installing MySQL Server, MySQL Workbench and the ODBC Driver.

    MySQL Install Feature Selection

    You may have an additional step to install Microsoft Visual C++ Redistributable packages – execute those within the wizard when prompted.

    Hit Exectute to install all features selected.

    MySQL Install Features - Ready
    MySQL Installing

    Now it’s time to configure the MySQL Server, starting off with High Availability which I don’t need on my local test environment here.

    MySQL Install High Availability

    On the next page, the Config Type is Development Computer, although here’s the options for reference;

    MySQL Install Server Config Type

    No need to deviate from the default port number or other config here.

    MySQL Install Networking

    Unless you have MySQL 5.x Servers in your stack, choose the recommended authentication method.

    MySQL Install Authentication Type

    Enter the MySQL Root Password – we can create new users later if required.

    MySQL Install Root Password

    This is where we select which account the MySQL Service will run as.

    MySQL Install Run As Account

    Now we’re ready for the configuration update – hit Execute.

    MySQL Install Applying Config Update

    We can have a browse in the log file to view what’s actually going on in the background.

    MySQL Installation Log

    All is now complete!


    MySQL Workbench

    Click to add a new MySQL Connection.

    MySQL Workbench Add New Connection

    Enter a name for the connection and no need to change connection details as this is all local on the default port.

    MySQL Workbench New Connection Window

    Click to Test Connection and enter the root user password as entered during installation.

    MySQL Workbench New Connection Password
    MySQL Workbench Connection Test

    Once the above is OK’d, this new connection will show up in the Workbench connections area – click to connect.

    MySQL Workbench New Connection

    And I’ll now a random query to finish this off…

    MySQL Workbench SELECT Version

  • Adding & Removing SQL Features via Command

    Adding & Removing SQL Features via Command

    This is a quick guide on how to add and remove features from SQL Server via command, following the theme of recent posts.

    The following areas will be covered within this post;
    # Check Features Installed on a SQL Server.
    # Installing a SQL Server Feature (Replication) via Command.
    # Uninstalling a SQL Server Feature (Replication) via Command.


    Check Features Installed on a SQL Server

    A mounted SQL Server ISO is required for us to first RunDiscovery.

    SQL Server Run Discovery via Command

    That’ll only take a second, and once done we can view the contents of the Summary.txt file to see the discovery result.

    SQL Server RunDiscovery Summary File

    Installing a SQL Server Feature (Replication) via Command

    The command below should be straight-forward enough to understand. See Microsoft’s documentation for more information on parameters etc.

    SQL Server Install Feature via Command

    We can verify the feature installation by running another Discovery.

    SQL Server RunDiscovery Summary File Replication

    Uninstalling a SQL Server Feature (Replication) via Command

    Uninstalling a feature is as easy as the installation of one. Run the command as shown below.

    SQL Server Uninstall Feature via Command

    And finally run another Discovery.

    SQL Server RunDiscovery via Command Removing Feature

  • Install/Uninstall SQL Server via Command

    Install/Uninstall SQL Server via Command

    This is a post on installing SQL Server via command; I have a Windows Server 2016 Core running on a local Hyper-V test environment, and I’m installing SQL Server 2019, then uninstalling it.

    There’s 4 parts to this one;
    # Download a SQL Server ISO.
    # Mount the ISO on a Hyper-V Virtual Machine.
    # Install SQL Server via Command.
    # Uninstall SQL Server via Command.


    Download a SQL Server ISO

    As this is on my test environment, I’m installing the most recent SQL Server Edition available, which is SQL Server 2019 CTP 2.4 at the time of writing.

    SQL Server 2019 can be downloaded here – Open it up and select to download the media.

    SQL Server 2019 Download Media

    ISO’s are good for local virtual environments (using HyperV here).

    SQL Server 2019 Download Media Type

    6Mbps…

    SQL Server 2019 Download Speed

    We’re now ready to get that ISO mounted.


    Mount the ISO on a Hyper-V Virtual Machine

    1. Click Media > Insert Disk…

    HyperV Insert Disk

    2. Select the ISO file.

    SQL Server 2019 ISO File

    Install SQL Server via Command

    1. First, lets have a look at our drives using the Get-PSDrive cmdlet.

    PowerShell Get-PSDrive

    2. Install using your preferred parameters. I’m adding the Domain Admins group as Sysadmins for quickness.

    SQL Server 2019 Install via Command

    3. We can look at the Summary.txt file using Get-Content to verify the installation has been successful.

    PowerShell Get-Content SQL Summary File
    PowerShell Get-Content SQL Summary

    The above shows the result of the installation was successful, and other things like where the Configuration INI file is (used for Unattended Installs).

    4. Next, we should check the SQL Server services, using Get-Service with “SQL” wrapped in wildcards.

    PowerShell Get-Service

    Uninstall SQL Server via Command

    Uninstalling is as simple as the install, funnily enough..

    SQL Server 2019 Uninstall via Command

    Once it’s done, we can check the summary file status and services to quickly verify the uninstallation has not been unsuccessful.


  • Configuring Windows Server Core

    Configuring Windows Server Core

    Windows Server Core is the CLI only version of the Windows OS. This post is a run-through of configuring a new Windows Server 2016 Core host, detailed in the following steps;

    # Installing Windows Server 2016 Core.
    # (CLI) Changing a Windows Server Hostname.
    # (CLI) Amending TCP/IP & DNS Settings.
    # (CLI) Joining a Windows Server to a Domain.


    Installing Windows Server 2016 Core

    When installing Windows Server, we need to select the following option.  

    Windows Server Core Install

    Once the above is done and the reboot happens, the following will appear.

    Server Core Enter Admin Password

    Enter the Administrator password…

    Server Core Enter Admin Password

    We’re now logged in and ready to go.

    PowerShell Get-Hostname

    Changing a Windows Server Hostname

    An easy task done with the Rename-Computer cmdlet accepted by PowerShell, followed by a reboot.

    PowerShell Rename-Computer

    Amending TCP/IP & DNS Settings

    First we need to look at the current recognised interfaces using the Get-NetIPConfiguration cmtlet.

    PowerShell Get-NetIPConfiguration

    Then set a new private IP address using the New-NetIPAddress cmdlet.

    PowerShell New-NetIPAddress

    Lastly, set the DNS address using Set-DnsClientServerAddress.

    PowerShell Set-DNSClientServerAddress

    The private IPv4 network has been configured, now we should verify we have line-of-sight.

    Ping IP Test
    Ping Hostname Test

    Joining a Windows Server to a Domain

    To join a Windows OS to the domain, we can use the Add-Computer cmtlet – You’ll immediately be prompted for authorisation from an account that can join computers to the domain.  

    PowerShell Add-Computer

    Enter the Domain Name.

    PowerShell Add Computer Domain Name

    Reboot once done.

    PowerShell Add-Computer Reboot

    When the box is up, you’ll have the option to login with the Local Administrator account, or with another user.

    Server Core Login Other User

    If selecting Other user, you’ll have an entry for the domain you’re signing into.

    Server Core Domain Login
    Server Core Hostname and Login Info

  • Joining a Windows Host to a Domain

    Joining a Windows Host to a Domain

    This post contains a demo of joining a Windows Server onto a test Domain, which I created in my previous post (Installing Active Directory on Windows Server 2016).

    Joining a Windows Computer to Active Directory Domain Services is required in 99% of Corporate environments, and is usually a Systems Administrator task to configure at the Domain Controller side. Adding a computer to AD allows your computer to be administered via Group Policies that can be applied to all remote computers globally within your company. This helps companies lock down computers and roll out new software or updates.

    There are 2 parts to this one:
    # Creating a New Domain User Account
    # Joining a Windows Server to a Domain

    Creating a New Domain User Account

    I’m creating a new user account for me to carry out all changes required to set up my lab. It’ll be a new OU in AD, create a new user account and then add it to the Domain Admin group.

    1. Right-click the Domain Name within Active Directory Users and Computers, select New and Organisational Unit:

    Active Directory New Organisational Unit

    2. Enter the new OU name – I’m going to pretend I’m in the IT Department:

    Active Directory New Organisational Unit Name

    3. Within the IT OU, right-click and select New-User:

    Active Directory New User

    4. Enter new user details:

    Active Directory New User Details

    5. Enter password stuff:

    Active Directory New User Password

    6. Finish:

    Active Directory New User Finish

    7. Right-click the new user and select Add to a group:

    Active Directory Add User to Group

    8. Enter king group, Domain Admins:

    Active Directory Add User to Domain Admins

    Joining a Windows Server to a Domain

    1. First, I have to get onto the same private IP range as the DC:

    IPv4 Config
    Ping Test to Active Directory

    2. Within Server Manager, click the Computer name link:

    Windows Server Manager Computer Name

    3. Click Change:

    Windows Systems Properties

    4. Amend the hostname and enter the Domain name:

    Computer Name Change and Join Domain

    5. Enter credentials for an account that can join the domain (the account created above within this post would also work):

    Domain Join Elevation Prompt

    6. When the server next boots up a domain user account can log in:

    Windows Server Login to New Domain

    And that should be us!