Powershell and using the Wildcard Pattern Escape Method

I was recently parsing strings which contained a wildcard pattern in the string name, by using the ‘WildCardPattern.Escape’ method I was able to convert the string which contained the escaped characters to a string that contains the escape codes and then parse the output as expected.

In the example, I was attempting to retrieve the datastore name of objects returned from the ‘Extensiondata.Layoutex.File’ property using PowerCLI. Where an example of the string is as below:

[DATASTORE 1] vm1/vm1.vmx

I was able to pass the property value to the ‘WildcardPattern.Escape’ method to convert the string containing the escape codes.

$Wildcard = [Management.Automation.WildcardPattern]::Escape($File.Name)

I was then able to parse the output to retrieve the datastore name from the Name property of the file.

$Datastore = $Wilcard.Split("`[")[1]).Split("``]")[0]

After invoking the ‘WildcardPattern.Escape’ method and parsing the output, we would receive a value as below:

DATASTORE 1

For more information on the ‘WildcardPattern.Escape’ method see http://msdn.microsoft.com/en-us/library/system.management.automation.wildcardpattern.escape(v=vs.85).aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s