1

I started a script with script-block:

[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
    }

So this is what it must look.

OK, but I need a variable.

$HKCURegistrySettings2 = {
@"

        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 `'$test`' -Value 1 -Type DWord -SID $UserProfile.SID
"@
}

So I replace blabla by $test.

$test="blabla"
$test3=&$HKCURegistrySettings2
$test3

[ScriptBlock]$HKCURegistrySettings3 = [ScriptBlock]::Create($test3)

$HKCURegistrySettings -eq $HKCURegistrySettings3

So now by comparing my first $HKCURegistrySettings and my now $HKCURegistrySettings3

They should be the same. But I get a false. 1. Why are they different? 2. How may I get to make them identical? 3. The variables are define AFTER the Here-strings creation. Other option?

When the scriptblock is create it is then use to call a function Initially:

Invoke-HKCURegistrySettingsForAllUsers -RegistrySettings $HKCURegistrySettings

and now

Invoke-HKCURegistrySettingsForAllUsers -RegistrySettings $HKCURegistrySettings3

So this is why the result should be the same.

Thanks,

Functino
  • 1,939
  • 17
  • 25
François
  • 11
  • 2

2 Answers2

1

HKCURegistrySettings2 expands other variables too, so $test3 string no longer has $UserProfile.SID, it's been expanded. Compare the contents yourself by running "$HKCURegistrySettings" and "$HKCURegistrySettings3" in the PS command prompt.

You can escape those variables that don't need expansion by using `$ instead of $:

$HKCURegistrySettings2 = {
@"

        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 `'$test`' -Value 1 -Type DWord -SID `$UserProfile.SID
"@
}

And then compare the trimmed contents:

"$HKCURegistrySettings".trim() -eq "$HKCURegistrySettings3".trim()

True

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
0

Your ScriptBlock can take parameters, just like a function. For example:

$sb = { param($x) $a = 'hello'; echo "$a $x!"; }
& $sb 'Powershell'

Should print Hello Powershell!

Burt_Harris
  • 6,415
  • 2
  • 29
  • 64