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;#039;HKCU\Software\Microsoft\Windows\Current\version\RunOnce&amp;amp;amp;#039; -Name &amp;amp;amp;#039;<strong>$Aname</strong>&amp;amp;amp;#039; -Value 1 -Type DWord -SID $UserProfile.SID
}&amp;amp;lt;/code&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,
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.
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 '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
}
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…
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 '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\CurrentVersion\RunOnce' -Name 'blabla' -Value 1 -Type DWord -SID $UserProfile.SID
}
$HKCURegistrySettings
$HKCURegistrySettings2 = {
@&amp;amp;quot;
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\CurrentVersion\RunOnce' -Name &amp;amp;lt;code&amp;amp;gt;'$test&amp;amp;lt;/code&amp;amp;gt;' -Value 1 -Type DWord -SID $UserProfile.SID
&amp;amp;quot;@
}
$test=&amp;amp;quot;blabla&amp;amp;quot;
$test3=&amp;amp;amp;$HKCURegistrySettings2
$test3
[ScriptBlock]$HKCURegistrySettings3 = [ScriptBlock]::Create($test3)
$HKCURegistrySettings -eq $HKCURegistrySettings3</code>
But I am losing the $UserProfile…
Thanks,
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,