Get-World | ConvertTo-PowerShell
Posts tagged hint
See Enums for last failed command
Feb 27th
During last PowerShell Academy session I received a question if it’s possible to have own color as argument for ForegroundColor parameter of Write-Host cmdlet. My answer was – no, and I then showed how to see available values – either in console directly (by providing non-existed value) or to check MSDN help for System.ConsoleColor.
I like the idea of console way – anytime you provide some non-existing value, PowerShell will show you correct values as part of exception raised. I was then thinking about something more – hmm – elegant (?). Usually I just copy name of used enumeration and paste it to GetValues() method of Enum class.
[206]: Write-Host 'text' -ForegroundColor SomeColor Write-Host : Cannot bind parameter 'ForegroundColor'. Cannot convert value "SomeColor" to type "System.ConsoleColor" du e to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeratio n values are "Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan , Red, Magenta, Yellow, White". At line:1 char:35 + Write-Host 'text' -ForegroundColor <<<< SomeColor + CategoryInfo : InvalidArgument: (:) [Write-Host], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteHostCommand [207]: [enum]::GetValues([System.ConsoleColor]) Black DarkBlue DarkGreen DarkCyan DarkRed DarkMagenta DarkYellow Gray DarkGray Blue Green Cyan Red Magenta Yellow White
Then I came with following function.
function Get-EnumValue { [CmdletBinding()] param() Write-Verbose 'Checking last error.' $e = $Global:Error[0].Exception if ($e.ParameterType.IsEnum) { Write-Verbose 'Is Enum.' Write-Verbose "Enum name: $($e.ParameterType)" [enum]::GetValues($e.ParameterType.FullName) } else { Write-Verbose 'Not Enum!' } }
Run it every time you receive an error mentioning that you used bad value for enumeration. Function will check last error (stored in $Error[0]) and if it contains notice that it’s related to enum, function will return all enum values. I created alias genum for that and put it to my profile.
[208]: Write-Host 'text' -ForegroundColor SomeColor Write-Host : Cannot bind parameter 'ForegroundColor'. Cannot convert value "SomeColor" to type "System.ConsoleColor" du e to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeratio n values are "Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan , Red, Magenta, Yellow, White". At line:1 char:35 + Write-Host 'text' -ForegroundColor <<<< SomeColor + CategoryInfo : InvalidArgument: (:) [Write-Host], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteHostCommand [209]: Get-EnumValue Black DarkBlue DarkGreen DarkCyan DarkRed DarkMagenta DarkYellow Gray DarkGray Blue Green Cyan Red Magenta Yellow White [210]: Get-Command -CommandType Something Get-Command : Cannot bind parameter 'CommandType'. Cannot convert value "Something" to type "System.Management.Automation.Com mandTypes" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possib le enumeration values are "Alias, Function, Filter, Cmdlet, ExternalScript, Application, Script, All". At line:1 char:25 + Get-Command -CommandType <<<< sgd + CategoryInfo : InvalidArgument: (:) [Get-Command], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetCommandCommand [211]: genum Alias Function Filter Cmdlet ExternalScript Application Script All [212]: gc notExists Get-Content : Cannot find path 'Dropbox:\PowerShell\Scripts\notExists' because it does not exist. At line:1 char:3 + gc <<<< notExists + CategoryInfo : ObjectNotFound: (Dropbox:\PowerShell\Scripts\notExists:String) [Get-Content], ItemNotFou ndException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand [213]: genum [214]:
Check your log files on the fly
Mar 23rd
Sometimes I need to check content of specific file. I usually use SMS Trace (part of SMS 2003 Toolkit). If I don’t have SMS Trace but there is PowerShell on the server, I used to use Get-Content cmdlet.
There is one very useful parameter: Wait. It will allow you to see content of the file on-the-fly. So as lines are coming, your console will show it.
Note: Wait is provider specific parameter so you don’t see it when running Get-Help Get-Content. To see it’s description you need to run Get-Help FileSystem.
Right console is updating file and changes are immediately visible in left console (Line 1 – Line 5 were in the file already so are returned immediately) and then console is updated every second with new line (Line 100 – Line 105).
Sometimes it’s not necessary (useful) to see all lines coming to the file. So then is possible to filter what you want to see (I wrote the code in ISE so it’s easier to read):
In the pipeline I am filtering with Where-Object (here represented by it’s alias ‘?’) so see only error lines. Of course, you can use more complex regex for that filtering.
