Reading Powershell varaibles with Workflow

BenBrazil's picture

Hello,

I am new to Workflow solution and am trying to work out how to read variables from a scripting language in a Workflow solution project. For instance I am connecting to a VMWare Host and reporting on the space left on a Datastore using Powershell. If my variable $AvailableSpace hold this information in the Powershell script, how can I read this into a webform to display to a user?

Are there any examples or turorials relating to scripting and Workflow?

Many thanks,

Ben

jz81's picture

Reading Powershell Variables with Workflow

Hello Ben,

Here's how I would do it (I tested this approach and it works just fine)

* First of all (prerequisites):
- You *of course* need to have Powershell installed on your box
- You also need to have the Powershell SDK installed on your box (as far as I know the SDK is no longer available as a separate download, so you'll need to install the "Windows SDK for Windows Server 2008 and .NET Framework 3.5" (don't worry you won't need all of it - installing 'Developer tools - Windows Development Tools - .NET Development Tools' should do the trick).
- And ... if you want to use any of the VI Toolkit cmdlets (the VmWare stuff) you need to have the VI Toolkit installed (make sure you add the Snapin in the beginning of your Powershell script (add-pssnapin VMWare.VimAutomation.Core).

* Now once all this stuff is taken care of go to your Workflow Project

In your project tab under the tab "Libraries" add the following dll:

C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll

Now go to your model and add a Code (Script) Component and set the following properties for this component:

1. Input parameters:

Name: PSInput (or whatever variable containing the contents of your Powershell script)
Type: Text

2. Result Variable:

Result Variable Name: PSOutput (make sure this one exists)
Result Variable Type: Text

3. Source Code:
Language: C Sharp

Using namespaces (one per line (you might want to reconsider this ...)): System.Collections.ObjectModel
System.Management.Automation
System.Management.Automation.Runspaces

Source Code:

// Source: http://www.codeproject.com/KB/cs/HowToRunPowerShel...

// create Powershell runspace

Runspace runspace = RunspaceFactory.CreateRunspace();

// open it

runspace.Open();

// create a pipeline and feed it the script text

Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(PSInput);

// add an extra command to transform the script
// output objects into nicely formatted strings

// remove this line to get the actual objects
// that the script returns. For example, the script

// "Get-Process" returns a collection
// of System.Diagnostics.Process instances.

pipeline.Commands.Add("Out-String");

// execute the script

Collection<psobject /> results = pipeline.Invoke();

// close the runspace

runspace.Close();

// convert the script result into a single string

StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}

return stringBuilder.ToString();

* Now Connect this component to whatever other components you are using in this model

* Make sure you feed this component your powershell script

* The variable psoutput will contain whatever output your script generated (in your case the $AvailableSpace variable).

I hope this helps (if not, send me a message).

Best regards,
John

BenBrazil's picture

Hi John, Thanks for your

Hi John,

Thanks for your detailed reply. I'll give this a go and let you know how I get on.

Thanks again,

Ben

jz81's picture

Hello Ben, If you're planning

Hello Ben,

If you're planning to use Workflow 7 in the near future, check out the ExecutePowerShellScriptSampleComponent.
I've compiled a pdf with some details about the Workflow 7 components. You can find it here: http://www.symantec.com/connect/downloads/workflow-solution-70-component-documentation

Best regards,
John

Rob Moore's picture

Can't find that component

John,

I have WF7 installed and I can't find that component... can you specify where it is located and if there is something I need to do to make it appear?

Thanks,
rob

stuper's picture

Should be able to search

Should be able to search "executepower" and find it. It's in the Symantec > SMP 7 > Tasks folder in the component toolbox . If you don't see it in there check the libraries tab in the process data to see if Symantec.Components.Generated.Altiris.ASDKTask.dll is in your project. If not, import it.

Rob Moore's picture

Nope, Not there

There is no component by that name. do I have to load a specific library?

rob

reecardo's picture

The ASDK 7 task DLL should be

The ASDK 7 task DLL should be loaded by default in your project. However, you do have to have a NS7 registered in the NS credentials tool to see the NS7 components.