Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .Synopsis
  3.    Script to import fileset onto BTLS supported SIMS servers. By AshG@BTLS
  4. .DESCRIPTION
  5.    This is intended to be deployed with SCCM. In the package should be this and the required fileset (as a .zip).  This script
  6.    will attempt to gain the schoolnumber from the registry, machinename or the connect.ini path.  It will then use this to
  7.    extract the password for the westfield user, and then use these along with identified variables to run teh deployfileset.exe
  8.    utility.
  9. .EXAMPLE
  10.    import-fileset -verbose
  11.  
  12. #>
  13.  
  14. Function import-fileset
  15. {
  16. [CmdletBinding()]
  17. param ([string] $fileset)
  18.  
  19. if (${env:ProgramFiles(x86)}){$deploypath = 'C:\Program Files (x86)\sims\SIMS .net\deployfileset.exe'} else {$deploypath = 'C:\Program Files\sims\SIMS .net\deployfileset.exe'}
  20.  
  21. $files = @("c:\shared\sims\connect.ini","d:\shared\sims\connect.ini")
  22. foreach ($file in $files)
  23. {
  24.     $user = "westfield"
  25.     if (test-path $file -ErrorAction SilentlyContinue)
  26.     {
  27.         $lines = gc $file
  28.         foreach ($line in $lines)
  29.             {
  30.                if ( $line | select-string -pattern "servername")
  31.                 {
  32.                     $servername = $line.split("=")
  33.                     $var =  "Server:"+$servername[1]
  34.                     write-verbose $var            
  35.                 }
  36.                 if ( $line | select-string -pattern "databasename")
  37.                 {
  38.                     $db = $line.split("=")
  39.                     $var2 = "db:"+$db[1]
  40.                     write-verbose $var2              
  41.                 }
  42.             }  
  43.  
  44.     # check DM"
  45.    
  46.     if (test-path "C:\Program Files (x86)\SIMS\SIMS .net Document Server\dmconfig.exe" -erroraction silentlycontinue ) {$dmconfig = "C:\Program Files (x86)\SIMS\SIMS .net Document Server\dmconfig.exe"}
  47.     if (test-path "C:\Program Files\SIMS\SIMS .net Document Server\dmconfig.exe" -erroraction silentlycontinue ) {$dmconfig = "C:\Program Files\SIMS\SIMS .net Document Server\dmconfig.exe"}
  48.     $result = & $dmconfig /T | select -last 1
  49.     if ($result -eq "Server is running")
  50.     {write-verbose "DmConfig responded OK"} else {write-verbose "DmConfig responded OK"}
  51.     $key = "HKLM:\SOFTWARE\Westfield\Stationdetails"
  52.    
  53.    # transpose schoolnumber
  54.     $value = (get-itemproperty -path $key -Name SchoolID -ea silentlycontinue).SchoolID
  55.     if ($value.Length -eq '5')
  56.         {
  57.             write-verbose "Schoolnumber identified as $value"
  58.         } else {
  59.             Write-Warning "Schoolnumber lookup failed"
  60.             # look to see if first 5 digits of computername are numbers, and if so, try that.
  61.             $var = $env:COMPUTERNAME
  62.             $snip =  $var.Substring(0,5)
  63.        
  64.             # Not all schoolnumbers are numbers, but most are ...
  65.             Add-type -AssemblyName microsoft.visualbasic
  66.             if ([microsoft.visualbasic.information]::IsNumeric($snip) -eq $true)
  67.                 {
  68.                     Write-verbose "Found a number in the computername - valid number"
  69.                     $value = $snip
  70.                 }
  71.                     else
  72.                 {
  73.                     if ($snip.substring(0,2) -eq "LG")
  74.                         {
  75.                             write-verbose "Found a LG number in the computername - valid number"
  76.                             $value = $snip
  77.                         }else {
  78.                             write-verbose "Was not able to find a number in the registry or the servername."
  79.                         }
  80.                 }
  81.         }
  82.  
  83.     # Number found, now need to transpose
  84.     $pw = @()
  85.         if ($value)
  86.         {
  87.  
  88.             for ($test = 0; $test -lt 5; $test++)
  89.             {
  90.                 $num = $value.Substring($test,1)
  91.                 $letter = [char](96 + $num)
  92.                 $pw+=$letter
  93.             }
  94.             $string =$pw[0]+$pw[1]+$pw[2]+$pw[3]+$pw[4]
  95.             write-verbose "Transposed password is $string"
  96.             $pwd = "west$string"
  97.             write-verbose "full password is $pwd"
  98.         }
  99.    
  100.     write-verbose "removing btls_fileset directory on c:"
  101.     remove-item -path c:\btls_fileset -force -recurse -ea silentlycontinue
  102.    
  103.     write-verbose "Exporting .zip file"
  104.     if(!$PSScriptRoot)
  105.         { $location = get-location
  106.         $PSScriptRoot = $location.path }
  107.     $path = $PSscriptroot
  108.     $zipfile = gci -path $path -filter *.zip
  109.     $fullpath = join-path $path $zipfile   
  110.     unzip $fullpath c:\btls_fileset
  111.    
  112.     $myfileset = gci -path c:\btls_fileset -filter *.mfs
  113.     $fileset = join-path c:\btls_fileset $myfileset
  114.    
  115.     # call command
  116.     $myserver= $servername[1]
  117.     $mydb=$db[1]
  118.     write-verbose "Attempting with: /server=$myserver /database=$mydb /user=$user /password=$pwd /fileset=$fileset /debug /validate"
  119.     & $deploypath /server=$myserver /database=$mydb /user=$user /password=$pwd /fileset=$fileset /debug /validate
  120.         }
  121.     }
  122. }
  123.  
  124. function Unzip
  125. {
  126.     param([string]$zipfile, [string]$outpath)
  127.    
  128.     if ($PSVersionTable.PSVersion.Major -lt 5)
  129.     {
  130.         write-verbose "Less than powershell 5. Unpacking file manually"
  131.         $shell = New-Object -ComObject shell.application
  132.         $zip = $shell.NameSpace($zipfile)
  133.         MkDir($outpath)
  134.         foreach ($item in $zip.items())
  135.             {
  136.                 $shell.Namespace($outpath).CopyHere($item)
  137.              }
  138.     }
  139.     else
  140.     {
  141.         Add-Type -AssemblyName System.IO.Compression.FileSystem
  142.         [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
  143.     }
  144. }
  145.  
  146. #import-fileset -fileset 'C:\Users\Administrator\Desktop\1003-statutoryreturns-autumn2018_fileset\1003-StatutoryReturns-Autumn2018_Fileset.mfs'  -verbose
  147. import-fileset -verbose
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement