internal/Connect-AsServer.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Function Connect-AsServer
{
<#
.SYNOPSIS
Internal function that creates SMO server object. Input can be text or SMO.Server.
#>

[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[object]$AsServer,
[switch]$ParameterConnection
)

if ($AsServer.GetType() -eq [Microsoft.AnalysisServices.Server])
{

if ($ParameterConnection)
{
$paramserver = New-Object Microsoft.AnalysisServices.Server
$paramserver.Connect("Data Source=$($AsServer.Name);Connect Timeout=2")
return $paramserver
}

if ($AsServer.Connected -eq $false) { $AsServer.Connect("Data Source=$($AsServer.Name);Connect Timeout=3") }
return $AsServer
}

$server = New-Object Microsoft.AnalysisServices.Server

try
{
if ($ParameterConnection)
{
$server.Connect("Data Source=$AsServer;Connect Timeout=2")
}
else { $server.Connect("Data Source=$AsServer;Connect Timeout=3") }
}
catch
{
$message = $_.Exception.InnerException
$message = $message.ToString()
$message = ($message -Split '-->')[0]
$message = ($message -Split 'at System.Data.SqlClient')[0]
$message = ($message -Split 'at System.Data.ProviderBase')[0]
throw "Can't connect to $asserver`: $message "
}

return $server
}