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