Invoke-HKCURegistrySettingsForAllUsers question

//Invoke-HKCURegistrySettingsForAllUsers question
Invoke-HKCURegistrySettingsForAllUsers question 2016-10-08T10:17:16+00:00

The Toolkit Forums General Invoke-HKCURegistrySettingsForAllUsers question

  • Author
    Posts
  • Francois
    Participant
    Post count: 101
    #1675 |

    Hi,

    I would like to insert variables and replacing one before calling Invoke-HKCURegistrySettingsForAllUsers

    <code>[scriptblock]$HKCURegistrySettings = {
    		Set-RegistryKey -Key 'HKCU\Software\Microsoft\Office\14.0\Common' -Name 'qmenable' -Value 0 -Type DWord -SID $UserProfile.SID
    		Set-RegistryKey -Key 'HKCU\Software\Microsoft\Office\14.0\Common' -Name 'updatereliabilitydata' -Value 1 -Type DWord -SID $UserProfile.SID
            Set-RegistryKey -Key &amp;amp;amp;amp;#039;HKCU\Software\Microsoft\Windows\Current\version\RunOnce&amp;amp;amp;amp;#039; -Name &amp;amp;amp;amp;#039;<strong>$Aname</strong>&amp;amp;amp;amp;#039; -Value 1 -Type DWord -SID $UserProfile.SID
    	}&amp;amp;amp;lt;/code&amp;amp;amp;gt;

    So wih $HKCURegistrySettings, I will get
    Set-RegistryKey -Key ‘HKCU\Software\Microsoft\Office\14.0\Common’ -Name ‘qmenable’ -Value 0 -Type DWord -SID $UserProfile.SID
    Set-RegistryKey -Key ‘HKCU\Software\Microsoft\Office\14.0\Common’ -Name ‘updatereliabilitydata’ -Value 1 -Type DWord -SID $UserProfile.SID
    Set-RegistryKey -Key ‘HKCU\Software\Microsoft\Windows\Current\version\RunOnce’ -Name ‘$Aname’ -Value 1 -Type DWord -SID $UserProfile.SID

    This is correct.

    But if I make
    $Aname=”packageone”

    Then
    $HKCURegistrySettings

    Will equal (and this is wrong for my needs):

    Set-RegistryKey -Key ‘HKCU\Software\Microsoft\Office\14.0\Common’ -Name ‘qmenable’ -Value 0 -Type DWord -SID $UserProfile.SID
    Set-RegistryKey -Key ‘HKCU\Software\Microsoft\Office\14.0\Common’ -Name ‘updatereliabilitydata’ -Value 1 -Type DWord -SID $UserProfile.SID
    Set-RegistryKey -Key ‘HKCU\Software\Microsoft\Windows\Current\version\RunOnce’ -Name ‘$Aname’ -Value 1 -Type DWord -SID $UserProfile.SID

    I know, this is because scriptblock is an object.

    So I made this and it might work:
    $y=”blabla”
    $x=”Set-RegistryKey -Key ‘HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce’ -Name $y”+’ -Value 0 -Type String -SID $UserProfile.SID’
    $x
    [ScriptBlock]$HKCURegistrySettings = [ScriptBlock]::Create($x)
    $HKCURegistrySettings

    Then I will get:
    Set-RegistryKey -Key ‘HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce’ -Name blabla -Value 0 -Type String -SID $UserProfile.SID

    But this is only on a single line. What if I need more than a registry line?

    So my question is:

    How creating a multiline scriptblock and being able to use it with

    Invoke-HKCURegistrySettingsForAllUsers -RegistrySettings $HKCURegistrySettings

    ?

    THanks,

    • This topic was modified 1 year, 1 month ago by  Francois.
    • This topic was modified 1 year, 1 month ago by  Francois.
    • This topic was modified 1 year, 1 month ago by  Francois.
  • Brian
    Participant
    Post count: 42

    Sorry I’m having a hard time understanding your question. The script block can contain as many registry entries as you want. I would use double quotes around any variables.

    • Francois
      Participant
      Post count: 101

      I made that code to illustrate my subject.

      <pre class=”brush: powershell; gutter: true; first-line: 1; highlight: []; html-script: false”>

      <code>[scriptblock]$HKCURegistrySettings = {
      		Set-RegistryKey -Key &#039;HKCU\Software\Microsoft\Office\14.0\Common&#039; -Name &#039;qmenable&#039; -Value 0 -Type DWord -SID $UserProfile.SID
      		Set-RegistryKey -Key &#039;HKCU\Software\Microsoft\Office\14.0\Common&#039; -Name &#039;updatereliabilitydata&#039; -Value 1 -Type DWord -SID $UserProfile.SID
              Set-RegistryKey -Key &#039;HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce&#039; -Name &#039;blabla&#039; -Value 1 -Type DWord -SID $UserProfile.SID
      	}
      	
      $HKCURegistrySettings
      
      $HKCURegistrySettings2 = {
      @&amp;amp;amp;quot;
      
              set-RegistryKey -Key &#039;HKCU\Software\Microsoft\Office\14.0\Common&#039; -Name &#039;qmenable&#039; -Value 0 -Type DWord -SID $UserProfile.SID
      		Set-RegistryKey -Key &#039;HKCU\Software\Microsoft\Office\14.0\Common&#039; -Name &#039;updatereliabilitydata&#039; -Value 1 -Type DWord -SID $UserProfile.SID
              Set-RegistryKey -Key &#039;HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce&#039; -Name &amp;amp;amp;lt;code&amp;amp;amp;gt;&#039;$test&amp;amp;amp;lt;/code&amp;amp;amp;gt;&#039; -Value 1 -Type DWord -SID $UserProfile.SID
      &amp;amp;amp;quot;@
      }
      $test=&amp;amp;amp;quot;blabla&amp;amp;amp;quot;
      $test3=&amp;amp;amp;amp;$HKCURegistrySettings2
      $test3
      
      [ScriptBlock]$HKCURegistrySettings3 = [ScriptBlock]::Create($test3)
      
      $HKCURegistrySettings -eq $HKCURegistrySettings3</code>

      But I am losing the $UserProfile…

      Thanks,

      • This reply was modified 1 year, 1 month ago by  Francois.
      • This reply was modified 1 year, 1 month ago by  Francois.
      • This reply was modified 1 year, 1 month ago by  Francois.
    • Francois
      Participant
      Post count: 101

      Hi,

      An even easier way to see it:

      $MakeHereString = {
      @”

      Variables 1-3 are:
      Variable 1 = $variable1
      Variable 2 = $variable2
      Variable 3 = $variable3
      “@
      }
      $MakeHereString
      #$Variable2 = “Second”
      #$Variable3 = “Third”

      &$MakeHereString

      So by running it you will get:

      Variables 1-3 are:
      Variable 1 =
      Variable 2 = Second
      Variable 3 = Third

      But what I would like to have is:

      Variables 1-3 are:
      Variable 1 = $variable1
      Variable 2 = Second
      Variable 3 = Third

      So my guess would be to create a scriptblock, replace what I want to replace and then make other variables appearing as a variable.

      Thanks,

  • Francois
    Participant
    Post count: 101

    Hi,
    I will take another approach 🙂 Hard to post code here.
    With the PSADT function Invoke-HKCURegistrySettingsForAllUsers, you have to call the function this way.

    <code>	[scriptblock]$HKCURegistrySettings = {
    		Set-RegistryKey -Key &#039;HKCU\Software\Microsoft\Office\14.0\Common&#039; -Name &#039;qmenable&#039; -Value 0 -Type DWord -SID $UserProfile.SID
    		Set-RegistryKey -Key &#039;HKCU\Software\Microsoft\Office\14.0\Common&#039; -Name &#039;updatereliabilitydata&#039; -Value 1 -Type DWord -SID $UserProfile.SID
    	}
    	Invoke-HKCURegistrySettingsForAllUsers -RegistrySettings $HKCURegistrySettings</code>

    But the -Name part might be a variable.
    In a scriptblock like this, I believe there is no way tu put a variable.

    Is it possible to use a here-string and convert it to a scriptvlock to get the correct value? In my first post, I found a way to make it from a string and then create a scriptblock but it is a single registry key…

You must be logged in to reply to this topic.