Advertisement
gizmobrat

First Powershell Script

Dec 24th, 2019
136
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 "F:\Sales\D. Blasdell\_Quotes"
  5.  
  6. #define default Vaules
  7. $S0 = ""
  8. $jobname = ""
  9. $contractor = ""
  10. $exit = ""
  11. $Filenames = "F:\Sales\D. Blasdell\Spread Sheets\Folder_names.txt"
  12. $error = 1
  13. #Inport .NET framewoks
  14. Add-Type -AssemblyName System.Windows.Forms
  15. Add-Type -AssemblyName System.Drawing
  16. Add-Type -AssemblyName PresentationCore,PresentationFramework
  17.  
  18. #ERROR loop
  19. Do{
  20. ############################################################################
  21. ######################## Text Box Start ####################################
  22. ############################################################################
  23.  
  24.  
  25.  
  26. #make Box
  27. $form = New-Object System.Windows.Forms.Form
  28. $form.Text = 'Make Quote Folder'
  29. $form.Size = New-Object System.Drawing.Size(500,230)
  30. $form.StartPosition = 'CenterScreen'
  31.  
  32. #Make buttons
  33. $OKButton = New-Object System.Windows.Forms.Button
  34. $OKButton.Location = New-Object System.Drawing.Point(300,150)
  35. $OKButton.Size = New-Object System.Drawing.Size(75,23)
  36. $OKButton.Text = 'OK'
  37. $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  38. $form.AcceptButton = $OKButton
  39. $form.Controls.Add($OKButton)
  40.  
  41.  
  42. $CancelButton = New-Object System.Windows.Forms.Button
  43. $CancelButton.Location = New-Object System.Drawing.Point(400,150)
  44. $CancelButton.Size = New-Object System.Drawing.Size(75,23)
  45. $CancelButton.Text = 'Cancel'
  46. $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  47. $form.Controls.Add($CancelButton)
  48.  
  49.  
  50. #Add Text
  51. $label0 = New-Object System.Windows.Forms.Label
  52. $label0.Location = New-Object System.Drawing.Point (145,20)
  53. $label0.Size = New-Object System.Drawing.Size(250,20)
  54. $label0.Text = "Fill out the Boxes Below with a-z,A-Z,0-9 #_&."
  55. $form.Controls.Add($label0)
  56.  
  57. $label = New-Object System.Windows.Forms.Label
  58. $label.Location = New-Object System.Drawing.Point(10,50)
  59. $label.Size = New-Object System.Drawing.Size(30,20)
  60. $label.Text = 'S0#:'
  61. $form.Controls.Add($label)
  62.  
  63. $label2 = New-Object System.Windows.Forms.Label
  64. $label2.Location = New-Object System.Drawing.Point(10,80)
  65. $label2.Size = New-Object System.Drawing.Size(60,20)
  66. $label2.Text = 'Job Name:'
  67. $form.Controls.Add($label2)
  68.  
  69. $label3 = New-Object System.Windows.Forms.Label
  70. $label3.Location = New-Object System.Drawing.Point(10,110)
  71. $label3.Size = New-Object System.Drawing.Size(60,20)
  72. $label3.Text = 'Contractor:'
  73. $form.Controls.Add($label3)
  74.  
  75. #Add Boxes
  76. $textBox1 = New-Object System.Windows.Forms.TextBox
  77. $textBox1.Location = New-Object System.Drawing.Point(160,50)
  78. $textBox1.Size = New-Object System.Drawing.Size(310,20)
  79. $form.Controls.Add($textBox1)
  80.  
  81.  
  82. $textBox2 = New-Object System.Windows.Forms.TextBox
  83. $textBox2.Location = New-Object System.Drawing.Point(160,80)
  84. $textBox2.Size = New-Object System.Drawing.Size(310,20)
  85. $form.Controls.Add($textBox2)
  86.  
  87. $textBox3 = New-Object System.Windows.Forms.TextBox
  88. $textBox3.Location = New-Object System.Drawing.Point(160,110)
  89. $textBox3.Size = New-Object System.Drawing.Size(310,20)
  90. $form.Controls.Add($textBox3)
  91.  
  92. #Move to top
  93. $form.Top = $true
  94.  
  95. $form.Add_Shown({$textBox1.Select()})
  96. $result = $form.ShowDialog()
  97.  
  98.  
  99.  
  100. #Check inputs
  101. if ($result -eq [System.Windows.Forms.DialogResult]::Cancel){
  102. exit
  103. }
  104. if ($result -eq [System.Windows.Forms.DialogResult]::OK)
  105. {
  106.     $error = 1
  107.     $ErrorMessage = "Invalid Input:"
  108.  
  109. #Error Function
  110. Function is-Good($Vaule,$ErrVaule){
  111.         if($Vaule -eq ""){
  112.             $ErrorMessage = $ErrorMessage + "`n" + "Please Fill in the"+ "$ErrVaule" + "Box"
  113.             $Error = 2
  114.         }
  115.         if ($Vaule -notmatch '[^a-z0-9 _&-]') {
  116.             $ErrorMessage = $ErrorMessage + "`n" + "Please Use A-Z, a-z and 0-9 in the"+"$ErrVaule" +"Box"
  117.             $Error = 2
  118.         }
  119.     }
  120.    
  121.     #Name Holders
  122.     $Temp1 = "S0#"
  123.     $Temp2 = "Job Name"
  124.     $Temp3 = "Contractor"
  125.  
  126.     #Call Error Function
  127.     is-Good($TextBox1.Text,$Temp1)
  128.     is-Good($TextBox2.Text,$Temp2)
  129.     is-Good($TextBox3.Text,$Temp3)
  130.    
  131.     #IF error show error box
  132.     if($Error -eq 2)
  133.     {
  134.     [System.Windows.MessageBox]::Show($ErrorMessage,"Make Quote Folder ERROR",0,16)
  135.     }
  136.     #Export good inputs#>
  137.     if ($error -eq 1){
  138.    
  139.     $S0 = $textBox1.Text
  140.     $jobname = $textBox2.Text
  141.     $contractor = $TextBox3.Text
  142.     $end = $exit
  143.     $error = 0
  144.     }
  145. }
  146. Write-host $Texbox1.Text
  147. ############################################################################
  148. ########################## Text Box End ####################################
  149. ############################################################################
  150. }until ($error -eq 0)
  151.  
  152. #Set combined Veriables
  153. $folder = "$S0" + "_" + "$jobname" + "_" + "$contractor"
  154. $subsubfolder = "./"+"$folder" + "/1 - Estimating Orginal Quote Material"
  155. $takeoffname = "$s0" + "_takeoff.xlsx"
  156. $PILname = "PIL_" + "$S0" +".xlsx"
  157.  
  158.  
  159. #Make master folder
  160. New-Item -ItemType Directory "./$folder"
  161.  
  162. #Make Subfolder
  163. foreach($line in Get-Content $Filenames)
  164. {
  165. New-Item $folder\$line -ItemType Directory
  166. }
  167.  
  168. #Copy Files
  169. Copy-Item '.\_master_takeoff.xlsx' "$subsubfolder\_master_takeoff.xlsx"
  170. Copy-Item '.\PIL_SOXXXXX.xlsx' $subsubfolder
  171. #Rename Files
  172. Rename-Item -Path "$subsubfolder\_master_takeoff.xlsx" -newname $takeoffname
  173. Rename-Item -Path "$subsubfolder\PIL_SOXXXXX.xlsx" -newname $PILname
  174.  
  175. ############################
  176. #Write to new take off file
  177. ############################
  178.  
  179. #Call excel and open file
  180. $xl = New-Object -ComObject excel.application
  181. Start-Sleep -Seconds 5
  182. $xl.Visible = $true
  183. Start-Sleep -Seconds 5
  184. $wb = $xl.Workbooks.Open("$subsubfolder\$takeoffname")
  185. $data = $wb.Worksheets.Item("Storm")
  186. $Data.Cells.Item(1,2) = "$jobname"
  187. $data.Cells.Item(1,7) = "$S0"
  188. $wb.Save()
  189. $xl.Quit()
  190.  
  191. pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement