RSS

PowerShell – Auto generate PowerShell Functions from .NET Code using ISESteroids

PowerShell – Auto generate PowerShell Functions from .NET Code using ISESteroids

In my previous blog posts I described how to determine possible values for a property / attribute using .NET code and how to create a function to simplify this task in the future.

Shortly after, I saw a post on Twitter showing of functionality of ISESteroids to auto generate functions from .NET code. Since I hate to do unnecessary work and I know others think this way as well  wanted to share this. Even though in my case the time savings is not that big and the end result is not exactly what I was looking for, it saves me time and gives me is a very useful basis to build on.

For more information and a better example and explanation, see: http://www.powertheshell.com/net2powershell/.
You should also take a look at all the other great stuff that ISEsteroids can do to save you time and effort.

 
Leave a comment

Posted by on March 29, 2016 in ICT, Microsoft, Powershell

 

PowerShell – WMF5 (including PowerShell) 5 can be deployed using WSUS again, but there is a catch …

A couple of weeks ago I was thinking that I should blog that it’s a pity that Windows Management Framework (including PowerShell) could be not distributed through WSUS anymore. In the past it was available on through WSUS, but it was removed (expired) at some point due to some issues.

This meant to deploy PowerShell you could not deploy it through regular WSUS, but you had to either:

  • Include it in your base image
  • Install it manually
  • Install it using scripting
  • Install it using GPO
  • Install it using WSUS add-on solutions to deploy 3rd party packages. Example solutions include, but are not limited to Local Update Publisher (LUP), WSUS package publisher, SolarWinds patch manager.
  • Install it using enterprise systems management software. Example solutions include, but are not limited to System Center Configuration Manager (SCCM), Altiris, Landesk Management Suite, Tivoli Endpoint Manager (BigFix)
  • Install it using Intune

At many customers of ours this meant that PowerShell was left at version 2.0 for older operating systems unfortunately. For newer operating systems luckily version 3.0 was shipped by default. Still the version would never be updated in most cases.

Apparently the PowerShell team also thought something had to be done about that, because they made the Windows Management Framework (WMF) 5.0 RTM available via the Microsoft Update Catalog. Since it is published to the Microsoft Update Catalog, you have to manually import it to your WSUS environment. Also as the blog post states, before installing ensure you have reviewed known product incompatibilities (Exchange, SharePoint and System Center Virtual Machine Manager) and that the prerequisites are met.

I hope this will mean I will be seeing more up-to-date versions of PowerShell on systems of customers from now on.

One of the disadvantages however is that for operating systems before Windows 8.1/2012, a prerequisite is that WMF4 is installed, which cannot easily be deployed using WSUS unfortunately.

 

 

 

 

 

 

 

 
 

Tags: , , , ,

SCVMM 2012 R2 – Fix for VM console access error : Virtual Machine Manager lost the connection to the virtual machine

In the last couple of weeks I’ve been installing and configuring a System Center Virtual Machine Manager 2012 R2 (VMM) environment.

At first all management was done from the VMM management server and console access to VMs from VMM was working fine. Later on the VMM console (and appropriate rollup) were installed on the laptop directly. Then when he opened the VMM console as a regular user and entered the administrator credentials on the VMM console. All was fine until he tried to access the console of VMs from within VMM, because then for most (not all VMs) he received the following error:

Virtual Machine Manager lost the connection to the virtual machine for one of the following reasons.

Another connection was established to the console of this machine
The virtual machine has been shut down or put into the saved state
The user credentials provided do not have the necessary privilege to connect

(0x0003, 0x0000)

Other connections were not established to the console of this machine and the virtual machine had also not been shut down or put into the saved state. The user credentials provided when connecting to VMM were of an administrator account with VMM admin permissions.

Since the administrator logs on to his laptop using a non-admin account, we first tried to run the VMM console “As Administrator” but this didn’t resolve the issue.

Then I googled the error and found this knowledge base article : VMM 2012 Self-Service users cannot open a console session to a virtual machine . The description didn’t really match our symptoms since the account specified to connect to VMM was an administrator account instead of a self-service user and it was possible to open a VM console session when the VMM console was opened from the VMM Management Server. To rule it out, we tested the proposed resolution and verified using Get-VMConnectAccess that everything was configured correctly.

The solution was to SHIFT+Rightclick on the VMM console shortcut and then choose “Run as different user” and enter the credentials of the administrator account. After that everything worked fine.

 

 

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

PowerShell – easily posting scripts to a website while maintaining syntax highlighting by copying as HTML

PowerShell – easily posting scripts to a website while maintaining syntax highlighting by copying as HTML

 

 

 

When sharing PowerShell scripts on the Internet there are a couple of options:

  1. Simply post it as plain text, which means syntax highlighting will not be included.
  2. Use available syntax highlighting for the platform you use. Sample platforms include:
    Microsoft ScriptCenter and a syntax highlighting example from a script of mine: https://gallery.technet.microsoft.com/scriptcenter/Determine-possible-values-eaf48782
    Wordpress : https://en.support.wordpress.com/code/posting-source-code/
  3. Copy code to HTML including syntax highlighting.

Option 2 and 3 are preferred, especially sharing to Microsoft ScriptCenter because others can then more easily find and use your scripts and maybe even improve them or give you tips on how to make better script.

Option 2 however requires that you have to accept whatever the platform provider has chosen with regards to syntax highlighting, which might differ between platforms. With option 3 you have more freedom with regards to the output and you are in control. Basically it means copying code to HTML including syntax highlighting. The solution I use mostly for this is the Windows PowerShell V3 ISE: Copy As HTML Add-On which adds a simple “Copy As HTML” option in the menu and which can also be used using the shortcut CTRL+SHIFT+C.

I don’t have experience with it, but you could take a look at the script that Lee Holmes from the PowerShell team provided: http://www.leeholmes.com/blog/2009/02/03/more-powershell-syntax-highlighting/
In the comments someone apparently also implemented it as a PowerGUI add-on.

For more PowerShell ISE Add-ons be sure to visit: http://social.technet.microsoft.com/wiki/contents/articles/2969.windows-powershell-ise-add-on-tools.aspx

 
Leave a comment

Posted by on March 14, 2016 in ICT, Microsoft, Powershell

 

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

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: , , , , , , ,