?
Solved

Running PowerShell script from VB.Net

Posted on 2011-03-22
4
Medium Priority
?
1,188 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
When rebooting a vCenters 6.0 and try to connect using vSphere Client we get this issue "Invalid URL: The hostname could not parsed." When we get this error we need to do some changes in the vCenter advanced settings to fix the issue.
Teach the user how to convert virtaul disk file formats and how to rename virtual machine files on datastores. Open vSphere Web Client: Review VM disk settings: Migrate VM to new datastore with a thick provisioned (lazy zeroed) disk format: Rename a…
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…
Suggested Courses

764 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