Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Thursday, December 8, 2016

Dealing with Property Names that Start with #

Tonight I’m doing my class maintenance on Microsoft Official Course 20697-2B Deploying and Managing Windows 10 Using Enterprise Services. For those of you who have taken my classes, you know I provide a OneDrive full of goodies to each class.  Right now I’m playing around with Chapter 3 and the migration of user data.  While working with size estimates for the migration store, I decided to import the XML file the ScanState.exe /p produces.

PS USMT:> $StorageInfo.Premigration.storeSize.Size | Get-Member –MemberType Properties

   TypeName: System.Xml.XmlElement

Name        MemberType Definition
----        ---------- ----------
#text       Property   string #text {get;set;}
clusterSize Property   string clusterSize {get;set;}

So, do you see the problem?  Take a look at the property called #text.  In PowerShell, the # symbol is the start of an inline comment.  That means that anything typed after it will not be processed.  That kind of makes calling this property a bit of a problem.  We can fix it.

If you do not code this correctly, PowerShell will interpret the # and everything after it as a comment.  The green font in the code demonstrates this.

$StorageInfo.PreMigration.StoreSize.Size |
    Select-Object -Property CluserSize,
        @{
           N = "SizeMB"
           E = {($_.#Text / 1MB).ToString('#.##') -as [Single]}
        }

To fix this problem, encapsulate our problematic property name inside of double quotes.

$StorageInfo.PreMigration.StoreSize.Size |
    Select-Object -Property CluserSize,
        @{
           N = "SizeMB"
           E = {($_."#Text" / 1MB).ToString('#.##') -as [Single]}
        }

Now you will be able to call the property and use it.



No comments: