public static NetAppResponse DeleteVolume(DeveloperParameters d, string connectionData)
{
using (var psInstance = PowerShell.Create())
{
// STEP 1: first things first. we need to set the execution policy for this script to be unrestricted. because we're assuming not everyone signs their powershell scripts.
psInstance.AddCommand("Set-ExecutionPolicy");
psInstance.AddArgument("Unrestricted");
psInstance.AddArgument("Process");
psInstance.AddParameter("-Force");
psInstance.Invoke();
var debugStream = string.Empty;
var errorStream = string.Empty;
debugStream += psInstance.Streams.Debug.Aggregate(debugStream, (current, debug) => current + debug);
debugStream += psInstance.Streams.Progress.Aggregate(debugStream,
(current, debug) => current + debug);
psInstance.Commands.Clear();
// -------------------------------------------------------------------------------------------/
// STEP 2: We need to copy the file locally. Now - for all intensive purposes, we're going to assume the file is in 1 of three places
// - Locally on the platform (in which an absolute path is provided)
// - Accessible server within the datacenter (in which a server name and path are provided)
// - Cloud Storage (in which an HTTPS link is provided)
//
// Anything else is custom or Phase II at this time.
// -------------------------------------------------------------------------------------------/
// case 1, 2: local or server
const string executingPs1File = ".\\DeleteVolume.ps1";
var remotePs1File = d.ScriptRepository + "\\DeleteVolume.ps1";
psInstance.AddCommand("Copy-Item");
psInstance.AddParameter("-Path", remotePs1File);
psInstance.AddParameter("-Destination", executingPs1File);
psInstance.AddParameter("-Force");
psInstance.Invoke();
debugStream += psInstance.Streams.Debug.Aggregate(debugStream, (current, debug) => current + debug);
debugStream += psInstance.Streams.Progress.Aggregate(debugStream,
(current, debug) => current + debug);
if (psInstance.Streams.Error.Count > 0)
{
errorStream += psInstance.Streams.Error.Aggregate(errorStream, (current, error) => current + error);
return new NetAppResponse
{
IsSuccess = false,
ErrorOut = errorStream,
ConnectionData = string.Empty,
ConsoleOut = "Could not copy file to execute. Please check connectivity to the server.",
ReturnCode = -1
};
}
// Step 3 - let's remove the volume.
// So normally the only change here from our existing provisioning process is that we need to pull the volume name from the
// connection string. so, let's extract it.
var volumeToDelete = ExtractFromConnectionString(connectionData);
psInstance.AddCommand(executingPs1File);
psInstance.AddParameter("-username", d.AdminUserName);
psInstance.AddParameter("-password", d.AdminPassword);
psInstance.AddParameter("-vserver", d.VServer);
psInstance.AddParameter("-endpoint", d.ClusterMgtEndpoint);
psInstance.AddParameter("-volName", volumeToDelete);
psInstance.Invoke();
debugStream += psInstance.Streams.Debug.Aggregate(debugStream, (current, debug) => current + debug);
debugStream += psInstance.Streams.Progress.Aggregate(debugStream,
(current, debug) => current + debug);
if (psInstance.Streams.Error.Count <= 0)
return new NetAppResponse
{
IsSuccess = true,
ErrorOut = string.Empty,
ConnectionData = string.Empty,
ConsoleOut = debugStream
};
errorStream += psInstance.Streams.Error.Aggregate(errorStream, (current, error) => current + error);
return new NetAppResponse
{
IsSuccess = false,
ErrorOut = errorStream,
ConnectionData = string.Empty,
ConsoleOut = "Could not copy file to execute. Please check connectivity to the server.",
ReturnCode = -1
};
}
}