Windows Vista Forums

Combining enumeration values
  1. #1


    Robin Moffatt Guest

    Combining enumeration values

    I'm calling a method that takes as a parameter ScriptOptions
    enumeration (https://msdn2.microsoft.com/en-us/library/
    microsoft.sqlserver.replication.scriptoptions.aspx)

    I've worked out that I can do this:
    $srv.ReplicationServer.Script([Microsoft.SqlServer.Replication.scriptoptions]::Creation)

    but what's the syntax to combine multiple values of the enumeration?

    thanks, moff




      My System SpecsSystem Spec

  2. #2


    Kirk Munro Guest

    Re: Combining enumeration values

    Hi Robin,

    Use -bor between the enumeration values you want to combine. -bor is the
    operator for bitwise or.

    e.g.
    $srv.ReplicationServer.Script([Microsoft.SqlServer.Replication.scriptoptions]::Creation
    -bor [Microsoft.SqlServer.Replication.scriptoptions]::SomeOtherValue)

    -
    Kirk Munro
    Poshoholic
    http://poshoholic.com

    "Robin Moffatt" <robin.moffatt@xxxxxx> wrote in message
    news:1192023274.325358.224760@xxxxxx

    > I'm calling a method that takes as a parameter ScriptOptions
    > enumeration (https://msdn2.microsoft.com/en-us/library/
    > microsoft.sqlserver.replication.scriptoptions.aspx)
    >
    > I've worked out that I can do this:
    > $srv.ReplicationServer.Script([Microsoft.SqlServer.Replication.scriptoptions]::Creation)
    >
    > but what's the syntax to combine multiple values of the enumeration?
    >
    > thanks, moff
    >


      My System SpecsSystem Spec

  3. #3


    Shay Levi Guest

    Re: Combining enumeration values


    This example lists all enum values and constants.
    As for the syntax to combine values, check the FlagsAttribute option page
    on MSDN
    https://msdn2.microsoft.com/en-us/li...attribute.aspx


    [void][reflection.assembly]::LoadWithPartialName("microsoft.sqlserver.rmo")
    $e = [system.enum]::getvalues([Microsoft.SqlServer.Replication.scriptoptions])
    0..($e.length-1) | select @{n="Value";e={[string][enum]:arse([Microsoft.SqlServer.Replication.scriptoptions],$_)}},
    @{n="Constant";e={$_}} | format-table -autosize



    Value
    Constant
    ----
    --------
    None
    0
    Creation
    1
    Deletion
    2
    Creation, Deletion
    3
    IncludeArticles
    4
    Creation, IncludeArticles
    5
    Deletion, IncludeArticles
    6
    Creation, Deletion, IncludeArticles
    7
    IncludePublisherSideSubscriptions
    8
    Creation, IncludePublisherSideSubscriptions
    9
    Deletion, IncludePublisherSideSubscriptions
    10
    Creation, Deletion, IncludePublisherSideSubscriptions
    11
    IncludeArticles, IncludePublisherSideSubscriptions
    12
    Creation, IncludeArticles, IncludePublisherSideSubscriptions
    13
    Deletion, IncludeArticles, IncludePublisherSideSubscriptions
    14
    Creation, Deletion, IncludeArticles, IncludePublisherSideSubscriptions
    15
    IncludeSubscriberSideSubscriptions
    16
    Creation, IncludeSubscriberSideSubscriptions
    17
    Deletion, IncludeSubscriberSideSubscriptions
    18
    Creation, Deletion, IncludeSubscriberSideSubscriptions
    19
    IncludeArticles, IncludeSubscriberSideSubscriptions
    20
    Creation, IncludeArticles, IncludeSubscriberSideSubscriptions
    21
    Deletion, IncludeArticles, IncludeSubscriberSideSubscriptions
    22
    Creation, Deletion, IncludeArticles, IncludeSubscriberSideSubscriptions
    23
    IncludePublisherSideSubscriptions, IncludeSubscriberSideSubscriptions
    24
    Creation, IncludePublisherSideSubscriptions, IncludeSubscriberSideSubscriptions
    25
    Deletion, IncludePublisherSideSubscriptions, IncludeSubscriberSideSubscriptions
    26
    Creation, Deletion, IncludePublisherSideSubscriptions, IncludeSubscriberSideSubscriptions
    27
    IncludeArticles, IncludePublisherSideSubscriptions, IncludeSubscriberSideSubscriptions
    28
    Creation, IncludeArticles, IncludePublisherSideSubscriptions, IncludeSubscriberSideSubscriptions
    29
    Deletion, IncludeArticles, IncludePublisherSideSubscriptions, IncludeSubscriberSideSubscriptions
    30
    Creation, Deletion, IncludeArticles, IncludePublisherSideSubscriptions, IncludeSubscriberSideSubscriptions
    31
    IncludePartialSubscriptions
    32
    Creation, IncludePartialSubscriptions
    33



    Shay
    http://scriptolog.blogspot.com



    > I'm calling a method that takes as a parameter ScriptOptions
    > enumeration (https://msdn2.microsoft.com/en-us/library/
    > microsoft.sqlserver.replication.scriptoptions.aspx)
    >
    > I've worked out that I can do this:
    > $srv.ReplicationServer.Script([Microsoft.SqlServer.Replication.scripto
    > ptions]::Creation)
    > but what's the syntax to combine multiple values of the enumeration?
    >
    > thanks, moff
    >


      My System SpecsSystem Spec

  4. #4


    Roman Kuzmin Guest

    Re: Combining enumeration values

    I a parameter is not ambiguous in this particular case you can use a string
    of comma separated names of enum values:

    $srv.ReplicationServer.Script('Creation,Whatever1,Whatever2')


    --
    Thanks,
    Roman Kuzmin
    PowerShellFar and FarNET: http://code.google.com/p/farnet/


    "Robin Moffatt" <robin.moffatt@xxxxxx> wrote in message
    news:1192023274.325358.224760@xxxxxx

    > I'm calling a method that takes as a parameter ScriptOptions
    > enumeration (https://msdn2.microsoft.com/en-us/library/
    > microsoft.sqlserver.replication.scriptoptions.aspx)
    >
    > I've worked out that I can do this:
    > $srv.ReplicationServer.Script([Microsoft.SqlServer.Replication.scriptoptions]::Creation)
    >
    > but what's the syntax to combine multiple values of the enumeration?
    >
    > thanks, moff
    >


      My System SpecsSystem Spec

  5. #5


    Robin Moffatt Guest

    Re: Combining enumeration values


    thanks to everyone for their answers :-)


      My System SpecsSystem Spec

  6. #6


    Oisin Grehan Guest

    Re: Combining enumeration values

    On Oct 10, 10:13 am, "Kirk Munro" <so...@xxxxxx> wrote:

    > Hi Robin,
    >
    > Use -bor between the enumeration values you want to combine. -bor is the
    > operator for bitwise or.
    >
    > e.g.
    > $srv.ReplicationServer.Script([Microsoft.SqlServer.Replication.scriptoption*s]::Creation
    > -bor [Microsoft.SqlServer.Replication.scriptoptions]::SomeOtherValue)
    >
    > -
    > Kirk Munro
    > Poshoholichttp://poshoholic.com
    >
    You _can_ do it this way if you want to get early RSI ;-), but
    alternatively you can use powershell's magic-casty bits (tm) to do the
    hard work for you. Just pass the plain names, comma separated inside a
    string, e.g. the above would be passed as:

    $srv.ReplicationServer.Script("Creation,SomeOtherValue")

    Hope this helps,

    - Oisin / x0n


      My System SpecsSystem Spec

  7. #7


    Robin Moffatt Guest

    Re: Combining enumeration values

    On Oct 10, 5:39 pm, Oisin Grehan <ois...@xxxxxx> wrote:

    > You _can_ do it this way if you want to get early RSI ;-), but
    > alternatively you can use powershell's magic-casty bits (tm) to do the
    > hard work for you. Just pass the plain names, comma separated inside a
    > string, e.g. the above would be passed as:
    >
    > $srv.ReplicationServer.Script("Creation,SomeOtherValue")
    >
    Out of interest, can this syntax be used for xor'ing values together
    too?
    At the moment my statement looks like this:
    $pub_svr.ReplicationServer.Script(([Microsoft.SqlServer.Replication.scriptoptions]::Creation
    `
    -bor
    [Microsoft.SqlServer.Replication.scriptoptions]::IncludeAll `
    -bxor
    [Microsoft.SqlServer.Replication.scriptoptions]::IncludeReplicationJobs ))


    cheers, moff.


      My System SpecsSystem Spec

  8. #8


    Oisin Grehan Guest

    Re: Combining enumeration values

    On Oct 11, 5:23 am, Robin Moffatt <robin.moff...@xxxxxx> wrote:

    > On Oct 10, 5:39 pm, Oisin Grehan <ois...@xxxxxx> wrote:
    >

    > > You _can_ do it this way if you want to get early RSI ;-), but
    > > alternatively you can use powershell's magic-casty bits (tm) to do the
    > > hard work for you. Just pass the plain names, comma separated inside a
    > > string, e.g. the above would be passed as:
    >

    > > $srv.ReplicationServer.Script("Creation,SomeOtherValue")
    >
    > Out of interest, can this syntax be used for xor'ing values together
    > too?
    > At the moment my statement looks like this:
    > $pub_svr.ReplicationServer.Script(([Microsoft.SqlServer.Replication.scripto*ptions]::Creation
    > `
    > -bor
    > [Microsoft.SqlServer.Replication.scriptoptions]::IncludeAll `
    > -bxor
    > [Microsoft.SqlServer.Replication.scriptoptions]::IncludeReplicationJobs ))
    >
    > cheers, moff.
    Sure, one way has you casting the operands, so you don't save as much
    typing this time. e.g.

    PS > $targets = ([attributetargets]"all" -bxor
    [attributetargets]"event,field")

    But if the type name is long, another handy trick is to assign the
    enum type to a variable, e.g.

    PS > $enum = [Microsoft.SqlServer.Replication.ScriptO*ptions]
    PS > $options = ($enum::creation -bor $enum::IncludeAll) -bxor
    $enum::includereplicationjobs

    ....and to answer your next question, if you want to cast multiple
    flags using a variable shortcut, use the -as operator:

    PS > $options = $enum::all -bxor ("includeall, includereplicationjobs"
    -as $enum)

    (because [$enum]"creation,includeall" won't work)

    Hope this helps,

    - Oisin / x0n

    http://www.nivot.org/







      My System SpecsSystem Spec

  9. #9


    Robin Moffatt Guest

    Re: Combining enumeration values

    [...]

    >
    > PS > $options = $enum::all -bxor ("includeall, includereplicationjobs"
    > -as $enum)
    >
    > (because [$enum]"creation,includeall" won't work)
    >
    > Hope this helps,
    >
    > - Oisin / x0n


    Thanks


      My System SpecsSystem Spec

  10. #10


    Robin Moffatt Guest

    Re: Combining enumeration values

    As a follow-on to this, how can I test an enumeration for a specific
    value?
    I'm doing this:
    $dist_svr.ReplicationServer.DistributionPublishers[0].DistributionPublications[5].attributes
    and attributes has the type
    Microsoft.SqlServer.Replication.PublicationAttributes
    So how can I test if an object has a specific bit set, eg.
    [Microsoft.SqlServer.Replication.publicationattributes]::AllowAnonymous

    TIA, moff


    On Oct 10, 3:13 pm, "Kirk Munro" <so...@xxxxxx> wrote:

    > Hi Robin,
    >
    > Use -bor between theenumerationvalues you want to combine. -bor is the
    > operator for bitwise or.
    >
    > e.g.
    > $srv.ReplicationServer.Script([Microsoft.SqlServer.Replication.scriptoptions]::Creation
    > -bor [Microsoft.SqlServer.Replication.scriptoptions]::SomeOtherValue)
    >
    > -
    > Kirk Munro
    > Poshoholichttp://poshoholic.com
    >
    > "Robin Moffatt" <robin.moff...@xxxxxx> wrote in message
    >
    > news:1192023274.325358.224760@xxxxxx
    >

    > > I'm calling a method that takes as a parameter ScriptOptions
    > >enumeration(https://msdn2.microsoft.com/en-us/library/
    > > microsoft.sqlserver.replication.scriptoptions.aspx)
    >

    > > I've worked out that I can do this:
    > > $srv.ReplicationServer.Script([Microsoft.SqlServer.Replication.scriptoptions]::Creation)
    >

    > > but what's the syntax to combine multiple values of theenumeration?
    >

    > > thanks, moff


      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
Combining enumeration values problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Referencing enumeration Shay Levy [MVP] PowerShell 0 07 Feb 2010
Referencing enumeration wfr PowerShell 1 07 Feb 2010
mirroringrole enumeration - script Frank PowerShell 3 19 Nov 2007
How do you use .net enumeration values in powershell? Bob Landau PowerShell 3 20 Sep 2007
Networok Enumeration Majhoos Vista networking & sharing 0 13 Feb 2007