Home   -   Computerkurs   DemoMatrix   Editpointstatic   Folderwatcher   Gipsydrive   Licenses   Shrinkseries   V4   More...  

All   Asm   Bas   C   C++   C#   CSS   HTML   Java   JS   Lisp   OPAL   PHP   Perl   PS   Py   Rb   SQL   Win  

Previous page Home chapter Next page

Trilo - Downtown - Languages

PowerShell

IDEs

I am using three different hosts:

Demos

Some demos may be found in the repository downtown.trilo.de/svn/powershell/trunk/demos.

Snippets

Otherwise, this page has various snippets .. put here from application code .. [todo: integrate snippets into demo program]

Snippet to detect the Host. Why? Depending on the host, different features are available, notably the PowerGUI Script Editor, which is a main tool, does not allow to retrieve keys from the user via keyboard.

         # (sequence 20100612°212102)
         # (A.1) set host specific flags
         $hostname = (Get-Host).name
         $hostname = $host.name
         $readkeyIsAvailable = $true
         $transcriptIsAvailable = $true
         $splitpathIsFine = $true
         # () set flags depending on the console type
         # Inside PowerGUI, several commandlets are not available
         switch ($hostname) {
            'ConsoleHost' {
               # all features available
            } 'PowerGUIScriptEditorHost' {
               $readkeyIsAvailable = $false
               $transcriptIsAvailable = $false
               $splitpathIsFine = $false
            } 'Windows PowerShell ISE Host' {
               $readkeyIsAvailable = $false                    # not sure
               $transcriptIsAvailable = $false
               $splitpathIsFine = $false                       # not sure
            } default {
               write-host ("Yet unknown hostname = " + $hostname)
            }
         }

Snippet to get information about the scripts location

   # (sequence 20100612°212103)
   # (A.2) get path of this script
   if ($splitpathIsFine) {
      $thispath = Split-Path -Parent $MyInvocation.MyCommand.Path
      $thisfile = Split-Path -Leaf $MyInvocation.MyCommand.Path
   } else {
      # PowerGUI has problems with the dot, so let's calculate the absolute path
      $thispath = ''
      $s3 = 'CDEFGHIJKLMNOPQRSTUVWXYZ'
      for ($i = 0; $i -lt $s3.length ; $i++) {
         $s2 = $s3.substring($i,1) + $sTHISPATH_WITHOUT_DRIVE # ':\work\handyxxl24\handies\trunk\importer'
         if (Test-Path $s2) {
            $thispath = $s2
            break
         }
      }
      if ($thispath -eq '') {
         Write-Host ("THISPATH NOT FOUND = ?" + $sTHISPATH_WITHOUT_DRIVE)
         exit-script $exitcode_error "Fatal."
      } else {
         $thisfile = $thispath + '\' + $sTHISFILE_FILENAME
         if (-not (Test-Path $thisfile)) {
            Write-Host ("THISFILE NOT FOUND = " + $thisfile)
            exit-script $exitcode_error "Fatal."
         }
      }
   }

Snippet to make calls to a third party DLL available

   # (sequence 20100612°212104)
   # (A.3) import mysql dotnet provider library
   # Used in provide-database-connection() to instantiate MySql.Data.MySqlClient.MySqlConnection
   $s = ($thispath + "\MySql.Data.dll")
   [void][system.reflection.Assembly]::LoadFrom($s)

   # (A.4) import Another.dll
   $s = ($thispath + $sAnotherDll)
   $sP = resolve-path $s
   if (-not (Test-Path $sP)) {
      Write-Host ("FILE NOT FOUND:" + $sP.ToString())
   }
   [void][system.reflection.Assembly]::LoadFrom($sP)

Snippet ..

               # ..

Snippet ..

               # ..

Imprint : www.trilo.de