Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Los comandos de PowerShell que toman un parámetro ComputerName se pueden ejecutar en cualquier equipo que ejecute PowerShell. Para ejecutar comandos que no toman un parámetro ComputerName, puede usar WS-Management para configurar un espacio de ejecución que se conecte a un equipo especificado y ejecute comandos en ese equipo.
Uso de WSManConnection para crear un espacio de ejecución remoto
Para crear un espacio de ejecución que se conecte a un equipo remoto, cree un objeto System.Management.Automation.Runspaces.WSManConnectionInfo. Especifique el punto de conexión de destino para la conexión estableciendo la propiedad System.Management.Automation.Runspaces.WSManConnectionInfo.ConnectionUri del objeto. Después, cree un espacio de ejecución llamando al método System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace, especificando el objeto System.Management.Automation.Runspaces.WSManConnectionInfo como parámetro connectionInfo.
En el ejemplo siguiente se muestra cómo crear un espacio de ejecución que se conecte a un equipo remoto. En el ejemplo, RemoteComputerUri se usa como marcador de posición para el URI real de un equipo remoto.
namespace Samples
{
using System;
using System.Collections.ObjectModel;
using System.Management.Automation; // PowerShell namespace.
using System.Management.Automation.Runspaces; // PowerShell namespace.
/// <summary>
/// This class contains the Main entry point for this host application.
/// </summary>
internal class RemoteRunspace02
{
/// <summary>
/// This sample shows how to create a remote runspace that
/// runs commands on the local computer.
/// </summary>
/// <param name="args">Parameter not used.</param>
private static void Main(string[] args)
{
// Create a WSManConnectionInfo object using the default constructor
// to connect to the "localHost". The WSManConnectionInfo object can
// also be used to specify connections to remote computers.
Uri RemoteComputerUri = new Uri("http://Server01:5985/WSMAN");
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(RemoteComputerUri);
// Set the OperationTimeout property and OpenTimeout properties.
// The OperationTimeout property is used to tell PowerShell
// how long to wait (in milliseconds) before timing out for an
// operation. The OpenTimeout property is used to tell Windows
// PowerShell how long to wait (in milliseconds) before timing out
// while establishing a remote connection.
connectionInfo.OperationTimeout = 4 * 60 * 1000; // 4 minutes.
connectionInfo.OpenTimeout = 1 * 60 * 1000; // 1 minute.
// Create a remote runspace using the connection information.
//using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace())
using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
// Establish the connection by calling the Open() method to open the runspace.
// The OpenTimeout value set previously will be applied while establishing
// the connection. Establishing a remote connection involves sending and
// receiving some data, so the OperationTimeout will also play a role in this process.
remoteRunspace.Open();
// Create a PowerShell object to run commands in the remote runspace.
using (PowerShell powershell = PowerShell.Create())
{
powershell.Runspace = remoteRunspace;
powershell.AddCommand("Get-Process");
powershell.Invoke();
Collection<PSObject> results = powershell.Invoke();
Console.WriteLine("Process HandleCount");
Console.WriteLine("--------------------------------");
// Display the results.
foreach (PSObject result in results)
{
Console.WriteLine(
"{0,-20} {1}",
result.Members["ProcessName"].Value,
result.Members["HandleCount"].Value);
}
}
// Close the connection. Call the Close() method to close the remote
// runspace. The Dispose() method (called by using primitive) will call
// the Close() method if it is not already called.
remoteRunspace.Close();
}
}
}
}