tools/spe/New-ScriptSession.ps1 (106 lines of code) (raw):
function New-ScriptSession {
<#
.SYNOPSIS
Creates a new script session in Sitecore PowerShell Extensions via web service calls.
.EXAMPLE
The following remotely connects to an instance of Sitecore initializes a session.
New-ScriptSession -Username admin -Password b -ConnectionUri http://remotesitecore
Username : admin
Password : b
ConnectionUri : http://concentrasitecore/sitecore%20modules/PowerShell/Services/RemoteAutomation.asmx
SessionId : 528b9865-a69e-4875-919f-12209646c934
Credential :
Proxy : Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1Services_RemoteAutomation_asmx.RemoteAutomation
.PARAMETER Username
Specifies the Sitecore identity used for connecting to a remote instance.
.PARAMETER Password
Specifies the Sitecore password associated with the identity.
.PARAMETER Timeout
Specifies the duration of the wait, in seconds.
.PARAMETER ConnectionUri
Specifies the remote instance url. HTTPS is highly recommended.
.PARAMETER Credential
Specifies a user account that has permission to perform this action. The default is the current user. This is an alternative to using the UseDefaultCredential parameter.
.PARAMETER UseDefaultCredential
Indicates that this command uses the default credential. This command sets the UseDefaultCredential property in the resulting proxy object to True. This is an alternative to using the Credential parameter.
.LINK
Invoke-RemoteScript
.LINK
Wait-RemoteScriptSession
.LINK
Stop-ScriptSession
#>
[CmdletBinding(DefaultParameterSetName="All")]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Username = $null,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Password = $null,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Uri[]]$ConnectionUri = $null,
[Parameter(Mandatory = $false, HelpMessage = "The timeout in seconds.")]
[int]$Timeout,
[Parameter(Mandatory = $false, ParameterSetName = "Credential")]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter(Mandatory = $false, ParameterSetName = "DefaultCredentials")]
[switch]$UseDefaultCredentials
)
begin {
$sessionId = [guid]::NewGuid()
$session = @{
"Username" = [string]$Username
"Password" = [string]$Password
"SessionId" = [string]$sessionId
"Credential" = [System.Management.Automation.PSCredential]$Credential
"Connection" = @()
}
}
process {
foreach($uri in $ConnectionUri) {
$baseUri = $uri
if($uri.AbsoluteUri -notmatch ".*\.asmx(\?wsdl)?") {
$uri = [Uri]"$($uri.AbsoluteUri.TrimEnd('/'))/sitecore%20modules/PowerShell/Services/RemoteAutomation.asmx?wsdl"
}
$proxyProps = @{
Uri = $uri
}
if($Credential) {
$proxyProps["Credential"] = $Credential
}
$connection = [PSCustomObject]@{
Uri = [Uri]$uri
BaseUri = [Uri]$baseUri
}
$lazyProxy = {
$result = & {
$serviceProxy = New-WebServiceProxy @proxyProps
if ($Credential) {
$serviceProxy.Credentials = $Credential
}
elseif ($UseDefaultCredentials.IsPresent) {
$serviceProxy.UseDefaultCredentials = $true
}
if ($Timeout -gt 0) {
$serviceProxy.Timeout = [math]::Min($Timeout * 1000, [int]::MaxValue)
}
if (!$serviceProxy.Url.StartsWith($uri.Scheme + "://")) {
Write-Verbose "Changing proxy URL scheme to: $($uri.Scheme)"
$serviceProxy.Url = $serviceProxy.Url -replace "^https?", $uri.Scheme
}
$serviceProxy
}
Add-Member -InputObject $this -MemberType NoteProperty -Name "Proxy" -Value $result -Force
$result
}.GetNewClosure()
Add-Member -InputObject $connection -MemberType ScriptProperty -Name "Proxy" -Value $lazyProxy
$session["Connection"] += @($connection)
}
}
end {
[PSCustomObject]$session
}
}