Article written by

A technology enthu and a Windows PowerShell MVP working on SharePoint solutions at Dell Inc. Has deep interests in Windows Server OS & Virtualization.

12 Responses

Page 1 of 1
  1. PowerShell 2.0 remoting guide: Part 11 – Interpreting, formatting and displaying remote output

    [...] can format this output based on a need. We already discussed various methods to execute commands (part4, part 5 and part 6) on a remote computer. In this post, for the sake of our discussion of remoting [...]

  2. Ismail Ahmed Syed
    Ismail Ahmed Syed at |

    Hi,
    Can i know , how i can execute the custom powershell functions written by me in the script file using the invoke-command.

  3. David Muegge
    David Muegge at |

    To: ravikanthchaganti

    FYI, your comment is incorrect

    From – Get-help Invokecommand -full
    This command allows you to run the script on the remote computers, even if the script file is not accessible to the remote computers.

  4. Executing script existing only on a remote system using Invoke-Command

    [...] But just wanted to make sure I share it here. A while back, in my remoting series, I wrote about executing commands / scripts on a remote machine using Invoke-Command cmdlet. Talking specifically about scripts, you [...]

  5. David Muegge
    David Muegge at |

    Ravikanth

    Thanks for your reply

    Based on your comment below.
    “You can use -FilePath parameter. But the script should exist at the same location on the remote system also”

    It sounded to me like you were saying the script has to exist on the local and remote computers, but it only needs to exist on the local machine the script is being executed on. This just confused me a little so I looked it up and tested it. I thought I would point it out, sorry if I misunderstood.

    I found some great info on your blog, Thanks!

    Dave

  6. Karthi Kumarasamy
    Karthi Kumarasamy at |

    Hi Ravi,

    Glad to read your blog. I’m new to powershell. I’m in middle of writing powershell script to execute a batch file in remote system which contains commands to execute a installer. When i trigger this script from my local system it’s throwing below error in middle after creating new session

    Error Message:
    “Processing data for a remote command failed with the following error message: Bad Request (Invalid Hostname) F
    or more information, see the about_Remote_Troubleshooting Help topic.
        + CategoryInfo          : OperationStopped: (System.Manageme…pressionSyncJob:PSInvokeExpressionSyncJob) [], PSRe
       motingTransportException
        + FullyQualifiedErrorId : JobFailure ”

    Script :
    —————————————————-
    $Username = “username”
    $Password = “password”
    $ComputerName = “computername”

    $script= {c:PPCSample.ps1}
     
    $SecurePassWord = ConvertTo-SecureString -AsPlainText $Password -Force
    $Cred = New-Object -TypeName “System.Management.Automation.PSCredential” -ArgumentList $Username, $SecurePassWord

    $Session =New-Pssession -ComputerName $ComputerName -credential $Cred

    Invoke-Command -Session $Session -ScriptBlock $script

    Remove-PSSession -Session $Session

    —————————————————

    Can you please help me to fix the issue ?

    Thanks,

    karthi K

  7. kiquenet kiquenet
    kiquenet kiquenet at |

    My tests:

    $ItemLocation = “C:TempFolderInRemoteMachine” + “$FileName”
    $ItemLocation2 = “.” + “$FileName”

    Invoke-Command  works OK:

    Invoke-Command -credential $testCred -ComputerName $serverName -FilePath “$ItemLocation2″

    Invoke-Command -credential $testCred -computer $serverName -Script{ C:TempFolderInRemoteMachine\Script1.ps1 }

    Invoke-Command  doesn’t works: 

     Invoke-Command -credential $testCred -computer $serverName -Script{ “$ItemLocation” }

  8. kiquenet kiquenet
    kiquenet kiquenet at |

    Passing local variables (more than 1 variable) as arguments to remote computer would be very useful. Thx

  9. Ivan
    Ivan at |

    U can use

    invoke-command -Session $Session -FilePath $script

Comments are closed, but trackbacks and pingbacks are open.

%d bloggers like this: