Advertisement
KySoto

Launch-AccessDB.ps1

Oct 5th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. try
  2. {
  3.    
  4. $appID = $args[0]
  5.     foreach( $a in $args)
  6.     {
  7.         echo $a
  8.     }
  9.  
  10.  
  11.     #try
  12.     #{
  13.         $beta = $args[1]
  14.         if( $beta -eq $null)
  15.         {  
  16.             $beta = 0
  17.         }
  18.         #echo $beta
  19.     #}
  20.     #catch
  21.     #{
  22.         #$beta = 0
  23.         #echo "Beta catch"
  24.     #
  25.     write-output '$appid = '$appID
  26.     #$appID = "7"
  27.     $dt = get-date -format G
  28.     $connection = New-Object System.Data.SqlClient.SqlConnection
  29.     $connection.ConnectionString = "Server=DB2\DB2;Database=VMI;Integrated Security=True;"
  30.     $connection.Open()
  31.     $command = $connection.CreateCommand()
  32.     $command.CommandText = "SELECT LivePath FROM Dict_Application_Info WHERE ApplicationID = $AppID and ProgramType like 'Access%'"
  33.     write-output '$command.CommandText = ' $command.CommandText
  34.     $out = $command.ExecuteScalar()
  35.    
  36.     if (-not ($out -eq $null))
  37.     {
  38.         if ($out -like 'T:*')
  39.         {
  40.             $o = $out.split(':')
  41.            
  42.             $out = "\\vmi.local\data\testing" +$o[1]
  43.         }
  44.         elseif($out -like 'V:*')
  45.         {
  46.             $o = $out.split(':')
  47.             $out = "\\vmi.local\data\vmi" +$o[1]
  48.         }
  49.         elseif ($out -like 'N:*')
  50.         {
  51.             $o = $out.split(':')
  52.             $out = "\\vmi.local\data\Engineering" +$o[1]
  53.         }
  54.         $s = $out.split(".")
  55. echo $s
  56.         if( $s.length -eq 3)
  57.         {
  58.             if ($beta -eq 0 )
  59.             {
  60.                 $result = $s[0] +'.'+ $s[1]+"ToLocal."+$s[2]
  61.                 echo '$result =' + $result
  62.                 $check = $s[0] +'.'+ $s[1]+"ToLocal.l"+$s[2]
  63.                 echo '$check = ' + $check
  64.             }
  65.             else
  66.             {
  67.                 $result = $s[0] +'.'+ $s[1]+"Beta."+$s[2]
  68.                 echo '$result =' + $result
  69.                 $check = $s[0] +'.'+ $s[1]+"Beta.l"+$s[2]
  70.                 echo '$check = ' + $check
  71.             }
  72.             $s2 = $result.split("\")
  73.             $localCopy = "c:\apps\"+$s2[$s2.length-1]
  74. echo $localCopy
  75.         }
  76. write-output '$out = ' $out
  77.         if(-not (test-path $check))
  78.         {
  79.         echo (test-path $result)
  80.             if(test-path $result)
  81.             {
  82.                 $command.CommandText = "SELECT Version FROM Dict_ApplicationChangeLog_Info WHERE ApplicationID = $AppID and beta = $beta order by Version desc"
  83.                 $vers = $command.ExecuteScalar()
  84.                 echo '$vers = '$vers
  85.                 if ($vers -eq $null)
  86.                 {
  87.                     $vers = "1"
  88.                 }
  89.                 $command.CommandText = "SELECT Version FROM VersionTable WHERE ComputerName = '" +  (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name + "' and ApplicationID = $appid And beta = $beta"
  90.                 $Myvers = $command.ExecuteScalar()
  91.                 #echo '$Myvers = '$Myvers
  92.                 #echo '$vers -eq $Myvers '$vers -eq $Myvers
  93.                 if($vers -eq $Myvers)
  94.                 {
  95.                     if (-not(test-path "c:\apps"))
  96.                     {
  97.                         mkdir "C:\apps"
  98.                     }
  99.                     #test-path $localCopy
  100.                     #test-path $result
  101.                     #(-not(test-path $localCopy)) -and (test-path $result)
  102.                     if (-not(test-path $localCopy))
  103.                     {
  104.                         copy-item -path $result -Destination $localCopy -ErrorAction Stop
  105.                     }
  106.                     $command.CommandText = "Update VersionTable SET Used = '$dt' WHERE ComputerName ='" +  (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name + "' AND ApplicationID = $appID And beta = $beta"
  107.                 }
  108.                 else
  109.                 {
  110.                     if (-not(test-path "c:\apps"))
  111.                     {
  112.                         mkdir "C:\apps"
  113.                     }
  114.                     copy-item -path $result -Destination $localCopy -ErrorAction Stop
  115.                     #echo '$Myvers = '$Myvers
  116.                     if ($Myvers -eq $Null)
  117.                     {
  118.                         $command.CommandText = "Insert Into VersionTable VALUES( '" +  (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name + "', $appID , $vers ,'$dt','$dt', $beta)"
  119.                     }
  120.                     else
  121.                     {
  122.                         $command.CommandText = "Update VersionTable SET Version = $vers,Updated = '$dt',Used = '$dt' WHERE ComputerName ='" +  (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name + "' AND ApplicationID = $appID And beta = $beta"
  123.                     }
  124.                 }
  125.                 echo $command.CommandText
  126.                 $command.ExecuteNonQuery()
  127.                 ii $localCopy
  128.             }
  129.             else
  130.             {
  131.                 write-output "Source File Doesnt Exist"
  132.                 if (test-path $localCopy)
  133.                 {
  134.                     ii $localCopy
  135.                 }
  136.                 else
  137.                 {
  138.                     if (test-path "c:\apps")
  139.                     {
  140.                         ii "C:\apps"
  141.                     }
  142.                 }
  143.             }
  144.         }
  145.         else
  146.         {
  147.             write-output "File Open"
  148.             if (test-path $localCopy)
  149.             {
  150.                 ii $localCopy
  151.             }
  152.             else
  153.             {
  154.                 if (test-path "c:\apps")
  155.                 {
  156.                     ii "C:\apps"
  157.                 }
  158.             }
  159.         }
  160.     }
  161.     else
  162.     {
  163.         write-output "Lookup failed"
  164.     }
  165.    
  166.  
  167. }
  168. catch
  169. {
  170.     echo "Catch"
  171.     echo $_.Exception
  172.     if (test-path "c:\apps")
  173.     {
  174.         ii "C:\apps"
  175.     }
  176. }
  177. Finally
  178. {
  179. $connection.Close()
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement