Functions/new-WITsqlconnection.ps1

<#
.Synopsis
   Skapar ett dbconn objekt mot databasen
.DESCRIPTION
   $sqlserver
   $database
.EXAMPLE
   Example of how to use this cmdlet
.EXAMPLE
   Another example of how to use this cmdlet
#>

function new-WITsqlconnection
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([int])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory = $true,
                   ValueFromPipelineByPropertyName = $true,
                   Position = 0)]
        $sqlserver,
        [Parameter(Mandatory = $true,
                   ValueFromPipelineByPropertyName = $true,
                   Position = 1)]
        $database,
        $username,
        $password
    )
    
    Begin
    {
        #Provider=SQLOLEDB;
        #Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
        #Öppna dataanslutningen
        $dbConn = new-object -comobject ADODB.Connection;
        if ($username -eq $null)
        {
            $connect = "Provider=SQLOLEDB;Server=$sqlserver;Database=$database;Integrated Security=SSPI;"; # ConnectionString
        }
        else
        {
            $connect = "Provider=SQLOLEDB;Server=$sqlserver;Database=$database;User Id=$username;Password=$password;"; # ConnectionString
        }
        $dbConn.Open($connect); # öppna anslutning
        $dbConn.CursorLocation = 3 # 3= Client cursor mode
    }
    Process
    {
        return $dbConn
    }
    End
    {
    }
}