Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Create Object With Cast
- function CreateWith-Cast
- {
- $ex = [System.Diagnostics.ProcessStartInfo]@{fileName="cmd"}
- [System.Diagnostics.Process]::Start($ex)
- }
- #Use New-Object
- function Create-NormalObject
- {
- $ex2 = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList "cmd"
- [System.Diagnostics.Process]::Start($ex2)
- }
- #Cast
- Measure-Command -Expression {$ex = [System.Diagnostics.ProcessStartInfo]@{fileName="cmd"}; [System.Diagnostics.Process]::Start($ex)}
- #New-Object
- Measure-Command -Expression {$ex2 = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList "cmd"; [System.Diagnostics.Process]::Start($ex2)}
- #New-Object Automatically Calls the Default Constructor for us
- $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo
- $test.GetType()
- #IsPublic IsSerial Name BaseType
- #-------- -------- ---- --------
- #True False ProcessStartInfo System.Object
- #If you want you can overload the defualt constructor in two ways when using New-Object:
- $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo("cmd")
- $test
- #Verb :
- #Arguments :
- #CreateNoWindow : False
- #EnvironmentVariables : {PROCESSOR_LEVEL, CommonProgramFiles, PROCESSOR_IDENTIFIER, TEMP...}
- #RedirectStandardInput : False
- #RedirectStandardOutput : False
- #RedirectStandardError : False
- #StandardErrorEncoding :
- #StandardOutputEncoding :
- #UseShellExecute : True
- #Verbs : {}
- #UserName :
- #Password :
- #Domain :
- #LoadUserProfile : False
- #FileName : cmd
- #WorkingDirectory :
- #ErrorDialog : False
- #ErrorDialogParentHandle : 0
- #WindowStyle : Normal
- #If you use two arguements you will see that it uses the next constructor
- $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo("cmd", "ramdom args")
- $test
- #Verb :
- #Arguments : ramdom args
- #CreateNoWindow : False
- #EnvironmentVariables : {PROCESSOR_LEVEL, CommonProgramFiles, PROCESSOR_IDENTIFIER, TEMP...}
- #RedirectStandardInput : False
- #RedirectStandardOutput : False
- #RedirectStandardError : False
- #StandardErrorEncoding :
- #StandardOutputEncoding :
- #UseShellExecute : True
- #Verbs : {}
- #UserName :
- #Password :
- #Domain :
- #LoadUserProfile : False
- #FileName : cmd
- #WorkingDirectory :
- #ErrorDialog : False
- #ErrorDialogParentHandle : 0
- #WindowStyle : Normal
- #Alternatively you can use the arguementlist parameter
- $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList "cmd"
- $test
- #Verb :
- #Arguments :
- #CreateNoWindow : False
- #EnvironmentVariables : {PROCESSOR_LEVEL, CommonProgramFiles, PROCESSOR_IDENTIFIER, TEMP...}
- #RedirectStandardInput : False
- #RedirectStandardOutput : False
- #RedirectStandardError : False
- #StandardErrorEncoding :
- #StandardOutputEncoding :
- #UseShellExecute : True
- #Verbs : {}
- #UserName :
- #Password :
- #Domain :
- #LoadUserProfile : False
- #FileName : cmd
- #WorkingDirectory :
- #ErrorDialog : False
- #ErrorDialogParentHandle : 0
- #WindowStyle : Normal
- $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList "cmd","random args"
- $test
- #Verb :
- #Arguments : ramdom args
- #CreateNoWindow : False
- #EnvironmentVariables : {PROCESSOR_LEVEL, CommonProgramFiles, PROCESSOR_IDENTIFIER, TEMP...}
- #RedirectStandardInput : False
- #RedirectStandardOutput : False
- #RedirectStandardError : False
- #StandardErrorEncoding :
- #StandardOutputEncoding :
- #UseShellExecute : True
- #Verbs : {}
- #UserName :
- #Password :
- #Domain :
- #LoadUserProfile : False
- #FileName : cmd
- #WorkingDirectory :
- #ErrorDialog : False
- #ErrorDialogParentHandle : 0
- #WindowStyle : Normal
- #If you specify too many arguements and a valid constructor cannot be found you will get an error.
- $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList "cmd","random args", "invalidoverload"
- $test
- #New-Object : Cannot find an overload for "ProcessStartInfo" and the argument count: "3".
- #At line:1 char:9
- #+ $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList " ...
- #+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # + CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
- # + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
- #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
- #the values are what you want to set the properties to.
- $test = New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList "cmd","random args" -Property @{CreateNoWindow=$True;RedirectStandardOutput=$True;UseShellExecute=$False}
- $test
- #Verb :
- #Arguments : random args
- #CreateNoWindow : True
- #EnvironmentVariables : {PROCESSOR_LEVEL, CommonProgramFiles, PROCESSOR_IDENTIFIER, TEMP...}
- #RedirectStandardInput : False
- #RedirectStandardOutput : True
- #RedirectStandardError : False
- #StandardErrorEncoding :
- #StandardOutputEncoding :
- #UseShellExecute : False
- #Verbs : {}
- #UserName :
- #Password :
- #Domain :
- #LoadUserProfile : False
- #FileName : cmd
- #WorkingDirectory :
- #ErrorDialog : False
- #ErrorDialogParentHandle : 0
- #WindowStyle : Normal
- #Call No Constructor (struct??)
- $test = [System.Diagnostics.ProcessStartInfo]
- $test.GetType()
- #IsPublic IsSerial Name BaseType
- #-------- -------- ---- --------
- #False True RuntimeType System.Type
- #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
- #However if you call a constuctor like:
- $test = [System.Diagnostics.ProcessStartInfo]("cmd")
- $test.GetType()
- #IsPublic IsSerial Name BaseType
- #-------- -------- ---- --------
- #True False ProcessStartInfo System.Object
- #You will get options in intellisense, in addition i noticed that the base type has changed.
- #You cant overload with the cast method
- $test = [System.Diagnostics.ProcessStartInfo]("cmd","aaa")
- #Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Diagnostics.ProcessStartInfo".
- #At line:1 char:1
- #+ $test = [System.Diagnostics.ProcessStartInfo]("cmd","aaa")
- #+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # + CategoryInfo : InvalidArgument: (:) [], RuntimeException
- # + FullyQualifiedErrorId : ConvertToFinalInvalidCastException
- #Call Default Constructor and Init Properties
- $test =[System.Diagnostics.ProcessStartInfo]@{CreateNoWindow=$True;RedirectStandardOutput=$True;fileName="cmd";UseShellExecute=$False}
- $test.GetType()
Add Comment
Please, Sign In to add comment