RSS

PowerShell – Get-EnumValue function to get possible values of properties / attributes

PowerShell – Get-EnumValue function to get possible values of properties / attributes

In my previous blog post I blogged about how to get the possible values of an attribute or property like by example the service starttype using enumerators.

For future use I’ve created this function called Get-EnumValue. The function (and any updated future versions) can be found on ScriptCenter: https://gallery.technet.microsoft.com/scriptcenter/Determine-possible-values-eaf48782

But for those that don’t want to click and just want to see the current version, here it is:

Function Get-EnumValue

{

    <#

    .SYNOPSIS

        Determine possible values of a property / attribute of the [system.enum] type

        By example the possible values of startup type of a service.

    .DESCRIPTION

        Determine possible values of a property / attribute of the [system.enum] type

        By example the possible values of startup type of a service

 

        In some cases this can easily be determined if there is a “set-” version of the cmdlet.

        By example with Get-Service, there is also a Set-Service where the StartUpType values can be determined using “Get-Help Set-Service -Full” or by simply typing: Set-Service -StartupType and using auto completion.

 

        In some cases this is however not possible and this function can be used.

    .PARAMETER EnumProperty

        Input a property of the [System.enum] type. By example: (Get-Date)[0].DayOfWeek or (Get-Service)[0].StartType’)    

    .EXAMPLE

        $FormatEnumerationLimit=-1

        $Property1 = (Get-Date)[0].DayOfWeek

        $Property2 = (Get-Service)[0].StartType

        Get-EnumValue -EnumProperty $Property1,$Property2

            

        Description

    

        ———–

    

        Set $FormatEnumerationLimit to -1 to prevent cutoff of the results.

        Store 2 properties in variables and use the Get-EnumValue function with the named parameter -EnumProperty to enumerate their values.

    .EXAMPLE

        $FormatEnumerationLimit=-1

        Get-EnumValue -EnumProperty (Get-Date)[0].DayOfWeek,(Get-Service)[0].StartType

            

        Description

    

        ———–

    

        Set $FormatEnumerationLimit to -1 to prevent cutoff of the results.

        Use the Get-EnumValue function with the named parameter -EnumProperty to enumerate their values without first storing them in variables.

    .EXAMPLE

        $FormatEnumerationLimit=-1

        $Properties = @((Get-Date)[0].DayOfWeek,(Get-Service)[0].StartType)

        Get-EnumValue -EnumProperty $Properties

            

        Description

    

        ———–

    

        Set $FormatEnumerationLimit to -1 to prevent cutoff of the results.

        Store 2 properties in a single array and use the Get-EnumValue function with the named parameter -EnumProperty to enumerate the values of the properties in the array.

    .EXAMPLE

        $FormatEnumerationLimit=-1

        (Get-Date)[0].DayOfWeek,(Get-Service)[0].StartType | Get-EnumValue | Format-Table    

    

        Description

    

        ———–

    

        Set $FormatEnumerationLimit to -1 to prevent cutoff of the results.

        Put 2 properties in the pipeline and pipe them to Get-EnumValue to get their values.

    .NOTES

        1) By default, the EnumValues are truncated like this for format-list, format-table, etc:

 

        For Format-List:

 

        TypeName   : System.DayOfWeek

        EnumValues : {Sunday, Monday, Tuesday, Wednesday…}

 

        TypeName   : System.ServiceProcess.ServiceStartMode

        EnumValues : {Boot, System, Automatic, Manual…}

 

        For Format-Table:

 

        TypeName                               EnumValues                            

        ——–                               ———-                            

        System.DayOfWeek                       {Sunday, Monday, Tuesday, Wednesday…}

        System.ServiceProcess.ServiceStartMode {Boot, System, Automatic, Manual…}

 

        By setting $FormatEnumerationLimit to -1 all values will be shown (https://blogs.technet.microsoft.com/heyscriptingguy/2011/11/20/change-a-powershell-preference-variable-to-reveal-hidden-data/):

        $FormatEnumerationLimit=-1

 

        2) This example uses [Enum]::GetValues but it can easily be modified to use [Enum]::GetNames

 

        3) Additional information and resources:

        http://social.technet.microsoft.com/wiki/contents/articles/26436.how-to-create-and-use-enums-in-powershell.aspx#UsingEnumsWiithFunction

        https://msdn.microsoft.com/en-us/library/system.enum.getnames(v=vs.110).aspx

        https://msdn.microsoft.com/en-us/library/system.enum.getvalues(v=vs.110).aspx

#>

    [CmdletBinding()] #Provides advanced functionality. For more details see “What does PowerShell’s [CmdletBinding()] Do?” : http://www.windowsitpro.com/blog/powershell-with-a-purpose-blog-36/windows-powershell/powershells-%5Bcmdletbinding%5D-142114

    Param

    (

        [Parameter(Mandatory=$true, #Parameter is mandatory.

                   ValueFromPipeline=$True, #Allows pipeline input.

                   Position=0, #Allows function to be called without explicitly specifying parameters, but instead using positional parameters in the correct order

                   HelpMessage=‘Input an object of the [System.enum] type. By example: (Get-Date)[0].DayOfWeek or (Get-Service)[0].StartType’)] #Enter a help message to be shown when no parameter value is provided.

        [ValidateNotNullOrEmpty()] #Validate the input is not NULL or empty

        [ValidateScript({$_ -is [System.Enum]})] #Validate whether or not the input is actually of the [System.enum] type.

        [System.enum[]]$EnumProperty

    )

    BEGIN

    {

    }

    PROCESS

    {

        $Output = @()

        Foreach($object in $EnumProperty)

        {

            TRY

            {

                $TypeName = ($object | Get-Member)[0].TypeName

                $EnumValues = [Enum]::GetValues($TypeName) #Pre-PowerShell 3.0

                $ObjectEnumResult = New-Object PSCustomObject -Property @{

                ‘TypeName’ = $TypeName

                ‘EnumValues’ = $EnumValues

                }

                $Output += $ObjectEnumResult

            }

            CATCH

            {

                Write-Verbose “Error occurred processing $object

            }

            FINALLY

            {

            }

        }

 

        #Send output to the pipeline

        $Output

    }

    END

    {

    }

}

 

 
1 Comment

Posted by on March 11, 2016 in Automation, ICT, Microsoft, Powershell

 

Tags: , , , , , , , , , , , , , ,

PowerShell – Determine possible values for an attribute

While working on a script for XenApp 6.5 I wanted to perform actions based on the session state. To me however, the possible state values were not clear.

Without knowing these states, I would have to make assumptions which could lead to unexpected behavior and results.

Generic example

Since not everyone is using Citrix, here’s a more generic sample using Get-EventLog to determine the possible values for attribute EntryType

1. Get-EventLog -LogName System | Select EntryType -First 1 | Get-Member

This resulted in:

   TypeName: Selected.System.Diagnostics.EventLogEntry

Name        MemberType   Definition
—-        ———-   ———-
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
EntryType   NoteProperty System.Diagnostics.EventLogEntryType EntryType=Information

2.  Enumerate the values using:
[Enum]::GetValues(‘System.Diagnostics.EventLogEntryType‘)

This results in the following possible values:

Error
Warning
Information
SuccessAudit
FailureAudit

XenApp 6.5 example

To determine the possible states, I did the following:

1. Get-XASession | Select State | Get-Member

This resulted in:

TypeName: Selected.Citrix.XenApp.Commands.XASession

        Name        MemberType   Definition
—-        ———-   ———-
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
State       NoteProperty Citrix.XenApp.Commands.SessionConnectionState State=Disconnected

2.  Enumerate the values using:
[Enum]::GetValues(‘Citrix.XenApp.Commands.SessionConnectionState‘)

This results in the following possible values:

Unknown
Active
Connected
Connecting
Shadowing
Disconnected
Idle
Listening
Resetting
Down
Initializing
Stale
Licensed
Unlicensed
Reconnected

Alternative method for PowerShell 3.0 / .NET Framework 4.0 and later

After writing this blog post I came across this post http://www.powershellmagazine.com/2013/03/15/pstip-getting-enum-values-in-powershell-3-0/ explaining an alternative method that is available for PowerShell 3.0 / .NET Framework 4.0 and later :

[System.Diagnostics.EventLogEntryType ].GetEnumValues()

[System.Diagnostics.EventLogEntryType].GetEnumNames()

 

 

 
2 Comments

Posted by on March 9, 2016 in Citrix, Microsoft, Powershell, XenApp

 

Tags: , , , , ,

Beschikbaar voor opdrachten / Available for job opportunities

Nederlands (Dutch):

Momenteel ben ik beschikbaar voor opdrachten via mijn werkgever Open Line. Mijn expertise is vooral op het gebied van Microsoft, PowerShell, virtualisatie en cloud computing. Voor meer informatie over mij, mijn kennis en ervaring zie: https://nl.linkedin.com/in/bjornhouben

Beschikbaarheid: Per direct, zowel fulltime als parttime.
Regio: Bij voorkeur (Zuid) Limburg, maar andere regio’s zijn ook bespreekbaar.

Indien u een opdracht voor mij heeft of een opdracht voor mij weet, stuur dan aub een e-mail naar: bjorn.houben@openline.nl

Het delen van dit bericht wordt ook enorm gewaardeerd.

Bij voorbaat dank .
Groeten Bjorn


 

English:

Currently I’m available for job opportunities through my employer Open Line. My main areas of expertise are Microsoft, PowerShell, virtualization and cloud computing. For more information about me, my knowledge and experience see: https://nl.linkedin.com/in/bjornhouben

Availability: As of today, either full time or part time.
Region: Preferably (Zuid) Limburg, but other regions are also negotiable.

If you have a job opportunity or know of a job opportunity, please send me an e-mail at: bjorn.houben@openline.nl

Sharing this post is also greatly appreciated.

Thanks in advance.

Best regards,
Bjorn

 

 

 

 
Leave a comment

Posted by on January 25, 2016 in Open Line

 

Microsoft Ignite 2015 : huge collection of information enhanced with more detailed resources to get you up-to-date with less time and effort

As some of you might know already, the Microsoft Ignite 2015 conference has started where Microsoft talks about where they’re going and what they’re doing. I watched the keynote, took many notes and added other relevant information which I’m sharing with you. Since so much was told and shown it’s certain I’ve missed stuff. As time goes by I will update the information in this post so it can be used as a reference.

I hope it’s useful. If you find any errors or have other useful resources or feedback, please leave a comment.

Update log:

-05-05-2015: Created Initial version including the keynote and additional information.

 

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

VMware VCP Recertification deadline extended until May 8 , those who recertified before the extension get 65% off VCP6 migration exam

Recently I had to get recertified for VCP and even though I didn’t really want to …. I did so anyways as you can read in my previous blog post: VMware VCP5-DCV Delta Recertification Exam (VCP550D) available until March 10th + resources to prepare for it

Apparently there were some technical issues and VMware decided to extend the VCP recertification deadline until May 8, 2015. VMware understood this might frustrate people  who worked hard to meet the original deadline on time (before March 10th 2015), so they decided to give those people 65% off the VCP6 migration exam.

Even though I still don’t really agree with the recertification, this is a step in the good direction VMware. Keep it up.

For more information, you can read the original blog post: Short Extension For VCP Recertification Deadline (and a Reward for Those Who Met the Original Deadline)

 
Leave a comment

Posted by on March 11, 2015 in ICT, Learning, VMware

 

Tags: , , , , , , ,

VMware VCP5-DCV Delta Recertification Exam (VCP550D) available until March 10th + resources to prepare for it

Again I want to stress that I don’t agree with the way VMware is enforcing the recertification in this case as I’ve described in more detail here. Nevertheless I had to take the exam to keep my certification status, contribute to my company’s partner status and to prevent having to take another expensive course in the future to get certified. I have to admit I did learn about a couple of new things though.

But enough about that. Until March 10th it is possible to take the VCP5-DCV Delta Recertification Exam (VCP550D) that is based on new material between the vSphere 5.0/5.1 and vSphere 5.5 exams. Another benefit is that it is cheaper (90 euros) and that it can be taken online from any location.

I took the exam and passed, so I’m good for the near future. I am thinking about postponing vSphere 6.0 certification due to this re-certification policy though.

Here are the resources I used to prepare for the exam:

I know Veeam also provides VCAP5-DCA and VCP5-DCV study guides, but I haven’t used these yet.

I hope it’s useful to you. If you have any other useful resources or feedback, please leave a comment

 
1 Comment

Posted by on March 4, 2015 in ICT, Learning, VMware

 

Tags: , , , , , , ,

TreeSize Free, an already great tool got even beter

One of my favorite tools for years has been TreeSize Free. It is a small, easy, fast and portable tool that can be used to determine disk space usage of folders and files on a volume.

In the past it had some limitations that hindered me, but with the latest updates they’ve added great features that I really love:

  • TreeSize Free now scans network drives (except Windows servers and network drives within a Windows domain).
  • TreeSize Free provides a touch-optimized user interface as an option for Windows 8.0 and higher.
  • Users can now search for folders in the directory tree.
  • TreeSize Free supports selection of multiple files now, e.g. to delete or move them.
  • The scan performance on local NTFS drives (especially on SSD drives) has been increased.

So be sure to check out TreeSize Free if you hadn’t already or if you’re on an older version, download the latest version.

For those that run into the limitations regarding network drives with TreeSize Free, take a look at the commercial versions of TreeSize or other free disk space analyzer programs like Space Sniffer, WinDirStat, JDiskReport, Disk Analyzer Pro, Daisy Disk, Space Gremlin, etc.

 
Leave a comment

Posted by on February 25, 2015 in ICT, Tools

 

Tags: , , , , , , , , , ,

 
Follow

Get every new post delivered to your Inbox.

Join 1,654 other followers

%d bloggers like this: