Function Restart-windreamClient-withLogging { <# .SYNOPSIS Restart windream Client Components via COM Interface .DESCRIPTION If Connection to the windream Server gets interrupted (network loss, eg.), it is neccessery to restart the Client Components. Otherwise you can Stop or Start Client Components with this Function. For a successful performed Action, Function will Return $True, for unccessful $False. .REQUIREMENT General PowerShell V3, windream Client Connectivity (>=V3.6) .REQUIREMENT Assembly .REQUIREMENT Variables windreamControlCenter, windreamIndexService, ServiceTest, Action .REQUIREMENT Variables preSet .REQUIREMENT Functions Write-LogFile .VERSION 1.2.0.0 / 28.09.2018 .PARAMETER Action Determine which Action you want to perform . Default Value is . .PARAMETER ServiceTest Set on $True, if Function should check for a running windream vfs Client Service. If it is and Service is stopped, Function will try to start Service. Default Value is $True. .PARAMETER StartTimeout .EXAMPLE Restart-windreamClient .EXAMPLE Restart-windreamClient -Action Start .EXAMPLE Restart-windreamClient -Action Start -ServiceTest $False #> Param ( [Parameter(Position=0,Mandatory=$False,HelpMessage='Determine which Action you want to perform . Default Value is "Restart".')] [ValidateSet("Stop","Restart","Start")] [String]$Action="Restart", [Parameter(Position=1,Mandatory=$False,HelpMessage='Set on $True, if Function should check for a running windream vfs Client Service. If it is and Service is stopped, Function will try to start Service. Default Value is $True.')] [ValidateSet($True,$False)] [Switch]$ServiceTest=$True, [Parameter(Position=2,Mandatory=$False,HelpMessage='Give the Timeout limit in Seconds for the windream Start/Restart procedure, ValidateRange(1 - 300). Default Value is "10".')] [ValidateRange(1,300)] [INT]$StartTimeout=10 ) #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 Servie Test was enabled (by default true) check if windream vfs Service is running IF ($ServiceTest -eq $True) { Write-LogFile -LogLine " " Write-LogFile -LogLine "Service Test is enabled!" #Check if windream vfs Service is installed Try { [Object]$ServiceTest = $NULL [Object]$ServiceTest = Get-Service -Name vfssvc -ErrorAction Stop Write-LogFile -LogLine "Found Service: vfssvc" Write-LogFile -LogLine "Service is currently: $((Get-Service -Name vfssvc).Status)" } #end try Catch { Write-LogFile -LogLine "WARNING: windream Client seems not to be installed completely." Write-LogFile -LogLine "Missing Service: vfssvc" } #end catch } #end if #If Servie Test is disabled ELSE { Write-LogFile -LogLine " " Write-LogFile -LogLine "Service Test is disabled!" } #end else #Try to create windream Objects Try { [Object]$windreamControlCenter = New-Object -ComObject "Wmcc.ControlCenter" -ErrorAction Stop [Object]$windreamIndexService = New-Object -ComObject "WMIndexServer.WMIdxSvControl" -ErrorAction Stop } #end try Catch { Write-Logfile -LogLine "Cannot create Object from windream Class Wmcc.ControlCenter or WMIndexServer.WMIdxSvControl!" Write-Logfile -LogLine $Error Return $False } #end catch #If Function was called to Stop windream Client Components IF ($Action -like "Stop") { Write-LogFile -LogLine "Stop windream Client Components." #Try to stop windream Client Components Try { Write-Logfile -LogLine "Stopping windream Client Components!" $windreamControlCenter.StartVFSService(0) | Out-Null $windreamIndexService.Shutdown() | Out-Null $windreamControlCenter.ExitCC(0) | Out-Null } #end try Catch { Write-Logfile -LogLine "Cannot stop windream Client Components!" Write-Logfile -LogLine $Error Return $False } #end catch Return $True } #end if #If Function was called to Restart windream Client Components and Service Test was enabled ELSEIF (($Action -like "Restart") -and ($ServiceTest -is [Object])) { #Checking if windream vfs Service is running IF ((Get-Service -Name vfssvc).Status -ne 'running') { Write-LogFile -LogLine "Warning: windream vfs Service is not running!" Try { Write-Logfile -LogLine "Trying to Start/Restart the windream vfs Service!" Stop-Service -Name vfssvc -ErrorAction SilentlyContinue Start-Service -Name vfssvc -ErrorAction Stop } #end try Catch { Write-Logfile -LogLine "Cannot Start/Restart windream vfs Service!" Write-Logfile -LogLine $Error Return $False } #end catch } #end if ELSE { Write-Logfile -LogLine " " Write-LogFile -LogLine "windream vfs Service is running!" } #end else Write-LogFile -LogLine "Restart windream Client Components." #Try to stop windream Client Components Try { Write-Logfile -LogLine "Stopping windream Client Components!" $windreamControlCenter.StartVFSService(0) | Out-Null $windreamIndexService.Shutdown() | Out-Null } #end try Catch { Write-Logfile -LogLine "Cannot stop windream Client Components!" Write-Logfile -LogLine $Error Return $False } #end catch #Try to start windream Client Components Try { Write-Logfile -LogLine "Starting windream Client Components!" $windreamControlCenter.StartVFSService(1) | Wait-Process -Timeout $StartTimeout | Out-Null $windreamIndexService.Start() | Out-Null } #end try Catch { Write-Logfile -LogLine "Cannot start windream Client Components!" Write-Logfile -LogLine $Error Return $False } #end catch Return $True } #end elseif #If Function was called to Restart windream Client Components and Service Test was disabled ELSEIF (($Action -like "Restart") -and ($ServiceTest -is [Switch])) { Write-LogFile -LogLine "Restart windream Client Components." #Try to stop windream Client Components Try { Write-Logfile -LogLine "Stopping windream Client Components!" $windreamControlCenter.StartVFSService(0) | Out-Null $windreamIndexService.Shutdown() | Out-Null } #end try Catch { Write-Logfile -LogLine "Cannot stop windream Client Components!" Write-Logfile -LogLine $Error Return $False } #end catch #Try to start windream Client Components Try { Write-Logfile -LogLine "Starting windream Client Components!" $windreamControlCenter.StartVFSService(1) | Wait-Process -Timeout $StartTimeout | Out-Null $windreamIndexService.Start() | Out-Null } #end try Catch { Write-Logfile -LogLine "Cannot start windream Client Components!" Write-Logfile -LogLine $Error Return $False } #end catch Return $True } #end elseif #If Function was called to Start windream Client Components and Service Test was enabled ELSEIF (($Action -like "Start") -and ($ServiceTest -is [Object])) { #Checking if windream vfs Service is running IF ((Get-Service -Name vfssvc).Status -ne 'running') { Write-LogFile -LogLine "Warning: windream vfs Service is not running!" Try { Write-Logfile -LogLine "Trying to Start/Restart the windream vfs Service!" Stop-Service -Name vfssvc -ErrorAction SilentlyContinue Start-Service -Name vfssvc -ErrorAction Stop } #end try Catch { Write-Logfile -LogLine "Cannot Start/Restart windream vfs Service!" Write-Logfile -LogLine $Error Return $False } #end catch } #end if ELSE { Write-Logfile -LogLine " " Write-LogFile -LogLine "windream vfs Service is running!" } #end else Write-LogFile -LogLine "Start windream Client Components." #Try to start windream Client Components Try { Write-Logfile -LogLine "Starting windream Client Components!" $windreamControlCenter.StartVFSService(1) | Wait-Process -Timeout $StartTimeout | Out-Null $windreamIndexService.Start() | Out-Null } #end try Catch { Write-Logfile -LogLine "Cannot start windream Client Components!" Write-Logfile -LogLine $Error Return $False } #end catch Return $True } #end elseif #If Function was called to Start windream Client Components and Service Test was disabled ELSEIF (($Action -like "Start") -and ($ServiceTest -is [Switch])) { Write-LogFile -LogLine "Start windream Client Components." #Try to start windream Client Components Try { Write-Logfile -LogLine "Starting windream Client Components!" $windreamControlCenter.StartVFSService(1) | Wait-Process -Timeout $StartTimeout | Out-Null $windreamIndexService.Start() | Out-Null } #end try Catch { Write-Logfile -LogLine "Cannot start windream Client Components!" Write-Logfile -LogLine $Error Return $False } #end catch Return $True } #end elseif #If Function was called invalid Values, which should be not possible be the ValidateSet of the Function Parameters ELSE { Write-Logfile -LogLine "Function Call went wrong, please check the ValidateSet" 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