Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Running PowerShell script from VB.Net

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Veeam Backup & Replication has added a new integration – Veeam Backup for Microsoft Office 365.  In this blog, we will discuss how you can benefit from Office 365 email backup with the Veeam’s new product and try to shed some light on the needs and …
This article outlines why you need to choose a backup solution that protects your entire environment – including your VMware ESXi and Microsoft Hyper-V virtualization hosts – not just your virtual machines.
Advanced tutorial on how to run the esxtop command to capture a batch file in csv format in order to export the file and use it for performance analysis. He demonstrates how to download the file using a vSphere web client (or vSphere client) and exp…
This Micro Tutorial walks you through using a remote console to access a server and install ESXi 5.1. This example is showing remote access and installation using a Dell server. The hypervisor is the very first component of your virtual infrastructu…

618 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