Solved

Running PowerShell script from VB.Net

Posted on 2011-03-22
4
1,138 Views
Last Modified: 2012-05-11
I have code that calls a PowerShell script to copy 1 user's groups and apply them to another user in Active Directory. I can run the script fine in a PowerShell, but when I try to call it from the code, I get an error that reads:
Cannot convert null to type "System.ConsoleColor" due to invalid enumeration values. Specify one of the following enumeration values and try again...

I have been able to launch scripts to find a user from AD and that works. There is a group (Domain Users) that exists for both users and it throws an error, but continues through the rest of the groups.

I think my problem has something to do with this error, but I don't know how to handle that from the PowerShell side.
0
Comment
Question by:enggarmin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35217131
It may help if you post the code that you are using to run the scripts.
0
 

Author Comment

by:enggarmin
ID: 35217634
The error shows up at the line "commandResults = pipeLine.Invoke"

Public Function modify_account(ByVal templateuser As String, ByVal targetuser As String, ByVal wipe As String) As String
        Dim MyStringBuilder As New StringBuilder()
        Dim results As Collection(Of PSObject)

        Dim myRunspace As Runspace
        Dim rsConfig As RunspaceConfiguration
        Dim snapInException As PSSnapInException = Nothing
        Dim info As PSSnapInInfo
        Dim psCommand As String

        rsConfig = RunspaceConfiguration.Create
        ' import the Quest ActiveRoles snapin for PowerShell
        info = rsConfig.AddPSSnapIn("Quest.ActiveRoles.ADManagement", snapInException)
        myRunspace = RunspaceFactory.CreateRunspace(rsConfig)
        myRunspace.Open()

        ' Create a pipeline to run the command through
        Dim pipeLine As Pipeline
        pipeLine = myRunspace.CreatePipeline
        ' Setup the required powershell command and parameters

        Dim getUser As New Command(LoadScript("c:\modifyuser.ps1"), True, True)

        getUser.Parameters.Add("args[0]", templateuser)
        getUser.Parameters.Add("args[1]", targetuser)
        getUser.Parameters.Add("args[2]", wipe)

    
        ' Add PS command to the pipeline
        pipeLine.Commands.Add(getUser)

        Dim commandResults As Collection(Of PSObject) = Nothing
        ' Run the pipeline command
        commandResults = pipeLine.Invoke
        ' If there is an error show it
        If Not pipeLine.Error Is Nothing Then
            For Each pserror As Object In pipeLine.Error.ReadToEnd
                MyStringBuilder.AppendLine(pserror.ToString())
            Next
        End If
        pipeLine.Dispose()

        If results IsNot Nothing AndAlso results.Count > 0 Then
            For Each ps As PSObject In results
                Dim blah As String
                blah = ps.ToString()
                MyStringBuilder.AppendLine(ps.ToString())
            Next
        End If
        Return MyStringBuilder.ToString
    End Function

Open in new window

0
 

Accepted Solution

by:
enggarmin earned 0 total points
ID: 35393511
I updated the script I was running to ignore errors. The Error my code was generating is now gone.
0
 

Author Closing Comment

by:enggarmin
ID: 35422374
Resolved by changing the PowerShell script I was calling.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

HOW TO: Install and Configure VMware vSphere Hypervisor 6.5 (ESXi 6.5), Step by Step Tutorial with screenshots. From Download, Checking Media, to Completed Installation.
In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
Teach the user how to use vSphere Update Manager to update the VMware Tools and virtual machine hardware version Open vSphere Client: Review manual processes for updating VMware Tools and virtual hardware versions: Create a new baseline group in vSp…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question