Solved

Running PowerShell script from VB.Net

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If we need to check who deleted a Virtual Machine from our vCenter. Looking this task in logs can be painful and spend lot of time, so the best way to check this is in the vCenter DB. Just connect to vCenter DB(default DB should be VCDB and using…
HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
Teach the user how to install vSphere Update Manager  Console to Windows system:  Install vSphere Update Manager: Configure vSphere Update Manager plug-in in vSphere Client: Verify vSphere Update Manager settings in vSphere Client:
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…

947 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

24 Experts available now in Live!

Get 1:1 Help Now