Showing posts with label Bug. Show all posts
Showing posts with label Bug. Show all posts

Wednesday, March 25, 2009

Strange Select-Object Behavior in PowerShell V2 CTP3

In PowerShell v1, the rules are as follows -

  • Select-Object used with –first or –last do not change the object type of the objects passing through
  • Select-Object selecting properties or adding changes the object type to PSCustomObject

Proof -

PS> gps -id $pid | Get-Type # Normal
System.Diagnostics.Process
PS> gps -id $pid | select name,ws | Get-Type # PSCustomObject
System.Management.Automation.PSCustomObject
PS> gps -id $pid | select name,ws | select | Get-Type # PSCustomObject
System.Management.Automation.PSCustomObject
PS> gps -id $pid | select * | Get-Type # Normal (errors are left out)
System.Management.Automation.PSCustomObject
PS> gps -id $pid | select | Get-Type # Normal
System.Diagnostics.Process



 



In PowerShell V2 CTP3 the rules seem to be -




  • Select-Object used with –first or –last works as in V1


  • Select-Object, selecting properties or adding properties, creates a new type called Selected.<originaltype>


  • Select-Object resulting in a Selected.<type> will be turned into a Selected.System.Management.Automation.PSCustomObject! Very strange and very problematic in real-life use



Examples, showing it -




PS> gps -id $pid | Get-Type # normal
System.Diagnostics.Process
PS> gps -id $pid | Select name,ws | Get-Type # Selected.<type>
Selected.System.Diagnostics.Process
PS> gps -id $pid | Select name,ws | Select | Get-Type # Selected...PSCustomObject
Selected.System.Management.Automation.PSCustomObject
PS> gps -id $pid | select | Get-Type # normal
System.Diagnostics.Process
PS> gps -id $pid | select | select | Get-Type # normal
System.Diagnostics.Process
PS> gps -id $pid | select -f 1| select | Get-Type # normal
System.Diagnostics.Process
PS>



 



PS. Get-Type is not a built-in, but this function -




function Get-Type($inputObject) {
process { if ($_ -is [object]) {$_.PSTypeNames[0] } }
end { if ($inputObject -is [object]) { $inputObject.PSTypeNames[0] } }
}

Monday, September 29, 2008

Restarting Windows XP from a startup script is not easy

Yes, they are still out there running the big businesses!

Today, I was messing with a startup script, that should restart the PC - but WMI did not do it and shutdown.exe just returned device is not ready. This should have been fixed in SP2 - at least some KBs claims that - but that is clearly not the case.

Eventually, I found a way around: Create an AT-job to do the restart. That worked. But it was too slow as AT-jobs needs to be postponed at least a minute.

Finally, good old Sysinternals came to the rescue - this time in the shape of psexec. Sysinternals have saved the day many times and back in the late nineties you could simply not lock down and figure out where to relax security on an NT 4 without regmon and filemon. Anyway, back to today. This was how I solved my present problem -

psexec -accepteula -sd shutdown -r -t 5



 



-sd tells it to start the process in system context (the s) and detached (d) as I did not want to wait for the command - I wanted my script to finish as long as it had time to do so.

Monday, April 23, 2007

PowerShell, bug in Stop-Transcript

Stop-Transcript emits an error even if -ErrorAction SilentlyContinue is used -

Stop-transcript –ea SilentlyContinue
Transcription has not been started. Use the start-transcript command to start transcription.
Stop-Transcript : An error occurred stopping transcription: The console host is not currently transcribing.
At line:1 char:16
+ Stop-transcript <<<< –ea SilentlyContinue


This must be a bug. Luckily, Stop-Transcript is not an important command as such.

You can suppress the error message using one of these method.
1) Set the global variable (or in the script scope)
$ErrorActionPreference="SilentlyContinue"

2) Do it locally in your script (better)
& { $ErrorActionPreference="SilentlyContinue"; Stop-Transcript }

3) Trap the message (probably best)
Trap {Continue} Stop-Transcript

If you need to suppress all output append > $null