Function Start-windreamSession-withLogging { <# .SYNOPSIS Start a connection / Session with a windream Server. .DESCRIPTION To work in or with windream, you need to start a Session with a windream Server, to access its files, folders and indices. The windream Drive letter which was retrived by running this module, will be set for the whole Script. For Impersonation Login Methode, all these Parameters must be set: windreamServer, windreamDomain, windreamUserName and windreamUserPassword. Function returns with the whole windreamSession Object if connection was successfully established, otherwise with a $False. .REQUIREMENT General PowerShell V3, windream Client Connectivity (>=V3.6) .REQUIREMENT Assembly .REQUIREMENT Variables windreamServer, windreamVersion, windreamServerBrowser, windreamConnect, windreamSession, windreamDriveLetter, windreamDriveLetterMapping, PathTest .REQUIREMENT Variables preSet .REQUIREMENT Functions Write-LogFile .VERSION 1.2.0.0 / 02.12.2016 .PARAMETER windreamServer Optional Parameter. Give Server Name of a specific windream Server, you want to connect with. Otherwise default will be choosen. .PARAMETER windreamDomain Optional Parameter. Give a Windows Domain (which is windream autorisiered) for Impersonation Login Methode. Otherwise current domain will be choosen. .PARAMETER windreamUserName Optional Parameter. Give a Windows User Name (who is windream autorisiered) for Impersonation Login Methode. Otherwise current User Name will be choosen. .PARAMETER windreamUserPassword Optional Parameter. Give the Password for the User name you set bevor in $windreamUserName for Impersonation Login Methode. .PARAMETER windreamVersion Optional Parameter. Give the minimum Version of windream, to make sure later function calls will work (Example 3.6, 4.0, ...). .PARAMETER windream64Bit Optional Parameter. Available since windream 6.0. Necessary, if following functions need 64Bit Integer Support (eg. GetWMObjectByIdEx64(WMEntity aWMEntity, int64 aWMObjectId)). .PARAMETER windreamDriveLetterMapping Optional Parameter. If windream Drive Letter was set in the windream Client config, call this Parameter using $true, to check the Mapping in the Windows Explorer. Default Value is "$True". .EXAMPLE Start-windreamSession .EXAMPLE Start-windreamSession -windreamVersion "3.6" -windream64Bit <$True|$False> .EXAMPLE Start-windreamSession -windreamServer -windreamDomain -windreamUserName -windreamUserPassword .EXAMPLE Start-windreamSession -windreamVersion "3.6" -windream64Bit <$True|$False> -windreamServer -windreamDomain -windreamUserName -windreamUserPassword #> [cmdletbinding()] Param ( [Parameter(Mandatory=$False,HelpMessage='Optional Parameter. Give Server Name of a specific windream Server, you want to connect with. Otherwise default will be choosen.')] [AllowEmptyString()] [String]$windreamServer=$NULL, [Parameter(Mandatory=$False,HelpMessage='Optional Parameter. Give a Windows Domain (which is windream autorisiered) for Impersonation Login Methode. Otherwise current domain will be choosen.')] [ValidateNotNullOrEmpty()] [String]$windreamDomain=$NULL, [Parameter(Mandatory=$False,HelpMessage='Optional Parameter. Give a Windows User Name (who is windream autorisiered) for Impersonation Login Methode. Otherwise current User Name will be choosen.')] [ValidateNotNullOrEmpty()] [String]$windreamUserName=$NULL, [Parameter(Mandatory=$False,HelpMessage='Optional Parameter. Give the Password for the User name you set bevor in $windreamUserName for Impersonation Login Methode.')] [ValidateNotNullOrEmpty()] [String]$windreamUserPassword=$NULL, [Parameter(Mandatory=$False,HelpMessage='Optional Parameter. Give the minimum Version of windream, to make sure later function calls will work (Example 3.6, 4.0, ...). Default Value is 4.0.')] [ValidateNotNullOrEmpty()] [String]$windreamVersion="4.0", [Parameter(Mandatory=$False,HelpMessage='Optional Parameter. Available since windream 6.0. Necessary, if following functions need 64Bit Integer Support (eg. GetWMObjectByIdEx64(WMEntity aWMEntity, int64 aWMObjectId)). Default Value is $False')] [ValidateSet($True,$False)] [Switch]$windream64Bit=$False, [Parameter(Mandatory=$False,HelpMessage='Optional Parameter. If windream Drive Letter was set in the windream Client config, call this Parameter using $true, to check the Mapping in the Windows Explorer. Default Value is "$True".')] [ValidateSet($True,$False)] [Switch]$windreamDriveLetterMapping=$True ) #end param #Clear Error Variable $error.clear() #Checking if "Write-LogFile" Module was loaded IF (Get-Module -Name "Write-LogFile") { Write-Host "DEBUG Info: Write-LogFile - Module exists." #If Function was called without an explicite windream Server, try to get the default IF (!$windreamServer) { Write-Logfile -LogLine " " Write-Logfile -LogLine "Function was called without a specific windream Server," Write-Logfile -LogLine "trying to find the default windream Server." Try { $windreamServerBrowser = New-Object -ComObject "WMOBrws.ServerBrowser" -ErrorAction Stop $windreamServer = $windreamServerBrowser.GetCurrentServer() Write-Logfile -LogLine "windream Server is: $windreamServer" } #end try Catch { Write-Logfile -LogLine "Cannot create Object from windream Class WMOBrws.ServerBrowser!" Write-Logfile -LogLine "And/Or unable the retrieve default windream Server!" $windreamServer = $NULL } #end catch } #end if #If Function was called with an explicite windream Server ELSE { Write-Logfile -LogLine " " Write-Logfile -LogLine "Function was called with specific windream Server, trying to connect to $windreamServer" Write-Logfile -LogLine "Remember: Since windream 4.0, this will only work if you are using Impersonation Login Methode." Write-LogFile -Logline "windreamServer, windreamDomain, windreamUserName and windreamUserPassword" Try { $windreamServerBrowser = New-Object -ComObject "WMOBrws.ServerBrowser" -ErrorAction Stop } #end try Catch { Write-Logfile -LogLine "Cannot create Object from windream Class WMOBrws.ServerBrowser!" Return $False } #end catch } #end else #Go ahead if windreamServer is not null and is reachable via network ping IF (($windreamServer) -and (Test-Connection -ComputerName $windreamServer -Count 1 -ErrorAction SilentlyContinue)) { Try { $windreamConnect = New-Object -ComObject "Windream.WMConnect" -ErrorAction Stop $windreamConnect.MinReqVersion = $windreamVersion $windreamConnect.ClientSupports64BitID = $windream64Bit Write-Logfile -LogLine "windream Connect created!" } # end try Catch { Write-Logfile -LogLine "Cannot create Object from windream Class Windream.WMSession!" Write-Logfile -LogLine $Error Return $False } #end catch Try { $windreamSession = New-Object -ComObject "Windream.WMSession" #-ErrorAction Stop Write-Logfile -LogLine "windream Session created!" } #end try Catch { Write-Logfile -LogLine "Cannot create Object from windream Class Windream.WMSession!" Write-Logfile -LogLine $Error Return $False } #end catch #Use Impersonation Login methode, if these three Parameters are set (-gt $NULL). IF (($windreamUserName) -and ($windreamUserPassword) -and ($windreamDomain)) { Try { $windreamConnect.ModuleId = 9 $windreamImpersonation = New-Object -ComObject "WMOTool.WMUserIdentity" -ErrorAction Stop $windreamImpersonation.aDomain = $windreamDomain $windreamImpersonation.aPassword = $windreamUserPassword $windreamImpersonation.aServerName = $windreamServer $windreamImpersonation.aUserName = $windreamUserName Try { Write-Logfile -LogLine "Try to Login Impersonation" $windreamSession = $windreamConnect.Login($windreamImpersonation) } #end try Catch { Write-Logfile -LogLine "Cannot Login into windream Session!" Write-Logfile -LogLine $Error Return $False } #end catch } #end try Catch { Write-Logfile -LogLine "Cannot create Object from windream Class WMOTool.WMUserIdentity!" Write-Logfile -LogLine $Error Return $False } #end catch } #end if #If Impersonation Login methode was not set, try to login with current username, domain and password ELSE { Try { Write-Logfile -LogLine "Try to Login with current credentials." $windreamConnect.LoginSession($windreamSession) } #end try Catch { Write-Logfile -LogLine "Cannot Login into windream Session!" Write-Logfile -LogLine $Error Return $False } #end catch } #end else IF ($windreamSession.aLoggedin -eq $True) { Write-Logfile -LogLine " " Write-Logfile -LogLine "Connection established" Write-Logfile -LogLine "You are connected with $($windreamConnect.ServerName) as $($windreamSession.aUser.aName)" Write-Host "DEBUG Info: Do not forget to Logout Session after finished work." $windreamDriveLetter = $windreamServerBrowser.GetServerValue($windreamServer, "DriveLetter", 0) IF ($windreamDriveLetter) { Write-Logfile -LogLine "The configured windream Drive Letter is: $windreamDriveLetter" Set-Variable -Name windreamDriveLetter -Value $windreamDriveLetter -Scope Global -ErrorAction SilentlyContinue $PathTest = Test-Path -Path "$windreamDriveLetter`:\" -PathType Container IF ($PathTest -eq $True) { Write-Logfile -LogLine "The configured windream Drive Letter seems to be connected, as well!" } #end if ELSE { Write-Logfile -LogLine "The configured windream Drive Letter seems not to be connected!" IF ($windreamDriveLetterMapping -eq $True) { Try { Write-Logfile -LogLine "Try to connect the windream Drive ..." Remove-PSDrive -Name "$windreamDriveLetter" -Force -Scope Global -ErrorAction SilentlyContinue $PathTest = New-PSDrive -Name "$windreamDriveLetter" -PSProvider FileSystem -Root "\\windream\objects" -Persist -Scope Global -ErrorAction Stop Write-Logfile -LogLine "windream Drive Letter $PathTest should be connected, now!" } #end try Catch { Write-Logfile -LogLine "Cannot map the windream Drive!" Set-Variable -Name windreamDriveLetter -Value $NULL -Scope Global -ErrorAction SilentlyContinue } #end catch } #end if ELSE { Write-Logfile -LogLine "Will not try to map the windream Drive..." } #end else } #end else } #end if ELSE { Write-Logfile -LogLine "WARNING: There is no windream Drive Letter configured." Write-Logfile -LogLine "This could cause access problems!" Write-Logfile -LogLine "Please check your windream Alias Settings!" } #end else Return $windreamSession } #end if ELSE { Write-Logfile -LogLine " " Write-Logfile -LogLine "Connection refused" Write-Logfile -LogLine $Error Return $False } #end else } #end if #Stop if windream Server was unavailable ELSE { Write-Logfile -LogLine "Cannot retrieve windream Server to connect with or test connection failed!" Write-Logfile -LogLine $Error Return $False } #end else } #end if ELSE { Write-Host "" Write-Host "DEBUG Info: Write-LogFile - Module does not exist!" Write-Host "DEBUG Info: Please load the Module and try again, running this Function/Module!" Write-Host "DEBUG Info: Exiting, because of this Issue." EXIT } #end else } #end function