Solved

Running PowerShell script from VB.Net

Posted on 2011-03-22
4
1,067 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
  • 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When we have a dead host and we lose all connections to the ESXi, and we need to find a way to move all VMs from that dead ESXi host.
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.
Teach the user how to rename, unmount, delete and upgrade VMFS datastores. Open vSphere Web Client: Rename VMFS and NFS datastores: Upgrade VMFS-3 volume to VMFS-5: Unmount VMFS datastore: Delete a VMFS datastore:
This Micro Tutorial steps you through the configuration steps to configure your ESXi host Management Network settings and test the management network, ensure the host is recognized by the DNS Server, configure a new password, and the troubleshooting…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now