Variables in your PowerShell session are not transferred to sessions created with Invoke-Command
You need to use the -ArgumentList
parameter to send the variables your command and then use the $args
array to access them in the script block so your command will look like:
Invoke-Command -ComputerName $remoteMachineName -ScriptBlock {& $args[0]} -ArgumentList $x