Enumerations in PoSh prior to V5

  • I'm trying to create an enum of our SQL Servers

    Add-Type -TypeDefinition @"
     public enum OurServers3
         {
         Srv1,
         Srv1\Instance1
         }
    "@

    The backslash in the  instance name is giving me headaches! Any ideas? I've tried escaping with ` and \, surrounding with double-quotes...

    Any ideas?

  • schleep - Friday, September 29, 2017 11:58 AM

    I'm trying to create an enum of our SQL Servers

    Add-Type -TypeDefinition @"
     public enum OurServers3
         {
         Srv1,
         Srv1\Instance1
         }
    "@

    The backslash in the  instance name is giving me headaches! Any ideas? I've tried escaping with ` and \, surrounding with double-quotes...

    Any ideas?

    You could also try using -
    Encode-SqlName -SQLName "Srv1\Instance1"

    Sue

  • Oddly, my editor recognizes that alias for ConvertTo-EncodedSQLName, but I get cmdlet or function not found, when I try to use it outside of the here-string.
    Still getting the same errors when i put that inside the here-string.

  • schleep - Monday, October 2, 2017 5:52 AM

    Oddly, my editor recognizes that alias for ConvertTo-EncodedSQLName, but I get cmdlet or function not found, when I try to use it outside of the here-string.
    Still getting the same errors when i put that inside the here-string.

    That is weird - not sure when that cmdlet came out as I'm kind of sick of trying to track Powershell versions all the time.
    I guess the error makes sense as that would be for Powershell and your creating a .Net type. So could be two different ways to escape it. You could try to hard code in the Powershell encoding - replace the backslash with  %5C  but I am guessing you already did.
    I  thought the escaping in .net would be the backslash you already tried so I'm out of ideas.

    Sue

  • Sue_H - Tuesday, October 3, 2017 3:27 PM

    "...I'm kind of sick of trying to track Powershell versions all the time."

    +10

    ---------
    I've given up on this: I can validate server names other ways.

    Thanks for taking the time, Sue.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply