Advertisement
gizmobrat

Powershell

Dec 25th, 2019
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Start up Config
  2. $UITitle = "Make Quote Folder"
  3. $host.UI.RawUI.WindowTitle = $UITitle
  4. SET-Location '\\opibaltimore\Shared\Sales\D. Blasdell\_QUOTES'
  5. $workingDir = '\\opibaltimore\Shared\Sales\D. Blasdell\_QUOTES'
  6.  
  7. #define default Vaules
  8. $S0 = ""
  9. $jobname = ""
  10. $contractor = ""
  11. $exit = ""
  12. $Filenames = "\\opibaltimore\Shared\Sales\D. Blasdell\Spread Sheets\Folder_names.txt"
  13. $Errorstate = 1
  14. #Inport .NET framewoks
  15. Add-Type -AssemblyName System.Windows.Forms
  16. Add-Type -AssemblyName System.Drawing
  17. Add-Type -AssemblyName PresentationCore,PresentationFramework
  18.  
  19. #ERROR loop
  20. Do{
  21. ############################################################################
  22. ######################## Text Box Start ####################################
  23. ############################################################################
  24.  
  25.  
  26.  
  27. #make Box
  28. $form = New-Object System.Windows.Forms.Form
  29. $form.Text = 'Make Quote Folder'
  30. $form.Size = New-Object System.Drawing.Size(500,230)
  31. $form.StartPosition = 'CenterScreen'
  32.  
  33. #Make buttons
  34. $OKButton = New-Object System.Windows.Forms.Button
  35. $OKButton.Location = New-Object System.Drawing.Point(300,150)
  36. $OKButton.Size = New-Object System.Drawing.Size(75,23)
  37. $OKButton.Text = 'OK'
  38. $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  39. $form.AcceptButton = $OKButton
  40. $form.Controls.Add($OKButton)
  41.  
  42.  
  43. $CancelButton = New-Object System.Windows.Forms.Button
  44. $CancelButton.Location = New-Object System.Drawing.Point(400,150)
  45. $CancelButton.Size = New-Object System.Drawing.Size(75,23)
  46. $CancelButton.Text = 'Cancel'
  47. $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  48. $form.Controls.Add($CancelButton)
  49.  
  50.  
  51. #Add Text
  52. $label0 = New-Object System.Windows.Forms.Label
  53. $label0.Location = New-Object System.Drawing.Point (145,20)
  54. $label0.Size = New-Object System.Drawing.Size(250,20)
  55. $label0.Text = "Fill out the Boxes Below with a-z,A-Z,0-9 #_&."
  56. $form.Controls.Add($label0)
  57.  
  58. $label = New-Object System.Windows.Forms.Label
  59. $label.Location = New-Object System.Drawing.Point(10,50)
  60. $label.Size = New-Object System.Drawing.Size(30,20)
  61. $label.Text = 'S0#:'
  62. $form.Controls.Add($label)
  63.  
  64. $label2 = New-Object System.Windows.Forms.Label
  65. $label2.Location = New-Object System.Drawing.Point(10,80)
  66. $label2.Size = New-Object System.Drawing.Size(60,20)
  67. $label2.Text = 'Job Name:'
  68. $form.Controls.Add($label2)
  69.  
  70. $label3 = New-Object System.Windows.Forms.Label
  71. $label3.Location = New-Object System.Drawing.Point(10,110)
  72. $label3.Size = New-Object System.Drawing.Size(60,20)
  73. $label3.Text = 'Contractor:'
  74. $form.Controls.Add($label3)
  75.  
  76. #Add Boxes
  77. $textBox1 = New-Object System.Windows.Forms.TextBox
  78. $textBox1.Location = New-Object System.Drawing.Point(160,50)
  79. $textBox1.Size = New-Object System.Drawing.Size(310,20)
  80. $form.Controls.Add($textBox1)
  81.  
  82.  
  83. $textBox2 = New-Object System.Windows.Forms.TextBox
  84. $textBox2.Location = New-Object System.Drawing.Point(160,80)
  85. $textBox2.Size = New-Object System.Drawing.Size(310,20)
  86. $form.Controls.Add($textBox2)
  87.  
  88. $textBox3 = New-Object System.Windows.Forms.TextBox
  89. $textBox3.Location = New-Object System.Drawing.Point(160,110)
  90. $textBox3.Size = New-Object System.Drawing.Size(310,20)
  91. $form.Controls.Add($textBox3)
  92.  
  93. #Move to top
  94. $form.Top = $true
  95.  
  96. $form.Add_Shown({$textBox1.Select()})
  97. $result = $form.ShowDialog()
  98.  
  99.  
  100.  
  101. #Check inputs
  102. if ($result -eq [System.Windows.Forms.DialogResult]::Cancel){
  103. exit
  104. }
  105. if ($result -eq [System.Windows.Forms.DialogResult]::OK)
  106. {
  107.     $Errorstate = 1
  108.     $ErrorMessage = "Invalid Input:"
  109. ######################################################################################
  110. #                               NEED HELP HERE                                       #
  111. ######################################################################################
  112. #Error Function
  113. Function is-Good($Vaule,$Errtxt,$Msg,$State){
  114.         if($Vaule -eq ""){
  115.             $Msg = $Msg + "`n" + "Please Fill in the"+ "$Errtxt" + "Box"
  116.             $State = 2
  117.         }
  118.         if ($Vaule -notmatch '[^a-z0-9 _&-]') {
  119.             $State = $State
  120.         }else{
  121.             $Msg = $Msg + "`n" + "Please Use A-Z, a-z and 0-9 in the"+"$Errtxt" +"Box"
  122.             $State = 2
  123.         }
  124.         return $Vaule
  125.         return $Errtxt
  126.         return $Msg
  127.         return $State
  128.     }
  129.    
  130.     #Name Holders
  131.     $Temp1 = "S0#"
  132.     $Temp2 = "Job Name"
  133.     $Temp3 = "Contractor"
  134.  
  135.     #Call Error Function
  136.     is-Good $TextBox1.Text $Temp1 $ErrorMessage $Errorstate
  137.     is-Good $TextBox2.Text $Temp2 $ErrorMessage $Errorstate
  138.     is-Good $TextBox3.Text $Temp3 $ErrorMessage $Errorstate
  139.    
  140.    
  141. ######################################################################################
  142. #                               NEED HELP HERE                                       #
  143. ######################################################################################
  144.    
  145.    
  146.     #IF error show error box
  147.     if($Errorstate -eq 2)
  148.     {
  149.     [System.Windows.MessageBox]::Show($ErrorMessage,"Make Quote Folder ERROR",0,16)
  150.     }
  151.     #Export good inputs#>
  152.     if ($Errorstate -eq 1){
  153.    
  154.     $S0     = $TextBox1.Text
  155.     $jobname    = $TextBox2.Text
  156.     $contractor = $TextBox3.Text
  157.     $end = $exit
  158.     $Errorstate = 0
  159.     }
  160. }
  161. ############################################################################
  162. ########################## Text Box End ####################################
  163. ############################################################################
  164. }until ($Errorstate -eq 0)
  165.  
  166.  
  167. # combine textbox outputs to form the directory
  168. $PILname  = 'PIL_{0}.xlsx' -f $S0
  169. $file     = '{0}_takeoff.xlsx' -f $S0
  170. $folder = '{0}_{1}_{2}' -f $S0, $jobname, $contractor
  171. $subfolder   = '{0}_{1}_{2}\1 - Estimating Orginal Quote Material' -f $S0, $jobname, $contractor
  172. $folderPath = Join-Path -Path $workingDir -ChildPath $folder
  173.  
  174.  
  175. #Make master folder
  176. New-Item -ItemType Directory $folderPath
  177.  
  178. #Make Subfolder
  179. foreach($line in Get-Content $Filenames)
  180. {
  181. New-Item $folder\$line -ItemType Directory
  182. }
  183.  
  184. #File Paths
  185. $subfolderPath = Join-Path -Path $workingDir -ChildPath $subfolder      # --> Full absolute path to the working folder
  186. $filePath   = Join-Path -Path $subfolderPath -ChildPath $file        # --> Full absolute path to the file
  187.  
  188. #Copy and rename master Files
  189. $masterFile = Join-Path -Path $workingDir -ChildPath '_master_takeoff.xlsx'
  190. $pilFile    = Join-Path -Path $workingDir -ChildPath 'PIL_S0XXXXX .xlsx'
  191. Copy-Item -Path $masterFile -Destination (Join-Path -Path $Subfolder -ChildPath $file)
  192. Copy-Item -Path $pilFile -Destination (Join-Path -Path $folderPath -ChildPath $PILname)
  193.  
  194. ############################
  195. #Write to new take off file
  196. ############################
  197.  
  198. # Call excel and open file
  199. $xl = New-Object -ComObject excel.application
  200. $xl.Visible = $true
  201. $wb = $xl.Workbooks.Open($filePath)
  202. $data = $wb.Worksheets.Item("Storm")
  203. $Data.Cells.Item(1,2) = $jobname
  204. $data.Cells.Item(1,7) = $S0
  205. $wb.Save()
  206. $xl.Quit()
  207.  
  208. [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb) | Out-Null
  209. [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl) | Out-Null
  210. [System.GC]::Collect()
  211. [System.GC]::WaitForPendingFinalizers()
  212.  
  213.  
  214. #Open File Exploer
  215. II $Subfolder
  216.  
  217. #Show Complete box
  218. [System.Windows.MessageBox]::Show("Folder $folder was created",$UITitle,0,48)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement