Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7727319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:25:49+00:00 2026-06-01T05:25:49+00:00

I need to be able to execute a PS1 script that resides on a

  • 0

I need to be able to execute a PS1 script that resides on a remote machine against another remote machine through a C# runspace.

To be clear what I mean by this: The service I’m creating resides on server A. It creates a remote runspace to server B using the method below. Through the runspace I’m trying to call a script residing on server C against server B. If it helps, currently server A IS server C, but it’s not guaranteed that will always be the case.

Here’s the method I’m using to make the remote call:

internal Collection<PSObject> RunRemoteScript(string remoteScript, string remoteServer, string scriptName, out bool scriptSuccessful)
    {
        bool isLocal = (remoteServer == "localhost" || remoteServer == "127.0.0.1" || remoteServer == Environment.MachineName);

        WSManConnectionInfo connectionInfo = null;

        if (!isLocal)
        {
            connectionInfo = new WSManConnectionInfo(new Uri("http://" + remoteServer + ":5985"));
        }

        PsHostImplementation myHost = new PsHostImplementation(scriptName);

        using (Runspace remoteRunspace = (isLocal ? RunspaceFactory.CreateRunspace(myHost) : RunspaceFactory.CreateRunspace(myHost, connectionInfo)))
        {
            remoteRunspace.Open();
            using (PowerShell powershell = PowerShell.Create())
            {
                powershell.Runspace = remoteRunspace;

                Pipeline pipeline = remoteRunspace.CreatePipeline();

                pipeline.Commands.AddScript(remoteScript);

                Collection<PSObject> results = pipeline.Invoke();

                remoteRunspace.Close();

                scriptSuccessful = myHost.ScriptSuccessful;
                return results;
            }
        }
    }

“remoteScript” is set to the Powershell script I want to run. For example:

"& \"\\\\remoteserveraddress\\PathToScript\\Install.ps1\" -Parameter;Import-Module Modulename;CustomCommand-FromModule -parameter(s) -ErrorAction stop"

If I’m on the remote machine that I want to run the script on, in the powershell console I can just give the following command:

& "\\remoteserverC\PathToScript\Install.ps1" -Parameter

However this simply refuses to work for me if I try to run it through the c# runspace.

If I send in the following as a parameter to “remoteScript”:

"& \"\\\\remoteserverC\\PathToScript\\Install.ps1\" -Parameter"

I get the following error:

The term ‘\remoteserverC\PathToScript\Install.ps1’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I’ve tried with and without ‘&’ and with and without the parameter. I can already call a script that resides directly on the remote machine “c:\…\Install.ps1” instead of “\\remoteserver\…\Install.ps1”, but it would be greatly beneficial to be able to call the remote script directly.

I’ve searched many many pages in google and here on stackoverflow, but I haven’t been able to find anything that helps to overcome this issue. Any help would be appreciated! Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:25:51+00:00Added an answer on June 1, 2026 at 5:25 am

    I never did get this to work directly and seems to be a security “feature” that you can’t access a third machine using a UNC address while working remotely. I was however able to find a workaround that worked great for me.

    Instead of trying to call directly to a \\server\share address, I dynamically map a network drive on the machine I’m trying to run the script against to a share on the machine that has the script. (Running remotely from A, map a drive on B to a share on C). Then I call my scripts through that drive and it works like a charm. This string is what I pass in to the RunRemoteScript method above:

    "$net = new-object -ComObject WScript.Network;" +
    "if(!($net.EnumNetworkDrives() -contains \"S:\"))" +
        "{Write-Host \"S: Drive Not Currently Mapped. Mapping to \\\\" + RemoteServerC + "\\Share.\";" +
        "$net.MapNetWorkDrive(\"S:\",\"\\\\" + RemoteServerC + "\\Share\",$false,\"username\",\"password\")};" +
    "Get-PSDrive | Write-Verbose;" +
    "& \"S:\\PathToScript\\Install.ps1\" -noPrompt;"
    

    RemoteServerC is a variable I pass in that is defined in a user config file.

    Here is the same code as just powershell script if anyone needs it (replacing RemoteServerC with a powershell variable you’d need to set before or just hardcode:

    $net = new-object -ComObject WScript.Network
    if(!($net.EnumNetworkDrives() -contains "S:"))
    {  Write-Host "S: Drive Not Currently Mapped. Mapping to \\remoteserverC\Share."
       $net.MapNetWorkDrive("S:","\\remoteserverC\Share",$false,"username","password")
    }
    Get-PSDrive | Write-Verbose
    & "S:\\PathToScript\\Install.ps1\" -noPrompt;"
    

    First I set up the object to be able to map the drive. I then check if there is already an “s” drive mapped on the remote machine. If it hasn’t I then map the share to the “s” drive on the remote machine using a name and password we set up in active directory to run this service.

    I included the Get-PSDrive command because it appears to force powershell to reload the list of available drives. This seems to only matter the very first time you try to run this in a powershell session (and through c sharp I don’t know if it is truly necessary or not, I included it to be safe). Apparently powershell does not recognize a drive addition in the same session it was created if you use MapNetworkDrive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a chrome extension and I need to be able to execute
I need to be able to execute either a php or jquery function to
I need to be able to execute (via JDBC) a straightforward SQL select query
I use the great tablesorter plugin. I need to be able to execute something
I need to be able to execute a task for all sub-directories with a
What I need is to be able to execute code in a code-behind for
I have a Matlab script consisting of several cells that should all be able
I need to be able to conditionally execute a method. I prefer not having
I have some code that plots triangles in MATLAB. I need to be able
I need to be able to loop through column names and put the coloumn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.