Roger Lipscombe
Roger Lipscombe

Reputation: 91965

Referencing a property in when using registry properties in MSBuild?

I'm attempting to use MSBuild to figure out whether a SQL server instance has SQL authentication enabled. I'm trying the following:

<Target Name="VerifySQLLoginMode">
  <PropertyGroup>
    <SqlInstanceName>SQL08X64</SqlInstanceName>
    <SqlInstanceKey>$(registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL@$(SqlInstanceName))</SqlInstanceKey>
    <SqlLoginMode>$(registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\$(SqlInstanceKey)\MSSQLServer@LoginMode)</SqlLoginMode>
  </PropertyGroup>

  <Message Text="SqlInstanceName = $(SqlInstanceName)" />
  <Message Text="SqlInstanceKey = $(SqlInstanceKey)" />
  <Message Text="SqlLoginMode = $(SqlLoginMode)" />

  <Error Condition="'$(SqlLoginMode)' != '2'" Text="Error: SQL Authentication is disabled. Please enable it." />
</Target>

Unfortunately, MSBuild doesn't seem to allow referencing properties ($(SqlInstanceName)) inside $(registry:...) properties.

Or is there some way to make this work?

Upvotes: 4

Views: 560

Answers (1)

Roger Lipscombe
Roger Lipscombe

Reputation: 91965

Actually, it's probably down to using the 32-bit MSBuild. Using the MSBuild 4.0 property functions gives me this:

<Target Name="VerifySQLLoginMode">
  <!-- Note that this can't deal with the default instance. -->

  <PropertyGroup>
    <SqlInstanceName>SQL08X64</SqlInstanceName>
    <SqlInstanceKey>$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL', '$(SqlInstanceName)', null, RegistryView.Registry64, RegistryView.Registry32))</SqlInstanceKey>
    <SqlLoginMode>$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\$(SqlInstanceKey)\MSSQLServer', 'LoginMode', null, RegistryView.Registry64, RegistryView.Registry32))</SqlLoginMode>
  </PropertyGroup>

  <Message Text="SqlInstanceName: $(SqlInstanceName)" />
  <Message Text="SqlInstanceKey: $(SqlInstanceKey)" />
  <Message Text="SqlLoginMode: $(SqlLoginMode)" />

  <Error Condition="'$(SqlLoginMode)' != '2'" Text="Error: SQL Authentication is disabled. Please enable it." />
</Target>

...which works fine.

Upvotes: 6

Related Questions