Thursday, May 5, 2022

Powershell Function: Test connectivity to SQL server

This powershell creates a helper function that tests connection to SQL database.  In my situation, found this in order to check if port and protocols needed for SQL connections were being allowed from different vlans.

Usage:
    Test-SQLDatabase -Server SQLServer -Database SomeDB -Username SQLUser -Password password

    Credit to: https://stackoverflow.com/a/38784435 (Rob Holme)


#
# Usage:
#     Test-SQLDatabase -Server SQLServer -Database SomeDB -Username SQLUser -Password password
#
# Credit to: https://stackoverflow.com/a/38784435 (Rob Holme)
#
function Test-SQLDatabase 
{
    param( 
    [Parameter(Position=0, Mandatory=$True, ValueFromPipeline=$True)] [string] $Server,
    [Parameter(Position=1, Mandatory=$True)] [string] $Database,
    [Parameter(Position=2, Mandatory=$True, ParameterSetName="SQLAuth")] [string] $Username,
    [Parameter(Position=3, Mandatory=$True, ParameterSetName="SQLAuth")] [string] $Password,
    [Parameter(Position=2, Mandatory=$True, ParameterSetName="WindowsAuth")] [switch] $UseWindowsAuthentication
    )

    # connect to the database, then immediatly close the connection. If an exception occurrs it indicates the conneciton was not successful. 
    process { 
        $dbConnection = New-Object System.Data.SqlClient.SqlConnection
        if (!$UseWindowsAuthentication) {
            $dbConnection.ConnectionString = "Data Source=$Server; uid=$Username; pwd=$Password; Database=$Database;Integrated Security=False"
            $authentication = "SQL ($Username)"
        }
        else {
            $dbConnection.ConnectionString = "Data Source=$Server; Database=$Database;Integrated Security=True;"
            $authentication = "Windows ($env:USERNAME)"
        }
        try {
            $connectionTime = measure-command {$dbConnection.Open()}
            $Result = @{
                Connection = "Successful"
                ElapsedTime = $connectionTime.TotalSeconds
                Server = $Server
                Database = $Database
                User = $authentication}
        }
        # exceptions will be raised if the database connection failed.
        catch {
                $Result = @{
                Connection = "Failed"
                ElapsedTime = $connectionTime.TotalSeconds
                Server = $Server
                Database = $Database
                User = $authentication}
        }
        Finally{
            # close the database connection
            $dbConnection.Close()
            #return the results as an object
            $outputObject = New-Object -Property $Result -TypeName psobject
            write-output $outputObject 
        }
    }
}

Monday, May 2, 2022

Powershell Function: Show members of active directory group

This powershell creates a function called "Show-ADGroup" with two parameters to display the members of an AD group.


#
# Show-ADGroup - show members of an AD group, exports to out-grid or CSV on desktop.
#
# Parameters: 
#    Gname - name of AD group to show
#    ToFileYN - "Y" will show export list to CSV file; otherwise, display in Out-gridview
#
function Show-ADGroup
{
    param (
        [Parameter(Mandatory)] $Gname,
        [Parameter(Mandatory)] $TofileYN
        )

    # set $DCname to one of your local domain controllers
    $DCname = "your_DC_name"

    # counts number of members
    $cnt = (Get-ADGroup $Gname -Properties *).Member.Count
    Write-Host "# members: $cnt"

    # specify location of export to file; in this case the user's desktop
    $path = "C:\Users\$ENV:USERNAME\Desktop"
    $pathexist = Test-Path -Path $path

    If ($pathexist -eq $false)
        {New-Item -type directory -Path $path}

    $reportdate = Get-Date -Format ssddmmyyyy
    $csvreportfile = $path + "\ADGroupMembers_$reportdate.csv"

    if ($TofileYN -eq "Y") {
        Get-ADGroupMember -Identity "$Gname" -Server $DCname| 
        Select-Object @{Label = "Name";Expression = {$_.Name}}, 
                      @{Label = "SamAcctName";Expression = {$_.SamAccountName}},
                      @{Label = "distinguishedName";Expression = {$_.distinguishedName}} |
        Export-Csv -Path $csvreportfile -NoTypeInformation
    } else {
        Get-ADGroupMember -Identity "$Gname" -Server $DCname| 
        Select-Object @{Label = "Name";Expression = {$_.Name}}, 
                      @{Label = "SamAcctName";Expression = {$_.SamAccountName}},
                      @{Label = "distinguishedName";Expression = {$_.distinguishedName}} |
        Out-GridView
    }
}

MS Teams was working, but now doesn't allow logon

 Occasionally, MS Teams prompts me to logon again and sometimes gets into a loop where it says it logon didn't work and restart to try again, repeatedly...

1. What I do next is right-click MS Teams in systray, then select Quit.


 






2. Open file explorer and go to folder "%appdata%\Microsoft\Teams" and delete the contents.

3. Then start up MS Teams again.  

Otherwise, see more MS Teams troubleshooting info here:
https://docs.microsoft.com/en-us/microsoftteams/troubleshoot/teams-sign-in/resolve-sign-in-errors


Saturday, January 30, 2021

SQL Server Agent (Agent XPs disabled)

How to enable Agent XPs when SQL Agent Service shows "(Agent XPs disabled)".

From SQL Server Management Studio, open query window and use sp_configure commands below:

SP_CONFIGURE 'SHOW ADVANCE',1
GO
RECONFIGURE WITH OVERRIDE
GO
SP_CONFIGURE 'AGENT XPs',1
GO
RECONFIGURE WITH OVERRIDE
GO

Start or restart SQL Agent in SSMS.


Reference:

https://blog.sqlauthority.com/2016/06/13/sql-server-fix-agent-xps-component-turned-off-part-security-configuration-server/

https://blog.sqlauthority.com/2016/02/29/sql-server-unable-to-start-sql-server-agent-failed-to-initialize-sql-agent-log/


Thursday, March 8, 2018

Netapp & SMB options

SMB setting changes:


  • To enable/disable SMB1
    "cifs control set smb1.enable [yes/no]"
  • To enable/disable SMB2
    "options cifs.smb2.enable [on/off]"

Tuesday, May 30, 2017

Reset SoftwareDistribution Folder

Occassionally, Windows Update process stops cleaning up or something and the C:\WINDOWS\SoftwareDistribution folder starts to get full.


To reset this, stop the service, rename the folder, and restart service.  This should create a new SoftwareDistribution folder:
  • Open a CMD window runas admin
  • type net stop wuauserv and press enter
  • type rename c:\windows\SoftwareDistribution softwaredistribution.oldand press enter
  • type net start wuauserv and press enter
  • type exit and press enter
Reboot and if all is well, you can delete the old softwaredistribution.old folder.