<commit_before><#
1. Navigate to your working folder:
    cd C:\Search\Scripts 

2. Configure environment variables. Run this script as administrator (!!!) to set up environment variables permanently. Machine or User - up to you        
    .\Search.Environment.ps1

3. Set up dev
    .\Search.Setup.ps1 -ESVersion "5.1.1" -ClusterName "OhMySearch-Dev"

4. Configure production cluster:
    cd E:\Search
    .\Search.Setup.ps1 -ESVersion "5.1.1" -ClusterName "OhMySearch-Prod" -SetEnvironment `
        -DiscoveryHosts @("10.1.0.178","10.1.0.179") -AsService `
        -LicenceFilePath "E:\Search\company-license-<your code>.json"

5. Debug locally:
    cmd.exe /C "C:\Search\elasticsearch-5.1.1\bin\elasticsearch.bat"
    $ESVersion = "5.0.0-beta1"

#>
[CmdletBinding(PositionalBinding=$false, DefaultParameterSetName = "SearchSet")] #SupportShouldProcess=$true, 
Param(
    [Parameter(Mandatory=$false, Position = 0, ValueFromRemainingArguments=$true , HelpMessage = 'current version of Elastic Search products is the same starting from 5.0')]
    [string]$ESVersion,
    [string]$ClusterName,
    [string[]]$DiscoveryHosts = @(),
    [switch]$AsService,
    [switch]$UseSnapshot,
    [string]$LicenceFilePath,
    [switch]$SetEnvironment
)

[string]$scripLocation = (Get-Variable MyInvocation).Value.PSScriptRoot
if ($scripLocation -eq ""){$scripLocation = (Get-Location).Path}
function Main(){
    Clear-Host

    if ($env:JAVA_HOME -eq $null) # since alpha 3 $env:JAVA_HOME is not the only option. It accepts java.exe path. Not implemented yet
    {
        Echo "Please install last version of Java and configure working catalog"
    }
    
    if ($UseSnapshot.IsPresent){#daily builds works for kibana only :(
        #DownLoadAndExtract -Url "http://download.elastic.co/elasticsearch/elasticsearch-snapshot/elasticsearch-5.0.0-snapshot.zip"
        #DownLoadAndExtract -Url "http://download.elastic.co/logstash/logstash-snapshot/logstash-5.0.0-snapshot.zip"
        #DownLoadAndExtract -Url "http://download.elastic.co/kibana/kibana-snapshot/kibana-5.0.0-snapshot-windows.zip"
        #DownLoadAndExtract -Url "http://download.elastic.co/beats/winlogbeat-snapshot/winlogbeat-5.0.0-snapshot-windows-64.zip"
    }
    else{ #Official release
        DownLoadAndExtract -Url "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-$($ESVersion).zip"
        DownLoadAndExtract -Url "https://artifacts.elastic.co/downloads/logstash/logstash-$($ESVersion).zip"
        DownLoadAndExtract -Url "https://artifacts.elastic.co/downloads/kibana/kibana-$($ESVersion)-windows-x86.zip"
        DownLoadAndExtract -Url "https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-$($ESVersion)-windows-x86_64.zip"
    }
    
    #configure file processors: tesseract, image, etc. Install-Module described here: http://psget.net
    Install-Module -ModuleUrl "https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Image-module-caa4405a/file/62238/1/Image.zip" -Verbose

    #For server OS you need to activate Desktop-Experience to avoid this exception in Search.Json.ps1 : Retrieving the COM class factory for component with CLSID {00000000-0000-0000-0000-000000000000} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
    try{
        Add-WindowsFeature Desktop-Experience 
    }
    catch{}

    <#you can use 3.02, but I use 3.05 dev. First of all, get sources: https://github.com/tesseract-ocr/tesseract/archive/master.zip
    if you do not want to compile sources, get ready to use binaries from here: https://www.dropbox.com/s/8t54mz39i58qslh/tesseract-3.05.00dev-win32-vc19.zip?dl=1
    finally, fill tessdata subfolder with your languages training content: https://github.com/justin/tesseract-ocr/tree/master/tessdata
        for example, english training data could be found in file named "eng.traineddata"
    #>


    #DownLoadAndInstall -Url "http://www.imagemagick.org/download/binaries/ImageMagick-7.0.2-3-Q16-x64-dll.exe"
    #DownLoadAndInstall -Url "https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs919/gs919w64.exe"
    #DownLoadAndInstall -Url "https://inkscape.org/en/gallery/item/3956/inkscape-0.91-x64.msi" or https://inkscape.org/en/gallery/item/3938/Inkscape-0.91-1-win64.7z



    #configure elastice settings 
    #For settings that you do not wish to store in the configuration file, you can use the value ${prompt.text} or ${prompt.secret} 
    $config = Get-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\elasticsearch.yml" -Raw
    $config = $config.Replace("#cluster.name: my-application", " cluster.name: $ClusterName")
    #Environment variables referenced with the ${...} notation within the configuration file will be replaced with the value of the environment variable, for instance:
    $config = $config.Replace("#node.name: node-1", " node.name: `${HOSTNAME}") #$env:COMPUTERNAME
    $config = $config.Replace("#path.data: /path/to/data", " path.data: $env:SEARCH_HOME\Data")
    $config = $config.Replace("#path.logs: /path/to/logs", " path.logs: $env:LOG_DIR")

    #default is localhost:9200, otherwise use cluster
    #command line configuration
    #configure settings from command line:
    #cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch.bat" -Enode.name=node_1 -Ecluster.name=my_cluster 

    if($DiscoveryHosts.Length -gt 0){
        #The network.host setting also understands some special values such as _local_, _site_, _global_ and modifiers like :ip4 and :ip6, details of which can be found in the section called Special values for network.hostedit.
        $ipV4 = (Test-Connection -ComputerName (hostname) -Count 1  | Select IPV4Address).IPV4Address.IPAddressToString
        $config = $config.Replace("# network.host: 192.168.0.1", " network.host: $ipV4") # `${ES_NETWORK_HOST}

        $otherhosts = ''
        for ([int]$i = 0; $i -lt $DiscoveryHosts.Length; $i++){
            if ($DiscoveryHosts[$i] -notin $ipV4, $env:COMPUTERNAME){
                $otherhosts += ',"'+$DiscoveryHosts[$i]+'"'
            }
        }
        $otherhosts = $otherhosts.Trim(',')
        if ($otherhosts -ne ""){
            $config = $config.Replace("# discovery.zen.ping.unicast.hosts: [""host1"", ""host2""]", " discovery.zen.ping.unicast.hosts: [$otherhosts]")
            #If discovery.zen.minimum_master_nodes is not set when Elasticsearch is running in production mode, an exception will be thrown which will prevent the node from starting.
            $config = $config.Replace("# discovery.zen.minimum_master_nodes: 3", " discovery.zen.minimum_master_nodes: $($DiscoveryHosts.Length / 2 + 1)") #quorum of master-eligible nodes
        }
    }

    #It is, however, possible to start more than one node on the same server by mistake and to be completely unaware that this problem exists. 
    #To prevent more than one node from sharing the same data directory, it is advisable to add the following setting:
    $config = $config.Replace("# node.max_local_storage_nodes: 1", " node.max_local_storage_nodes: 1")

    #add some trusted sources of data:
    if ($config.Contains("repositories.url.allowed_urls") -eq $false){
        $config += "`r`n repositories.url.allowed_urls: [""http://download.elastic.co/*""]"
    }

    Set-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\elasticsearch.yml" $config

    #configure logging setting. 
    if (Test-Path "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\logging.yml")
    {
        $logging = Get-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\logging.yml" -Raw
        if ($logging -ne $null)
        {
            #$logging = $logging.Replace("es.logger.level: INFO", "es.logger.level: DEBUG")
            #For example, this will create a daily rolling deprecation log file in your log directory. Check this file regularly, especially when you intend to upgrade to a new major version.:
            $logging = $logging.Replace("deprecation: INFO, deprecation_log_file", "deprecation: DEBUG, deprecation_log_file")
            Set-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\logging.yml" $logging
        }
    }

    <#The service installer requires that the thread stack size setting be configured in jvm.options before you install the service. 
        On 32-bit Windows, you should add -Xss320k to the jvm.options file, 
        and on 64-bit Windows you should add -Xss1m to the jvm.options file.#>
    $jvmoptoins = Get-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\jvm.options" -Raw
    if ([Environment]::Is64BitProcess -eq $true -and $jvmoptoins.Contains("-Xss1m") -eq $false){
        $jvmoptoins += "`r`n-Xss1m"
        Set-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\jvm.options" $jvmoptoins
    }
    elseif (([Environment]::Is64BitProcess -eq $null -or [Environment]::Is64BitProcess -eq $false) -and $jvmoptoins.Contains("-Xss320k") -eq $false){
        $jvmoptoins += "`r`n-Xss320k"
        Set-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\jvm.options" $jvmoptoins
    }

    <#initial heap size [268435456] not equal to maximum heap size [2147483648]; this can cause resize pauses and prevents mlockall from locking the entire heap
        Min and max heap size must be equal to start ES 5.0.0-alpha2#>
    if (([Environment]::Is64BitProcess -eq $null -or [Environment]::Is64BitProcess -eq $false) -and $jvmoptoins.Contains("-Xms1g") -eq $false){
        $jvmoptoins += "`r`n-Xms1g"
        Set-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\jvm.options" $jvmoptoins
    }
    if (([Environment]::Is64BitProcess -eq $null -or [Environment]::Is64BitProcess -eq $false) -and $jvmoptoins.Contains("-Xmx1g") -eq $false){
        $jvmoptoins += "`r`n-Xmx1g"
        Set-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\jvm.options" $jvmoptoins
    }

    #check current versions
    cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch.bat" -V
    cmd.exe /C "$env:SEARCH_HOME\kibana-$ESVersion-windows-x86\bin\kibana.bat" -V
    java -version

    <#dotnet new
    dotnet restore
    dotnet run#>
    dotnet --version

    #configure windows services
    if ($AsService.IsPresent){
        #uninstall service
        try{
            cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\service.bat" stop Elastic-Search
            cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\service.bat" remove Elastic-Search
            #"Waiting 5 seconds to allow service to be uninstalled."
            Start-Sleep -s 5  
        }
        catch{}

        #install service
        cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\service.bat" install #Elastic-Search
        cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\service.bat" start #Elastic-Search
        #or run it manually in command line deamon mode. To stop use Cntrl+C
        #cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\service.bat" -d

        <#unfortunately kibana as a service is not available yet in v5
            cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\kibana.bat" install Kibana

        but we can simulate that service. run the following command in admin mode:
            cmd.exe /C sc create "ElasticSearch Kibana" binPath= "$env:SEARCH_HOME\kibana-$ESVersion-windows\bin\kibana.bat" depend= "Elastic-Search" 
            sc start "ElasticSearch Kibana"
        ignore messages that service is not accesable. it is already running, just check http://localhost:5601
        to uninstall run:
            sc stop "ElasticSearch Kibana"
            sc delete "ElasticSearch Kibana"
        Or use our script function provided below:
            Setup-Service -serviceName "ElasticSearch Kibana" -exePath "$env:SEARCH_HOME\kibana-$ESVersion-windows\bin\kibana.bat" -uninstall -install -start #-cred $cred
        #>
    }
    else{
        Echo "To run elastic service use this command in separate window: cmd.exe /C '$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch.bat'"
    }

    <#configure plugins. 
        cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch-plugin.bat list"
        cmd.exe /C "$env:SEARCH_HOME\kibana-$ESVersion-windows\bin\kibana-plugin.bat list"<commit_msg>change settings for run as service mode<commit_after><#
1. Navigate to your working folder:
    cd C:\Search\Scripts 

2. Configure environment variables. Run this script as administrator (!!!) to set up environment variables permanently. Machine or User - up to you        
    .\Search.Environment.ps1

3. Set up dev
    .\Search.Setup.ps1 -ESVersion "5.1.2" -ClusterName "OhMySearch-Dev"

4. Configure production cluster:
    cd E:\Search
    .\Search.Setup.ps1 -ESVersion "5.1.2" -ClusterName "OhMySearch-Prod" -SetEnvironment `
        -DiscoveryHosts @("10.1.0.178","10.1.0.179") -AsService `
        -LicenceFilePath "E:\Search\company-license-<your code>.json"

5. Debug locally:
    cmd.exe /C "C:\Search\elasticsearch-5.1.2\bin\elasticsearch.bat"
    $ESVersion = "5.1.2"

#>
[CmdletBinding(PositionalBinding=$false, DefaultParameterSetName = "SearchSet")] #SupportShouldProcess=$true, 
Param(
    [Parameter(Mandatory=$false, Position = 0, ValueFromRemainingArguments=$true , HelpMessage = 'current version of Elastic Search products is the same starting from 5.0')]
    [string]$ESVersion,
    [string]$ClusterName,
    [string[]]$DiscoveryHosts = @(),
    [switch]$AsService,
    [switch]$UseSnapshot,
    [string]$LicenceFilePath,
    [switch]$SetEnvironment
)

[string]$scripLocation = (Get-Variable MyInvocation).Value.PSScriptRoot
if ($scripLocation -eq ""){$scripLocation = (Get-Location).Path}
function Main(){
    Clear-Host

    if ($env:JAVA_HOME -eq $null) # since alpha 3 $env:JAVA_HOME is not the only option. It accepts java.exe path. Not implemented yet
    {
        Echo "Please install last version of Java and configure working catalog"
    }
    
    if ($UseSnapshot.IsPresent){#daily builds works for kibana only :(
        #DownLoadAndExtract -Url "http://download.elastic.co/elasticsearch/elasticsearch-snapshot/elasticsearch-5.0.0-snapshot.zip"
        #DownLoadAndExtract -Url "http://download.elastic.co/logstash/logstash-snapshot/logstash-5.0.0-snapshot.zip"
        #DownLoadAndExtract -Url "http://download.elastic.co/kibana/kibana-snapshot/kibana-5.0.0-snapshot-windows.zip"
        #DownLoadAndExtract -Url "http://download.elastic.co/beats/winlogbeat-snapshot/winlogbeat-5.0.0-snapshot-windows-64.zip"
    }
    else{ #Official release
        DownLoadAndExtract -Url "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-$($ESVersion).zip"
        DownLoadAndExtract -Url "https://artifacts.elastic.co/downloads/logstash/logstash-$($ESVersion).zip"
        DownLoadAndExtract -Url "https://artifacts.elastic.co/downloads/kibana/kibana-$($ESVersion)-windows-x86.zip"
        DownLoadAndExtract -Url "https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-$($ESVersion)-windows-x86_64.zip"
    }
    
    #configure file processors: tesseract, image, etc. Install-Module described here: http://psget.net
    Install-Module -ModuleUrl "https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Image-module-caa4405a/file/62238/1/Image.zip" -Verbose

    #For server OS you need to activate Desktop-Experience to avoid this exception in Search.Json.ps1 : Retrieving the COM class factory for component with CLSID {00000000-0000-0000-0000-000000000000} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
    try{
        Add-WindowsFeature Desktop-Experience 
    }
    catch{}

    <#you can use 3.02, but I use 3.05 dev. First of all, get sources: https://github.com/tesseract-ocr/tesseract/archive/master.zip
    if you do not want to compile sources, get ready to use binaries from here: https://www.dropbox.com/s/8t54mz39i58qslh/tesseract-3.05.00dev-win32-vc19.zip?dl=1
    finally, fill tessdata subfolder with your languages training content: https://github.com/justin/tesseract-ocr/tree/master/tessdata
        for example, english training data could be found in file named "eng.traineddata"
    #>


    #DownLoadAndInstall -Url "http://www.imagemagick.org/download/binaries/ImageMagick-7.0.2-3-Q16-x64-dll.exe"
    #DownLoadAndInstall -Url "https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs919/gs919w64.exe"
    #DownLoadAndInstall -Url "https://inkscape.org/en/gallery/item/3956/inkscape-0.91-x64.msi" or https://inkscape.org/en/gallery/item/3938/Inkscape-0.91-1-win64.7z



    #configure elastice settings 
    #For settings that you do not wish to store in the configuration file, you can use the value ${prompt.text} or ${prompt.secret} 
    $config = Get-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\elasticsearch.yml" -Raw
    $config = $config.Replace("#cluster.name: my-application", " cluster.name: $ClusterName")
    #Environment variables referenced with the ${...} notation within the configuration file will be replaced with the value of the environment variable, for instance:
    #do not use host name in service mode to avoide Exception in thread "main" ception: Could not resolve placeholder 'HOSTNAME'
    $config = $config.Replace("#node.name: node-1", " node.name: $($env:COMPUTERNAME)") #`${HOSTNAME}
    $config = $config.Replace("#path.data: /path/to/data", " path.data: $env:SEARCH_HOME\Data")
    $config = $config.Replace("#path.logs: /path/to/logs", " path.logs: $env:LOG_DIR")

    #default is localhost:9200, otherwise use cluster
    #command line configuration
    #configure settings from command line:
    #cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch.bat" -Enode.name=node_1 -Ecluster.name=my_cluster 

    if($DiscoveryHosts.Length -gt 0){
        #The network.host setting also understands some special values such as _local_, _site_, _global_ and modifiers like :ip4 and :ip6, details of which can be found in the section called Special values for network.hostedit.
        $ipV4 = (Test-Connection -ComputerName (hostname) -Count 1  | Select IPV4Address).IPV4Address.IPAddressToString
        $config = $config.Replace("# network.host: 192.168.0.1", " network.host: $ipV4") # `${ES_NETWORK_HOST}

        $otherhosts = ''
        for ([int]$i = 0; $i -lt $DiscoveryHosts.Length; $i++){
            if ($DiscoveryHosts[$i] -notin $ipV4, $env:COMPUTERNAME){
                $otherhosts += ',"'+$DiscoveryHosts[$i]+'"'
            }
        }
        $otherhosts = $otherhosts.Trim(',')
        if ($otherhosts -ne ""){
            $config = $config.Replace("# discovery.zen.ping.unicast.hosts: [""host1"", ""host2""]", " discovery.zen.ping.unicast.hosts: [$otherhosts]")
            #If discovery.zen.minimum_master_nodes is not set when Elasticsearch is running in production mode, an exception will be thrown which will prevent the node from starting.
            $config = $config.Replace("# discovery.zen.minimum_master_nodes: 3", " discovery.zen.minimum_master_nodes: $($DiscoveryHosts.Length / 2 + 1)") #quorum of master-eligible nodes
        }
    }

    #It is, however, possible to start more than one node on the same server by mistake and to be completely unaware that this problem exists. 
    #To prevent more than one node from sharing the same data directory, it is advisable to add the following setting:
    $config = $config.Replace("# node.max_local_storage_nodes: 1", " node.max_local_storage_nodes: 1")

    #add some trusted sources of data:
    if ($config.Contains("repositories.url.allowed_urls") -eq $false){
        $config += "`r`n repositories.url.allowed_urls: [""http://download.elastic.co/*""]"
    }

    Set-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\elasticsearch.yml" $config

    #configure logging setting. 
    if (Test-Path "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\logging.yml")
    {
        $logging = Get-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\logging.yml" -Raw
        if ($logging -ne $null)
        {
            #$logging = $logging.Replace("es.logger.level: INFO", "es.logger.level: DEBUG")
            #For example, this will create a daily rolling deprecation log file in your log directory. Check this file regularly, especially when you intend to upgrade to a new major version.:
            $logging = $logging.Replace("deprecation: INFO, deprecation_log_file", "deprecation: DEBUG, deprecation_log_file")
            Set-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\logging.yml" $logging
        }
    }
    $env:JAVA_HOME
    <#The service installer requires that the thread stack size setting be configured in jvm.options before you install the service. 
        On 32-bit Windows, you should add -Xss320k to the jvm.options file, 
        and on 64-bit Windows you should add -Xss1m to the jvm.options file.
        
        While a JRE can be used for the Elasticsearch service, due to its use of a client VM 
        (as opposed to a server JVM which offers better performance for long-running applications) 
        its usage is discouraged and a warning will be issued.
    #>
    $jvmoptoins = Get-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\jvm.options" -Raw
    if ([Environment]::Is64BitProcess -eq $true -and $jvmoptoins.Contains("-Xss1m") -eq $false){
        $jvmoptoins += "`r`n-Xss1m"
        Set-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\jvm.options" $jvmoptoins
    }
    elseif (([Environment]::Is64BitProcess -eq $null -or [Environment]::Is64BitProcess -eq $false) -and $jvmoptoins.Contains("-Xss320k") -eq $false){
        $jvmoptoins += "`r`n-Xss320k"
        Set-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\jvm.options" $jvmoptoins
    }

    <#initial heap size [268435456] not equal to maximum heap size [2147483648]; this can cause resize pauses and prevents mlockall from locking the entire heap
        Min and max heap size must be equal to start ES 5.0.0-alpha2#>
    if (([Environment]::Is64BitProcess -eq $null -or [Environment]::Is64BitProcess -eq $false) -and $jvmoptoins.Contains("-Xms1g") -eq $false){
        $jvmoptoins += "`r`n-Xms1g"
        Set-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\jvm.options" $jvmoptoins
    }
    if (([Environment]::Is64BitProcess -eq $null -or [Environment]::Is64BitProcess -eq $false) -and $jvmoptoins.Contains("-Xmx1g") -eq $false){
        $jvmoptoins += "`r`n-Xmx1g"
        Set-Content "$env:SEARCH_HOME\elasticsearch-$ESVersion\config\jvm.options" $jvmoptoins
    }

    #check current versions
    cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch.bat" -V
    cmd.exe /C "$env:SEARCH_HOME\kibana-$ESVersion-windows-x86\bin\kibana.bat" -V
    java -version

    <#dotnet new
    dotnet restore
    dotnet run#>
    dotnet --version

    #configure windows services
    if ($AsService.IsPresent){
        #uninstall service
        try{
            cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch-service.bat" stop Elastic-Search
            cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch-service.bat" remove Elastic-Search
            #"Waiting 5 seconds to allow service to be uninstalled."
            Start-Sleep -s 5  
        }
        catch{
            sc.exe delete "Elastic-Search"
            sc.exe delete elasticsearch-service-x64 #default name
        }

        #install service
        cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch-service.bat" install Elastic-Search
        #manager allowes to check current status of service and java options in UI
        #cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch-service.bat" manager Elastic-Search

        cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch-service.bat" start Elastic-Search
        #or run it manually in command line deamon mode. To stop use Cntrl+C
        #cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch-service.bat" -d

        <#unfortunately kibana as a service is not available yet in v5
            cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\kibana.bat" install Kibana

        but we can simulate that service. run the following command in admin mode:
            cmd.exe /C sc create "ElasticSearch Kibana" binPath= "$env:SEARCH_HOME\kibana-$ESVersion-windows\bin\kibana.bat" depend= "Elastic-Search" 
            sc start "ElasticSearch Kibana"
        ignore messages that service is not accesable. it is already running, just check http://localhost:5601
        to uninstall run:
            sc stop "ElasticSearch Kibana"
            sc delete "ElasticSearch Kibana"
        Or use our script function provided below:
            Setup-Service -serviceName "ElasticSearch Kibana" -exePath "$env:SEARCH_HOME\kibana-$ESVersion-windows\bin\kibana.bat" -uninstall -install -start #-cred $cred
        #>
    }
    else{
        Echo "To run elastic service use this command in separate window: cmd.exe /C '$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch.bat'"
    }

    <#configure plugins. 
        cmd.exe /C "$env:SEARCH_HOME\elasticsearch-$ESVersion\bin\elasticsearch-plugin.bat list"
        cmd.exe /C "$env:SEARCH_HOME\kibana-$ESVersion-windows\bin\kibana-plugin.bat list"
{'fraction_non_alphanumeric': 0.12111703796673988, 'fraction_numerical': 0.021611233134609352, 'mean_word_length': 3.432719054242003, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 22, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3272927', 'n_tokens_mistral': 8307, 'n_tokens_neox': 7924, 'n_words': 2015}