Using invoke-command with a Varible

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Mark Prior Mark Prior 1 year, 4 months ago.

  • Author
    Posts
  • #31701
    Profile photo of Mark Prior
    Mark Prior
    Participant

    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

    Thanks

  • #31702
    Profile photo of Mark Prior
    Mark Prior
    Participant

    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

  • #31704
    Profile photo of Lauras Juozulynas
    Lauras Juozulynas
    Participant

    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
    

    results:

    PS C:\Users\xxx> $result
    
    Status   Name               DisplayName                            PSComputerName                                                                                                             
    ------   ----               -----------                            --------------                                                                                                             
    Running  bits               Background Intelligent Transfer Ser... server                                                                                                                  
    
    
  • #31734
    Profile photo of Mark Prior
    Mark Prior
    Participant

    thanks

You must be logged in to reply to this topic.