How to use splatting

Sara 441 Reputation points
2024-09-20T07:27:13.79+00:00

I am looking for some help with splatting , I have the below lines to grab a registry key and want it to be passed into a hash table to make it more simpler to view to reduce the length

    if ($Version -ge 16)

    {

       # Grab the value of SAID from the registry based on the instance

       $getTargetResourceReturnValue.SAid = Get-RegistryPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL$($SqlVersion).$($InstanceName)\Setup" -Name "SAID"  
```}

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,549 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,581 questions
{count} votes

Accepted answer
  1. Michael Taylor 54,806 Reputation points
    2024-09-20T15:40:15.9066667+00:00

    Your hashtable declaration is incorrect. When you are manually creating a hashtable put each value on separate lines.

    $getRegistryPropertyParams = @{ 
    Path = "HKLM:\SOFTWARE\Microsoft\Microsoft SQLServer\MSSQL$($SqlVersion).$($InstanceName)\Setup"
    Name = 'SAID' 
    }
    

    If you want to put everything on a single line then PS considers it to be one statement. But you need to generate it as multiple statements so use the semicolon to separate the statements that PS should look at.

    $getRegistryPropertyParams = @{ Path = "HKLM:\SOFTWARE\Microsoft\Microsoft SQLServer\MSSQL$($SqlVersion).$($InstanceName)\Setup"; Name = 'SAID' }
    

    Now PS sees the first statement, up to the semicolon as the first line and then everything after that as the second. This is no different than if you manually typed in the first line and pressed ENTER and then entered the second line.

    This is how PS works in general so this is not specific to hashtables. Use a semicolon to "insert" a newline in the middle of commands.


1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 46,806 Reputation points
    2024-09-20T19:24:36.8733333+00:00

    "Splatting" is a way to pass parameters to a cmdlet or advanced function. The "splat" is a hashtable (so-named after the "@" character (a.k.a. "splat"))

    In the hashtable, each key represents the name of a parameter, and each value is the argument of that parameter.

    When you use the hashtable with the function or cmdlet name, be sure to use the "@" decorator and not the usual "$".

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-5.1

    Here's an example of using your function with splatting:

    $props - @{
                  Path = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL$($SqlVersion).$($InstanceName)\Setup"
                  Name = "SAID"
              }
    $getTargetResourceReturnValue.SAid = Get-RegistryPropertyValue @props
      
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.