![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | unexpected truncation for [System.DateTime] Just run another test with asynchronous script and get time truncated from [System.DateTime] parameter! I guess, I shouldn’t be playing with PowerShell on Sunday... Sorry, the scripts are quite long now… {Cut here} #------------------------------------------------------------------------------ # g1.ps1 file [System.Collections.ArrayList] $script ipelines = new-objectSystem.Collections.ArrayList; [string] $g2_script = "./g2.ps1" #------------------------------------------------------------------------------ function BeginAsync([string] $command) { #[diagnostics.process]::start("PowerShell.exe", "-NoLogo -Command M:\Backup\CatalogNavigator\backup_sql.ps1 $backupPath $serverName $databases") [System.Management.Automation.Runspaces.RunspaceConfiguration] $config = [System.Management.Automation.Runspaces.RunspaceConfiguration]::Create(); [System.Management.Automation.Runspaces.Runspace] $runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($host, $config); $runspace.Open() [scriptblock] $script = Invoke-Expression $command [System.Management.Automation.Runspaces.Pipeline] $pipeline = $runspace.CreatePipeline($script); $pipeline.Input.Close(); $pipeline.InvokeAsync(); $script ipelines.Add($pipeline);} #------------------------------------------------------------------------------ function EndAsync { while($script ipelines.Count -gt 0) {for($private:i = 0; $i -lt $script ipelines.Count; $i++) {[System.Management.Automation.Runspaces.Pipeline] $pipeline = $script ipelines[$i]if($pipeline.PipelineStateInfo.State -ne [System.Management.Automation.Runspaces.PipelineState]::Running) { #$pipelineReader = $pipeline.Output #$pipelineReader.Count foreach($private:result in $pipeline.Output.ReadToEnd()) { Write-Host ("1-- Value {0}" -f $result) } foreach($private:result in $pipeline.Error.ReadToEnd()) { Write-Host ("2-- Value {0}" -f $result) } $script ipelines.RemoveAt($i);break } Write-Host ("Waiting for {0} " -f $script ipelines.Count)Start-Sleep -milli 500 } } } #------------------------------------------------------------------------------ function DoAsync([DateTime] $a) { $a BeginAsync "{ $g2_script $a }" Start-Sleep -milli 100 } #------------------------------------------------------------------------------ #-- Main script --------------------------------------------------------------- #------------------------------------------------------------------------------ [DateTime] $private:a = [DateTime]::Now; DoAsync $a EndAsync return #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ {Cut here} #------------------------------------------------------------------------------ # g2.ps1 file param ( [DateTime] $a ) #param ( $a ) #------------------------------------------------------------------------------ #-- Main script --------------------------------------------------------------- #------------------------------------------------------------------------------ $a #$a.GetType() #return Start-Sleep -milli 200 [DateTime] $private:b = [System.DateTime]::Now Start-Sleep -milli 200 @" A started $a. A elapsed time $($b - $a)<br> B started $b. B elapsed time $([System.DateTime]::Now - $b)<br> Total elapsed time $([System.DateTime]::Now - $a)<br> "@ return #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ {Cut here} Output: > ./g1 Sunday, October 29, 2006 1:52:12 PM ^^^^^^^^^^^^^^^^^^^^^^^^ Time is here 0 Waiting for 1 1-- Value 10/29/2006 12:00:00 AM ^^^^^^^^^^^^^^^^^^^^^^^^ Time is truncated! 1-- Value A started 10/29/2006 00:00:00. A elapsed time 13:52:12.6406865<br> B started 10/29/2006 13:52:12. B elapsed time 00:00:00.2031224<br> Total elapsed time 13:52:12.8438089<br> |
My System Specs![]() |
| | #2 (permalink) |
| | RE: unexpected truncation for [System.DateTime] Eigene, Thanks to you post “PoSh quiz”, I figured out what was wrong in mine! Apparently, [DateTime] gets converted to string “10/29/2006 20:36:31” and then become two parameters “10/29/2006” and “20:36:31”! Thus the next question, how to pass any parameter to the script block for further execution? I.e. pass it without conversion to string. # g3.ps1 file [System.Collections.ArrayList] $script ipelines = new-objectSystem.Collections.ArrayList; [string] $g2_script = "./g2.ps1" #------------------------------------------------------------------------------ function test([DateTime] $a) { Start-Sleep -milli 200 [DateTime] $private:b = [System.DateTime]::Now Start-Sleep -milli 200 @" A started $a. A elapsed time $($b - $a)<br> B started $b. B elapsed time $([System.DateTime]::Now - $b)<br> Total elapsed time $([System.DateTime]::Now - $a)<br> "@ } #------------------------------------------------------------------------------ #-- Main script --------------------------------------------------------------- #------------------------------------------------------------------------------ [DateTime] $private:a = [DateTime]::Now; [scriptblock] $scriptA = Invoke-Expression "{ test `"$a`" }" &$scriptA "------------------------------------------------------------------------------" [scriptblock] $scriptB = Invoke-Expression "{ test $a }" &$scriptB return #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ Output: PS X:\PowerShell\0\CatalogNavigator.PS> ./g3 A started 10/29/2006 20:36:31. A elapsed time 00:00:01.1007521<br> B started 10/29/2006 20:36:32. B elapsed time 00:00:00.2031224<br> Total elapsed time 00:00:01.3038745<br> ------------------------------------------------------------------------------ A started 10/29/2006 00:00:00. A elapsed time 20:36:32.5226217<br> B started 10/29/2006 20:36:32. B elapsed time 00:00:00.2031224<br> Total elapsed time 20:36:32.7257441<br> "OK" wrote: > Just run another test with asynchronous script and get time truncated from > [System.DateTime] parameter! > > I guess, I shouldn’t be playing with PowerShell on Sunday... > > Sorry, the scripts are quite long now… > > {Cut here} > #------------------------------------------------------------------------------ > # g1.ps1 file > > > [System.Collections.ArrayList] $script ipelines = new-object> System.Collections.ArrayList; > > [string] $g2_script = "./g2.ps1" > > #------------------------------------------------------------------------------ > function BeginAsync([string] $command) { > > #[diagnostics.process]::start("PowerShell.exe", "-NoLogo -Command > M:\Backup\CatalogNavigator\backup_sql.ps1 $backupPath $serverName $databases") > > [System.Management.Automation.Runspaces.RunspaceConfiguration] $config = > [System.Management.Automation.Runspaces.RunspaceConfiguration]::Create(); > [System.Management.Automation.Runspaces.Runspace] $runspace = > [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($host, $config); > $runspace.Open() > > [scriptblock] $script = Invoke-Expression $command > > [System.Management.Automation.Runspaces.Pipeline] $pipeline = > $runspace.CreatePipeline($script); > $pipeline.Input.Close(); > $pipeline.InvokeAsync(); > > $script ipelines.Add($pipeline);> } > > > #------------------------------------------------------------------------------ > function EndAsync { > > while($script ipelines.Count -gt 0) {> > for($private:i = 0; $i -lt $script ipelines.Count; $i++) {> > [System.Management.Automation.Runspaces.Pipeline] $pipeline = > $script ipelines[$i]> if($pipeline.PipelineStateInfo.State -ne > [System.Management.Automation.Runspaces.PipelineState]::Running) { > > #$pipelineReader = $pipeline.Output > #$pipelineReader.Count > > foreach($private:result in $pipeline.Output.ReadToEnd()) { > Write-Host ("1-- Value {0}" -f $result) > } > > foreach($private:result in $pipeline.Error.ReadToEnd()) { > Write-Host ("2-- Value {0}" -f $result) > } > > $script ipelines.RemoveAt($i);> break > } > Write-Host ("Waiting for {0} " -f $script ipelines.Count)> Start-Sleep -milli 500 > } > } > } > > > #------------------------------------------------------------------------------ > function DoAsync([DateTime] $a) { > > $a > > BeginAsync "{ $g2_script $a }" > Start-Sleep -milli 100 > } > > > #------------------------------------------------------------------------------ > #-- Main script > --------------------------------------------------------------- > #------------------------------------------------------------------------------ > > [DateTime] $private:a = [DateTime]::Now; > > DoAsync $a > > EndAsync > > > return > > #------------------------------------------------------------------------------ > #------------------------------------------------------------------------------ > #------------------------------------------------------------------------------ > > {Cut here} > #------------------------------------------------------------------------------ > # g2.ps1 file > > param ( [DateTime] $a ) > > #param ( $a ) > > #------------------------------------------------------------------------------ > #-- Main script > --------------------------------------------------------------- > #------------------------------------------------------------------------------ > > $a > > > #$a.GetType() > #return > > Start-Sleep -milli 200 > [DateTime] $private:b = [System.DateTime]::Now > Start-Sleep -milli 200 > > > @" > A started $a. A elapsed time $($b - $a)<br> > B started $b. B elapsed time $([System.DateTime]::Now - $b)<br> > Total elapsed time $([System.DateTime]::Now - $a)<br> > "@ > > > > return > > #------------------------------------------------------------------------------ > #------------------------------------------------------------------------------ > #------------------------------------------------------------------------------ > {Cut here} > > > Output: > > > ./g1 > > Sunday, October 29, 2006 1:52:12 PM > ^^^^^^^^^^^^^^^^^^^^^^^^ Time is here > 0 > Waiting for 1 > 1-- Value 10/29/2006 12:00:00 AM > ^^^^^^^^^^^^^^^^^^^^^^^^ Time is truncated! > 1-- Value A started 10/29/2006 00:00:00. A elapsed time 13:52:12.6406865<br> > B started 10/29/2006 13:52:12. B elapsed time 00:00:00.2031224<br> > Total elapsed time 13:52:12.8438089<br> > |
My System Specs![]() |
| | #3 (permalink) |
| | RE: unexpected truncation for [System.DateTime] "OK" wrote: > Thus the next question, how to pass any parameter to the script block for > further execution? I.e. pass it without conversion to string. I'm not sure whether this is what you mean, but it may help you to understand how to deal with scriptblocks and parameters in general: # a function that takes a scriptblock as parameter and executes it function start-scriptblock([scriptblock]$sb) {&$sb} # a scriptblock variable that also can take a string parameter and prints it $mysb = { param([string]$p1) 'hello world'; get-date; "parameter1: $p1" } # testing the function PS> start-scriptblock {&$mysb 123} hello world Montag, 30. Oktober 2006 09:59:44 parameter1: 123 Hope that helps. -- greetings dreeschkind |
My System Specs![]() |
| | #4 (permalink) |
| | RE: unexpected truncation for [System.DateTime] Dreeschkind, Thanks a lot! You posts always raise more questions then they answer! :-) I like them! They make me think! I made another post PoSh quiz II And I’ll make another test scritp to show what I actually want! "dreeschkind" wrote: > "OK" wrote: > > > Thus the next question, how to pass any parameter to the script block for > > further execution? I.e. pass it without conversion to string. > > I'm not sure whether this is what you mean, but it may help you to > understand how to deal with scriptblocks and parameters in general: > > # a function that takes a scriptblock as parameter and executes it > function start-scriptblock([scriptblock]$sb) {&$sb} > > # a scriptblock variable that also can take a string parameter and prints it > $mysb = { param([string]$p1) 'hello world'; get-date; "parameter1: $p1" } > > # testing the function > PS> start-scriptblock {&$mysb 123} > hello world > > Montag, 30. Oktober 2006 09:59:44 > parameter1: 123 > > Hope that helps. > > -- > greetings > dreeschkind |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Unexpected system restore | General Discussion | |||
| Unexpected shut down of the guest operating system. | Virtual Server | |||
| System Restore Unexpected Error 8007007B | Vista General | |||
| Unexpected results with [System.IO.File] methods | PowerShell | |||
| unexpected conversion for [System.DateTime] | PowerShell | |||