Using invoke-command with a Varible

so a very simplified version of what i need to achieve here

Enter-PSSession -ComputerName computer01

$result = .\hello.ps1

(hello.ps1 contents)

write-output “Hello world”
$result > c:\helloworld.csv

running the command on the local (computer01) produces a csv with the contents “Hello World”, however running this through invoke-command or enter-pssession created the file with a corrupt entry.

any help would be great


so came up with this, works… not sure if correct but gets me the right result.

$Username = “global\mark.prior”
$Password = ConvertTo-SecureString “PasswordGoesHere”-AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

$scriptblockcontent = {$result = C:.\Check-Details-mptest.ps1 -Identity “mark.prior”

$result > c:\TEMP\Identity\75074.csv

invoke-command -computername computer01 -Authentication credssp -Credential $Cred $scriptblockcontent

Hi Mark,

Here is a way for passing variables to remote command:

$service = "bits"
$result = Invoke-Command -ComputerName "server" -ScriptBlock { Get-Service $args[0] } -ArgumentList $service


PS C:\Users\xxx> $result

Status   Name               DisplayName                            PSComputerName                                                                                                             
------   ----               -----------                            --------------                                                                                                             
Running  bits               Background Intelligent Transfer Ser... server