none
Call command-line processes and write result to CSV file RRS feed

  • Question

  • Hi,

    I have two lines of command line script that I want to call from my Windows Form app.  How do I call these scripts from my app and then write the result to a CSV file?  The command line scripts are:

    Import-Module ActiveDirectory
    Get-ADComputer -Filter {enabled -eq $true} -properties *|select Name, DNSHostName, OperatingSystem, LastLogonDate

    The above generates a list of computers connected to our domain and the last logon date for each.  I'd like to write this result to a CSV file so I can use it in my app.

    Appreciate any response.


    Marilyn Gambone

    Friday, November 22, 2019 3:18 PM

Answers

  • Installed System.Management.Automation using Nuget.  I can't load it though.  It seems like it's looking for .NET 4.2 version but I'm running .NET 4.6.1 version.

    I added the Reference directly  :

     C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll

    (I tested with .NET 4.7.1)

    • Marked as answer by deskcheck1 Monday, November 25, 2019 3:35 PM
    Saturday, November 23, 2019 8:36 AM

All replies

  • Use the Process Class. Use the Process.StandardOutput Property and related properties. See the samples in the documentation. There are many other samples in many other places. Do you have specific questions?


    Sam Hobbs
    SimpleSamples.Info

    Friday, November 22, 2019 4:25 PM
  • To execute PowerShell scripts, use System.Management.Automation :

    For example (with another script sample, I cannot execute your script on my OS):

            Dim powershell As PowerShell = PowerShell.Create()
            powershell.AddScript("get-wmiobject Win32_Process | Select Name, CommandLine")
            Dim results As Collection(Of PSObject) = powershell.Invoke()
            For Each obj As PSObject In results
                Console.WriteLine("Name : {0}", obj.Members("Name").Value.ToString())
                If obj.Members("CommandLine").Value IsNot Nothing Then
                    Console.WriteLine("CommandLine : {0}", obj.Members("CommandLine").Value.ToString())
                End If
            Next

    Friday, November 22, 2019 7:05 PM
  • Installed System.Management.Automation using Nuget.  I can't load it though.  It seems like it's looking for .NET 4.2 version but I'm running .NET 4.6.1 version.

    Marilyn Gambone

    Friday, November 22, 2019 9:46 PM
  • Hi,

    I think I'll just avoid Powershell for now.  Can't get the Reference Assemblies to work.  

    I just would like to know why when I run a command from Command Prompt to execute an executable file for my Windows Form application, that the Windows Form doesn't launch.  Yet, the Resource Monitor shows that the executable is actually running.

    Is it not possible to launch a Windows Form executable from the Command Prompt?


    Marilyn Gambone

    Friday, November 22, 2019 10:10 PM
  • I just would like to know why when I run a command from Command Prompt to execute an executable file for my Windows Form application, that the Windows Form doesn't launch. Yet, the Resource Monitor shows that the executable is actually running.

    That is not your original question. When you ask that question in a separate thread, please provide more information.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, November 23, 2019 6:32 AM

  • Is it not possible to launch a Windows Form executable from the Command Prompt?

    The general answer is "Yes, of course it is." But more info about your application, 
    the environment, etc. would be needed before any definitive answer could be given.

    You should be able to answer that question yourself. Create a very simple VB
    WinForms program that does nothing but give a display when a button is pressed.
    You should be able to execute it from the IDE, from Windows Explorer, or from
    a command prompt with the same results in all cases.

    - Wayne

    Saturday, November 23, 2019 7:11 AM
  • Installed System.Management.Automation using Nuget.  I can't load it though.  It seems like it's looking for .NET 4.2 version but I'm running .NET 4.6.1 version.

    I added the Reference directly  :

     C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll

    (I tested with .NET 4.7.1)

    • Marked as answer by deskcheck1 Monday, November 25, 2019 3:35 PM
    Saturday, November 23, 2019 8:36 AM
  • Hi Castorix31,

    In my dev machine, the namespace is stored where you said it would be.  C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll

    When I deployed the app to our production server running Windows Server 2012 R2, it automatically stored it in the GACC:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll

    Thanks.  I'm able to run the script as follows:

            Dim myscript As String = "Get-ADComputer -Filter {enabled -eq $true} -properties *|select Name, DNSHostName, OperatingSystem, LastLogonDate | export-csv c:spider.csv"
            Dim myrunspace As Runspace = RunspaceFactory.CreateRunspace()
            myrunspace.Open()
    
            Dim mypipeline As Pipeline = myrunspace.CreatePipeline()
            mypipeline.Commands.AddScript(myscript)
    
            'Execute the script
            Dim results As Collection(Of PSObject) = mypipeline.Invoke()
    
            'Close the runspace
            myrunspace.Close()

    The above creates a CSV file where I want it stored.

    Thanks for the help.

    I'd like to give credit to above script from here where I got it.


    Marilyn Gambone





    • Edited by deskcheck1 Monday, November 25, 2019 3:46 PM
    Monday, November 25, 2019 3:39 PM