Guest User

Untitled

a guest
Sep 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Create Object With Cast
  2. function CreateWith-Cast
  3. {
  4.     $ex = [System.Diagnostics.ProcessStartInfo]@{fileName="cmd"}
  5.     [System.Diagnostics.Process]::Start($ex)
  6. }
  7.  
  8. #Use New-Object
  9. function Create-NormalObject
  10. {
  11.     $ex2 = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList "cmd"
  12.     [System.Diagnostics.Process]::Start($ex2)
  13. }
  14.  
  15. #Cast
  16. Measure-Command -Expression {$ex = [System.Diagnostics.ProcessStartInfo]@{fileName="cmd"}; [System.Diagnostics.Process]::Start($ex)}
  17.  
  18. #New-Object
  19. Measure-Command -Expression {$ex2 = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList "cmd"; [System.Diagnostics.Process]::Start($ex2)}
  20.  
  21.  
  22.  
  23.  
  24. #New-Object Automatically Calls the Default Constructor for us
  25. $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo
  26. $test.GetType()
  27.  
  28. #IsPublic IsSerial Name                                     BaseType
  29. #-------- -------- ----                                     --------
  30. #True     False    ProcessStartInfo                         System.Object
  31.  
  32. #If you want you can overload the defualt constructor in two ways when using New-Object:
  33. $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo("cmd")
  34. $test
  35.  
  36. #Verb                    :
  37. #Arguments               :
  38. #CreateNoWindow          : False
  39. #EnvironmentVariables    : {PROCESSOR_LEVEL, CommonProgramFiles, PROCESSOR_IDENTIFIER, TEMP...}
  40. #RedirectStandardInput   : False
  41. #RedirectStandardOutput  : False
  42. #RedirectStandardError   : False
  43. #StandardErrorEncoding   :
  44. #StandardOutputEncoding  :
  45. #UseShellExecute         : True
  46. #Verbs                   : {}
  47. #UserName                :
  48. #Password                :
  49. #Domain                  :
  50. #LoadUserProfile         : False
  51. #FileName                : cmd
  52. #WorkingDirectory        :
  53. #ErrorDialog             : False
  54. #ErrorDialogParentHandle : 0
  55. #WindowStyle             : Normal
  56.  
  57. #If you use two arguements you will see that it uses the next constructor
  58. $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo("cmd", "ramdom args")
  59. $test
  60.  
  61. #Verb                    :
  62. #Arguments               : ramdom args
  63. #CreateNoWindow          : False
  64. #EnvironmentVariables    : {PROCESSOR_LEVEL, CommonProgramFiles, PROCESSOR_IDENTIFIER, TEMP...}
  65. #RedirectStandardInput   : False
  66. #RedirectStandardOutput  : False
  67. #RedirectStandardError   : False
  68. #StandardErrorEncoding   :
  69. #StandardOutputEncoding  :
  70. #UseShellExecute         : True
  71. #Verbs                   : {}
  72. #UserName                :
  73. #Password                :
  74. #Domain                  :
  75. #LoadUserProfile         : False
  76. #FileName                : cmd
  77. #WorkingDirectory        :
  78. #ErrorDialog             : False
  79. #ErrorDialogParentHandle : 0
  80. #WindowStyle             : Normal
  81.  
  82. #Alternatively you can use the arguementlist parameter
  83. $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList "cmd"
  84. $test
  85.  
  86. #Verb                    :
  87. #Arguments               :
  88. #CreateNoWindow          : False
  89. #EnvironmentVariables    : {PROCESSOR_LEVEL, CommonProgramFiles, PROCESSOR_IDENTIFIER, TEMP...}
  90. #RedirectStandardInput   : False
  91. #RedirectStandardOutput  : False
  92. #RedirectStandardError   : False
  93. #StandardErrorEncoding   :
  94. #StandardOutputEncoding  :
  95. #UseShellExecute         : True
  96. #Verbs                   : {}
  97. #UserName                :
  98. #Password                :
  99. #Domain                  :
  100. #LoadUserProfile         : False
  101. #FileName                : cmd
  102. #WorkingDirectory        :
  103. #ErrorDialog             : False
  104. #ErrorDialogParentHandle : 0
  105. #WindowStyle             : Normal
  106.  
  107.  
  108. $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList "cmd","random args"
  109. $test
  110.  
  111. #Verb                    :
  112. #Arguments               : ramdom args
  113. #CreateNoWindow          : False
  114. #EnvironmentVariables    : {PROCESSOR_LEVEL, CommonProgramFiles, PROCESSOR_IDENTIFIER, TEMP...}
  115. #RedirectStandardInput   : False
  116. #RedirectStandardOutput  : False
  117. #RedirectStandardError   : False
  118. #StandardErrorEncoding   :
  119. #StandardOutputEncoding  :
  120. #UseShellExecute         : True
  121. #Verbs                   : {}
  122. #UserName                :
  123. #Password                :
  124. #Domain                  :
  125. #LoadUserProfile         : False
  126. #FileName                : cmd
  127. #WorkingDirectory        :
  128. #ErrorDialog             : False
  129. #ErrorDialogParentHandle : 0
  130. #WindowStyle             : Normal
  131.  
  132. #If you specify too many arguements and a valid constructor cannot be found you will get an error.
  133. $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList "cmd","random args", "invalidoverload"
  134. $test
  135.  
  136. #New-Object : Cannot find an overload for "ProcessStartInfo" and the argument count: "3".
  137. #At line:1 char:9
  138. #+ $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList " ...
  139. #+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  140. #    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
  141. #    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
  142.  
  143. #You can also set properties straight after creation by using the -Property parameter which accepts a hash table where the keys are the name of the properties and
  144. #the values are what you want to set the properties to.
  145. $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList "cmd","random args" -Property @{CreateNoWindow=$True;RedirectStandardOutput=$True;UseShellExecute=$False}
  146. $test
  147.  
  148. #Verb                    :
  149. #Arguments               : random args
  150. #CreateNoWindow          : True
  151. #EnvironmentVariables    : {PROCESSOR_LEVEL, CommonProgramFiles, PROCESSOR_IDENTIFIER, TEMP...}
  152. #RedirectStandardInput   : False
  153. #RedirectStandardOutput  : True
  154. #RedirectStandardError   : False
  155. #StandardErrorEncoding   :
  156. #StandardOutputEncoding  :
  157. #UseShellExecute         : False
  158. #Verbs                   : {}
  159. #UserName                :
  160. #Password                :
  161. #Domain                  :
  162. #LoadUserProfile         : False
  163. #FileName                : cmd
  164. #WorkingDirectory        :
  165. #ErrorDialog             : False
  166. #ErrorDialogParentHandle : 0
  167. #WindowStyle             : Normal
  168.  
  169.  
  170. #Call No Constructor (struct??)
  171. $test = [System.Diagnostics.ProcessStartInfo]
  172. $test.GetType()
  173.  
  174. #IsPublic IsSerial Name                                     BaseType
  175. #-------- -------- ----                                     --------
  176. #False    True     RuntimeType                              System.Type
  177.  
  178. #if you try to do $test.Redir nothing comes up in intellisense, this is because at this point there is no properties with that name
  179.  
  180. #However if you call a constuctor like:
  181. $test = [System.Diagnostics.ProcessStartInfo]("cmd")
  182. $test.GetType()
  183.  
  184. #IsPublic IsSerial Name                                     BaseType
  185. #-------- -------- ----                                     --------
  186. #True     False    ProcessStartInfo                         System.Object
  187.  
  188. #You will get options in intellisense, in addition i noticed that the base type has changed.
  189.  
  190. #You cant overload with the cast method
  191. $test = [System.Diagnostics.ProcessStartInfo]("cmd","aaa")
  192.  
  193. #Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Diagnostics.ProcessStartInfo".
  194. #At line:1 char:1
  195. #+ $test = [System.Diagnostics.ProcessStartInfo]("cmd","aaa")
  196. #+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  197. #    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
  198. #    + FullyQualifiedErrorId : ConvertToFinalInvalidCastException
  199.  
  200. #Call Default Constructor and Init Properties
  201. $test =[System.Diagnostics.ProcessStartInfo]@{CreateNoWindow=$True;RedirectStandardOutput=$True;fileName="cmd";UseShellExecute=$False}
  202. $test.GetType()
Add Comment
Please, Sign In to add comment