using a variable with invoke Command

Hello,
I have a question for using Variables with Invoke-Command
I am working on a script for changing a reg value

my script is:

Function Fix-FIPS {
Param
(
$SID,
$Computername
)
$Script blockContent = {
Param($SID)
get-itemproerty HKU:$SID\Software\Adobe\Acrobat Reader\10.0\AVGeneral}

$PC = new-PSsession -Computername $Computername
Invoke-Command -Session $PC -Scriptblock {New-PSDrive HKU Registry HKey_Users}

Invoke-Command -Session $PC -ScriptBlock $Script blockContent

but I have run into a problem,
it seems if I write HKU:$SID\Software the script seems to Ignore the variable totally, if i put a space around $SID HKU:\ $SID \Software
the scrip sees the \Software as an argument not as part of the path.
Not quite sure how to get the script to pick up the $SID into the path correctly.

also Is there a better way to run every thing all at once with invoke command?
I will be making some changes to the Reg value but can only do it with the created PSDrive HKU and it drops at te end of each session so if I coudl do it all in one Line with invoke command because I suspect that it is part of my problem.

Thanks for the help

Hey Brian,

You will need to use the Using scope modifier in your script block (http://technet.microsoft.com/en-us/library/jj149005.aspx) or see example 9 of the Invoke-Command help (http://technet.microsoft.com/en-us/library/hh849719.aspx).

Function Fix-FIPS {
  Param (
    $SID,
    $Computername
  )
 
  $Script = {
    New-PSDrive HKU Registry HKey_Users
    get-itemproperty HKU:\$Using:SID\Software\Adobe\Acrobat Reader\10.0\AVGeneral
  }

 $PC = new-PSsession -Computername $Computername
 Invoke-Command -Session $PC -Scriptblock $Script
}

I think a better way would be to develop and test your “remote” script as normal script in the ISE or your favourite editor and use the -FilePath parameter of Invoke-Command.

Function Fix-FIPS {
  Param (
    $ComputerName,
    $SID
  )
 
  $PC = new-PSsession -Computername $Computername
 Invoke-Command -Session $PC -FilePath C:\MyFixFIPSRemote.ps1 -ArgumentList $SID 

The MyFixFIPSRemote.ps1 could look like below:

Param (
  [Parameter(Mandatory=$true)]
  [String]
  $SID
)

New-PSDrive HKU Registry HKey_Users
get-itemproperty HKU:\$SID\Software\Adobe\Acrobat Reader\10.0\AVGeneral
# more commands

I hope above helps.

Best and Merry Christmas,
Daniel

Sadly I am limited to Powershell 2.0
I figured out a work around by getting the Variables via user input once the session is made.
it is not as sexy but it is a way to do it I guess lol

In PowerShell 2.0 you can just use the -ArgumentList parameter to Invoke-Command. For example:

Function Fix-FIPS {
    Param
    (
        $SID,
        $Computername
    )

    $ScriptblockContent = {
        Param($SID)
        Get-ItemProperty HKU:\$SID\Software\Adobe\Acrobat Reader\10.0\AVGeneral
    }

    $PC = New-PSSession -Computername $Computername

    Invoke-Command -Session $PC -Scriptblock {New-PSDrive HKU Registry HKey_Users}
    Invoke-Command -Session $PC -ScriptBlock $ScriptblockContent -ArgumentList $SID

    $PC | Remove-PSSession
}

I’d recommend staying away from assigning your variable in the remote session - especially since there’s no reason to do it that way, and because you’ll do so via user input. Even if you don’t have PowerShell 3.0, you can get locally assigned variables into your remote session. The underlying problem here, by the way, is that variables referenced in the remote session, are assumed to be defined in the remote session.

Although Dave provided you a helpful answer, I wanted to share a couple links. Read the bottom, PowerShell 2.0 section of this help file: http://technet.microsoft.com/en-us/library/jj149005.aspx (or, Get-Help about_Remote_Variables), and this PowerShell Team blog: http://blogs.msdn.com/b/powershell/archive/2009/12/29/arguments-for-remote-commands.aspx.

I’ve referenced the second link several times (before I learned about the Using scope modifier).