本文整理汇总了C#中PowerShell类的典型用法代码示例。如果您正苦于以下问题:C# PowerShell类的具体用法?C# PowerShell怎么用?C# PowerShell使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PowerShell类属于命名空间,在下文中一共展示了PowerShell类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Init
public void Init()
{
_ps = Utilities.CreatePowerShell(CmdletName, typeof(GetRelease));
var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);
// Create a project
var project = new ProjectResource { Name = "Octopus" };
octoRepo.Setup(o => o.Projects.FindByName("Octopus")).Returns(project);
octoRepo.Setup(o => o.Projects.Get("projects-1")).Returns(project);
octoRepo.Setup(o => o.Projects.Get("Gibberish")).Throws(new OctopusResourceNotFoundException("Not Found"));
var releases = new List<ReleaseResource>
{
new ReleaseResource {Version = "1.0.0"},
new ReleaseResource {Version = "1.0.1"},
new ReleaseResource {Version = "1.1.0"}
};
octoRepo.Setup(o => o.Projects.GetReleases(project, 0))
.Returns(new ResourceCollection<ReleaseResource>(releases, new LinkCollection()));
octoRepo.Setup(o => o.Projects.GetReleaseByVersion(project, "1.0.0")).Returns(releases[0]);
octoRepo.Setup(o => o.Projects.GetReleaseByVersion(project, "Gibberish")).Throws(new OctopusResourceNotFoundException("Not found"));
octoRepo.Setup(o => o.Releases.Get("releases-1")).Returns(releases[0]);
octoRepo.Setup(o => o.Releases.Get("Gibberish")).Throws(new OctopusResourceNotFoundException("Not found"));
}
示例2: Init
public void Init()
{
_ps = Utilities.CreatePowerShell(CmdletName, typeof(AddVariable));
var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);
_variableSet.Variables.Clear();
var project = new ProjectResource {DeploymentProcessId = "deploymentprocesses-1"};
project.Links.Add("Variables", "variablesets-1");
octoRepo.Setup(o => o.Projects.FindByName("Octopus")).Returns(project);
octoRepo.Setup(o => o.Projects.FindByName("Gibberish")).Returns((ProjectResource) null);
octoRepo.Setup(o => o.VariableSets.Get("variablesets-1")).Returns(_variableSet);
var process = new DeploymentProcessResource();
process.Steps.Add(new DeploymentStepResource {Name = "Website", Id = "Step-1"});
octoRepo.Setup(o => o.DeploymentProcesses.Get("deploymentprocesses-1")).Returns(process);
var envs = new List<EnvironmentResource>
{
new EnvironmentResource {Id = "Environments-1", Name = "DEV"}
};
octoRepo.Setup(o => o.Environments.FindByNames(new[] {"DEV"})).Returns(envs);
var machines = new List<MachineResource>
{
new MachineResource {Id = "Machines-1", Name = "web-01"}
};
octoRepo.Setup(o => o.Machines.FindByNames(new[] { "web-01" })).Returns(machines);
}
示例3: Init
public void Init()
{
_ps = Utilities.CreatePowerShell(CmdletName, typeof(RemoveProjectGroup));
var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);
// Create some library variable sets
_groups.Clear();
_groups.Add(new ProjectGroupResource { Id = "ProjectGroups-1", Name = "Octopus" });
_groups.Add(_group);
_groups.Add(new ProjectGroupResource { Id = "ProjectGroups-3", Name = "Automation" });
octoRepo.Setup(o => o.ProjectGroups.Delete(It.IsAny<ProjectGroupResource>())).Returns(
delegate(ProjectGroupResource set)
{
if (_groups.Contains(set))
_groups.Remove(set);
else
throw new KeyNotFoundException("The given key was not present in the dictionary.");
return new TaskResource();
}
);
octoRepo.Setup(o => o.ProjectGroups.Get("ProjectGroups-2")).Returns(_group);
octoRepo.Setup(o => o.ProjectGroups.Get(It.IsNotIn(new[] { "ProjectGroups-2" })))
.Throws(new OctopusResourceNotFoundException("Not Found"));
octoRepo.Setup(o => o.ProjectGroups.FindByName("Test")).Returns(_group);
octoRepo.Setup(o => o.ProjectGroups.FindByName("Gibberish")).Returns((ProjectGroupResource)null);
}
示例4: Init
public void Init()
{
_ps = Utilities.CreatePowerShell(CmdletName, typeof(AddLibraryVariable));
var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);
_variableSet.Variables.Clear();
var lib = new LibraryVariableSetResource { Name = "Octopus" };
var libs = new List<LibraryVariableSetResource> {lib};
lib.Links.Add("Variables", "variablesets-1");
octoRepo.Setup(o => o.LibraryVariableSets.FindOne(It.IsAny<Func<LibraryVariableSetResource, bool>>()))
.Returns(
(Func<LibraryVariableSetResource, bool> f) =>
(from l in libs where f(l) select l).FirstOrDefault());
octoRepo.Setup(o => o.Projects.FindByName("Gibberish")).Returns((ProjectResource)null);
octoRepo.Setup(o => o.VariableSets.Get("variablesets-1")).Returns(_variableSet);
var process = new DeploymentProcessResource();
process.Steps.Add(new DeploymentStepResource { Name = "Website", Id = "Step-1" });
octoRepo.Setup(o => o.DeploymentProcesses.Get("deploymentprocesses-1")).Returns(process);
var envs = new List<EnvironmentResource>
{
new EnvironmentResource {Id = "Environments-1", Name = "DEV"}
};
octoRepo.Setup(o => o.Environments.FindByNames(new[] { "DEV" })).Returns(envs);
var machines = new List<MachineResource>
{
new MachineResource {Id = "Machines-1", Name = "web-01"}
};
octoRepo.Setup(o => o.Machines.FindByNames(new[] { "web-01" })).Returns(machines);
}
示例5: ExecuteScript
public void ExecuteScript(ScriptConfigElement e)
{
using (_instance = PowerShell.Create())
{
_instance.AddScript(File.ReadAllText(e.PathToScript));
PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
outputCollection.DataAdded += outputCollection_DataAdded;
_instance.Streams.Progress.DataAdded += Progress_DataAdded;
_instance.Streams.Error.DataAdded += Error_DataAdded;
_instance.Streams.Verbose.DataAdded += Verbose_DataAdded;
_instance.Streams.Debug.DataAdded += Debug_DataAdded;
_instance.Streams.Warning.DataAdded += Warning_DataAdded;
IAsyncResult result = _instance.BeginInvoke<PSObject, PSObject>(null,
outputCollection);
while (result.IsCompleted == false)
{
Thread.Sleep(500);
}
foreach (PSObject o in outputCollection)
{
Console.WriteLine(o.GetType());
Console.WriteLine(o);
}
}
}
示例6: Init
public void Init()
{
_ps = Utilities.CreatePowerShell(CmdletName, typeof (GetProjectGroup));
var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);
// Create some project groups
var groupRepo = new Mock<IProjectGroupRepository>();
var groupResources = new List<ProjectGroupResource>
{
new ProjectGroupResource {Name = "Octopus", Id = "projectgroups-1"},
new ProjectGroupResource {Name = "Deploy", Id = "projectgroups-2"}
};
groupRepo.Setup(p => p.FindAll()).Returns(groupResources);
groupRepo.Setup(p => p.FindByNames(It.IsAny<string[]>())).Returns(
(string[] names) =>
(from n in names
from g in groupResources
where g.Name.Equals(n, StringComparison.InvariantCultureIgnoreCase)
select g).ToList());
groupRepo.Setup(p => p.Get(It.IsAny<string>())).Returns(CreateGet(groupResources));
octoRepo.Setup(o => o.ProjectGroups).Returns(groupRepo.Object);
}
示例7: Init
public void Init()
{
_ps = Utilities.CreatePowerShell(CmdletName, typeof (RemoveVariableSet));
var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);
// Create some library variable sets
_sets.Clear();
_sets.Add(new LibraryVariableSetResource {Id = "LibraryVariableSets-1", Name = "ConnectionStrings"});
_sets.Add(_set);
_sets.Add(new LibraryVariableSetResource {Id = "LibraryVariableSets-3", Name = "Service Endpoints"});
octoRepo.Setup(o => o.LibraryVariableSets.Delete(It.IsAny<LibraryVariableSetResource>())).Returns(
delegate(LibraryVariableSetResource set)
{
if (_sets.Contains(set))
_sets.Remove(set);
else
throw new KeyNotFoundException("The given key was not present in the dictionary.");
return new TaskResource();
}
);
octoRepo.Setup(o => o.LibraryVariableSets.Get("LibraryVariableSets-2")).Returns(_set);
octoRepo.Setup(o => o.LibraryVariableSets.Get(It.IsNotIn(new[] {"LibraryVariableSets-2"})))
.Throws(new OctopusResourceNotFoundException("Not Found"));
// Allow the FindOne predicate to operate on the collection
octoRepo.Setup(o => o.LibraryVariableSets.FindOne(It.IsAny<Func<LibraryVariableSetResource, bool>>()))
.Returns(
(Func<LibraryVariableSetResource, bool> f) =>
(from l in _sets where f(l) select l).FirstOrDefault());
}
示例8: Init
public void Init()
{
_ps = Utilities.CreatePowerShell(CmdletName, typeof(GetDeployment));
var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);
// Create a project
var projectResource = new ProjectResource {Name = "Octopus"};
octoRepo.Setup(o => o.Projects.FindByName("Octopus")).Returns(projectResource);
octoRepo.Setup(o => o.Projects.FindByName("Gibberish")).Returns((ProjectResource) null);
// Create a Release
var release = new ReleaseResource { Id = "Releases-1", Links = new LinkCollection()};
release.Links.Add("Deployments", "/api/releases/releases-1/deployments");
octoRepo.Setup(o => o.Projects.GetReleaseByVersion(projectResource, "1.0.0")).Returns(release);
octoRepo.Setup(o => o.Projects.GetReleaseByVersion(projectResource, "Gibberish")).Returns((ReleaseResource) null);
// Create Deployments
var deployments = new ResourceCollection<DeploymentResource>(new List<DeploymentResource>
{
new DeploymentResource {Id = "deployments-1"}
}, new LinkCollection());
octoRepo.Setup(o => o.Releases.GetDeployments(release, 0)).Returns(deployments);
octoRepo.Setup(o => o.Releases.Get("Releases-1")).Returns(release);
}
示例9: Start
/// <summary>
/// Called to start the COM handler.
/// </summary>
/// <param name="data">Data string passed in from Task Scheduler action.</param>
public override void Start(string data)
{
psInstance = PowerShell.Create();
psInstance.AddScript(data);
result = psInstance.BeginInvoke();
psInstance.InvocationStateChanged += PowerShellInstance_InvocationStateChanged;
}
示例10: ServerSteppablePipelineDriver
internal ServerSteppablePipelineDriver(PowerShell powershell, bool noInput, Guid clientPowerShellId, Guid clientRunspacePoolId, ServerRunspacePoolDriver runspacePoolDriver, ApartmentState apartmentState, HostInfo hostInfo, System.Management.Automation.RemoteStreamOptions streamOptions, bool addToHistory, Runspace rsToUse, ServerSteppablePipelineSubscriber eventSubscriber, PSDataCollection<object> powershellInput)
{
this.localPowerShell = powershell;
this.clientPowerShellId = clientPowerShellId;
this.clientRunspacePoolId = clientRunspacePoolId;
this.remoteStreamOptions = streamOptions;
this.apartmentState = apartmentState;
this.noInput = noInput;
this.addToHistory = addToHistory;
this.eventSubscriber = eventSubscriber;
this.powershellInput = powershellInput;
this.input = new PSDataCollection<object>();
this.inputEnumerator = this.input.GetEnumerator();
this.input.ReleaseOnEnumeration = true;
this.dsHandler = runspacePoolDriver.DataStructureHandler.CreatePowerShellDataStructureHandler(clientPowerShellId, clientRunspacePoolId, this.remoteStreamOptions, null);
this.remoteHost = this.dsHandler.GetHostAssociatedWithPowerShell(hostInfo, runspacePoolDriver.ServerRemoteHost);
this.dsHandler.InputEndReceived += new EventHandler(this.HandleInputEndReceived);
this.dsHandler.InputReceived += new EventHandler<RemoteDataEventArgs<object>>(this.HandleInputReceived);
this.dsHandler.StopPowerShellReceived += new EventHandler(this.HandleStopReceived);
this.dsHandler.HostResponseReceived += new EventHandler<RemoteDataEventArgs<RemoteHostResponse>>(this.HandleHostResponseReceived);
this.dsHandler.OnSessionConnected += new EventHandler(this.HandleSessionConnected);
if (rsToUse == null)
{
throw PSTraceSource.NewInvalidOperationException("RemotingErrorIdStrings", "NestedPipelineMissingRunspace", new object[0]);
}
this.localPowerShell.Runspace = rsToUse;
eventSubscriber.SubscribeEvents(this);
this.stateOfSteppablePipeline = PSInvocationState.NotStarted;
}
示例11: Init
public void Init()
{
_ps = Utilities.CreatePowerShell(CmdletName, typeof (GetStep));
var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);
const string deploymentProcessId = "deploymentprocess-projects-1";
// Create a project
var projectResources = new List<ProjectResource>
{
new ProjectResource {Name = "Octopus", DeploymentProcessId = deploymentProcessId, Id = "projects-1"}
};
octoRepo.Setup(o => o.Projects.FindByNames(new[] {"Octopus"})).Returns(projectResources);
octoRepo.Setup(o => o.Projects.FindByNames(new[] {"Gibberish"})).Returns(new List<ProjectResource>());
octoRepo.Setup(o => o.Projects.Get("projects-1")).Returns(projectResources[0]);
octoRepo.Setup(o => o.Projects.Get("Gibberish")).Throws(new OctopusResourceNotFoundException("Not Found"));
var process = new DeploymentProcessResource();
//{
// Id = "deploymentprocess-projects-1"
//};
process.Steps.Add(new DeploymentStepResource { Name = "Test Step" });
process.Steps.Add(new DeploymentStepResource { Name = "Test Step 2" });
octoRepo.Setup(o => o.DeploymentProcesses.Get(It.IsIn(new[] {deploymentProcessId})))
.Returns(process);
octoRepo.Setup(o => o.DeploymentProcesses.Get(It.IsNotIn(new[] {deploymentProcessId})))
.Throws(new OctopusResourceNotFoundException("Not Found"));
}
示例12: Init
public void Init()
{
_ps = Utilities.CreatePowerShell(CmdletName, typeof(AddProject));
var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);
// Create a project group
var groupResource = new ProjectGroupResource {Name = "Octopus", Id = "projectgroups-1"};
octoRepo.Setup(o => o.ProjectGroups.FindByName("Octopus")).Returns(groupResource);
octoRepo.Setup(o => o.ProjectGroups.Get(It.IsIn(new[] { "projectgroups-1" })))
.Returns(groupResource);
octoRepo.Setup(o => o.ProjectGroups.Get(It.IsNotIn(new[] { "projectgroups-1" })))
.Throws(new OctopusResourceNotFoundException("Not Found"));
_projects.Clear();
var repo = new Mock<IProjectRepository>();
repo.Setup(e => e.Create(It.IsAny<ProjectResource>()))
.Returns(delegate(ProjectResource p)
{
_projects.Add(p);
return p;
});
octoRepo.Setup(o => o.Projects).Returns(repo.Object);
}
示例13: Init
public void Init()
{
_ps = Utilities.CreatePowerShell(CmdletName, typeof(GetVariableSet));
var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);
// Create a library variable set
const string vsId = "/api/variables/variableset-LibraryVariableSets-1";
var libraryResources = new List<LibraryVariableSetResource>
{
new LibraryVariableSetResource {Id = "LibraryVariableSets-1", Name = "Octopus"},
new LibraryVariableSetResource {Id = "LibraryVariableSets-2", Name = "Deploy"},
new LibraryVariableSetResource {Id = "LibraryVariableSets-3", Name = "Automation"}
};
octoRepo.Setup(o => o.LibraryVariableSets.FindAll()).Returns(libraryResources);
// Create a variableset
var variableRepo = new Mock<IVariableSetRepository>();
var vsResource = new VariableSetResource
{
Variables = new List<VariableResource>
{
new VariableResource {Name = "Octopus"},
new VariableResource {Name = "Deploy"},
new VariableResource {Name = "Automation"},
}
};
variableRepo.Setup(v => v.Get(vsId)).Returns(vsResource);
octoRepo.Setup(o => o.VariableSets).Returns(variableRepo.Object);
}
示例14: PrintPSCommand
protected void PrintPSCommand(PowerShell powershell)
{
StringBuilder command = new StringBuilder();
DateTime currentTime = DateTime.UtcNow;
command.Append("[" + currentTime.ToShortDateString() + " " + currentTime.ToLongTimeString() + "] ");
for (int i = 0; i < powershell.Commands.Commands.Count; i++)
{
command.Append(powershell.Commands.Commands[i].CommandText + " ");
for (int j = 0; j < powershell.Commands.Commands[i].Parameters.Count; j++)
{
command.Append("-" + powershell.Commands.Commands[i].Parameters[j].Name + " ");
var value = powershell.Commands.Commands[i].Parameters[j].Value;
if (value != null)
{
command.Append("\"" + value.ToString() + "\" ");
}
command.Append("\n");
}
}
Console.WriteLine(command);
}
示例15: ServerPowerShellDriver
private IRSPDriverInvoke _psDriverInvoker; // Handles nested invocation of PS drivers.
#endregion Private Members
#region Constructors
#if !CORECLR
/// <summary>
/// Default constructor for creating ServerPowerShellDrivers
/// </summary>
/// <param name="powershell">decoded powershell object</param>
/// <param name="extraPowerShell">extra pipeline to be run after <paramref name="powershell"/> completes</param>
/// <param name="noInput">whether there is input for this powershell</param>
/// <param name="clientPowerShellId">the client powershell id</param>
/// <param name="clientRunspacePoolId">the client runspacepool id</param>
/// <param name="runspacePoolDriver">runspace pool driver
/// which is creating this powershell driver</param>
/// <param name="apartmentState">apartment state for this powershell</param>
/// <param name="hostInfo">host info using which the host for
/// this powershell will be constructed</param>
/// <param name="streamOptions">serialization options for the streams in this powershell</param>
/// <param name="addToHistory">
/// true if the command is to be added to history list of the runspace. false, otherwise.
/// </param>
/// <param name="rsToUse">
/// If not null, this Runspace will be used to invoke Powershell.
/// If null, the RunspacePool pointed by <paramref name="runspacePoolDriver"/> will be used.
/// </param>
internal ServerPowerShellDriver(PowerShell powershell, PowerShell extraPowerShell, bool noInput, Guid clientPowerShellId,
Guid clientRunspacePoolId, ServerRunspacePoolDriver runspacePoolDriver,
ApartmentState apartmentState, HostInfo hostInfo, RemoteStreamOptions streamOptions,
bool addToHistory, Runspace rsToUse)
: this(powershell, extraPowerShell, noInput, clientPowerShellId, clientRunspacePoolId, runspacePoolDriver,
apartmentState, hostInfo, streamOptions, addToHistory, rsToUse, null)
{
}