none
The PcsvDevice modules do not show up in VB program RRS feed

  • Question

  • Hi,

    When running the PowerShell Get-Command from a Visual Basic program in a PowerShell runspace/pipeline the PcsvDevice modules do not show up. When running the Get-Command from a normal PS box, these commands show up as expected. Is a solution available?

    Sunday, June 9, 2019 5:18 PM

All replies

  • Hello,

    We would need to see the code from your VB.NET project. If this is not VB.NET

    VB6

    http://www.vbforums.com/forumdisplay.php?1-Visual-Basic-6-and-Earlier


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, June 9, 2019 5:51 PM
    Moderator
  • This is all the code:

    Imports System.Collections.ObjectModel
    Imports System.Management.Automation
    Imports System.Management.Automation.Runspaces
    Imports System.Text
    Imports System.IO
    
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'run our script and put the result into our textbox 
            'NOTE: make sure to change the path to the correct location of your script 
            RichTextBox1.Text = RunScript(LoadScript("c:\BMCManager\Test.ps1"))
        End Sub
    
        ' Takes script text as input and runs it, then converts 
        ' the results to a string to return to the user 
        Private Function RunScript(ByVal scriptText As String) As String
    
            ' create Powershell runspace 
            Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
    
            ' open it 
            MyRunSpace.Open()
    
            ' create a pipeline and feed it the script text 
            Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
    
            MyPipeline.Commands.AddScript(scriptText)
    
            ' add an extra command to transform the script output objects into nicely formatted strings 
            ' remove this line to get the actual objects that the script returns. For example, the script 
            ' "Get-Process" returns a collection of System.Diagnostics.Process instances. 
            ''MyPipeline.Commands.Add("Out-String")
    
            ' execute the script 
            Dim results As Collection(Of PSObject) = MyPipeline.Invoke()
    
            ' close the runspace 
            MyRunSpace.Close()
    
            ' convert the script result into a single string 
            Dim MyStringBuilder As New StringBuilder()
    
            ComboBox1.Items.Clear()
            For Each obj As PSObject In results
                MyStringBuilder.AppendLine(obj.ToString())
                ComboBox1.Items.Add(obj.ToString())
            Next
    
            ' return the results of the script that has 
            ' now been converted to text 
            Return MyStringBuilder.ToString()
    
        End Function
    
        ' helper method that takes your script path, loads up the script 
        ' into a variable, and passes the variable to the RunScript method 
        ' that will then execute the contents 
        Private Function LoadScript(ByVal filename As String) As String
    
            Try
    
                ' Create an instance of StreamReader to read from our file. 
                ' The using statement also closes the StreamReader. 
                Dim sr As New StreamReader(filename)
    
                ' use a string builder to get all our lines from the file 
                Dim fileContents As New StringBuilder()
    
                ' string to hold the current line 
                Dim curLine As String = ""
    
                ' loop through our file and read each line into our 
                ' stringbuilder as we go along 
                Do
                    ' read each line and MAKE SURE YOU ADD BACK THE 
                    ' LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF 
                    curLine = sr.ReadLine()
                    fileContents.Append(curLine + vbCrLf)
                Loop Until curLine Is Nothing
    
                ' close our reader now that we are done 
                sr.Close()
    
                ' call RunScript and pass in our file contents 
                ' converted to a string 
                Return fileContents.ToString()
    
            Catch e As Exception
                ' Let the user know what went wrong. 
                Dim errorText As String = "The file could not be read:"
                errorText += e.Message + "\n"
                Return errorText
            End Try
    
        End Function
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    End Class

    Sunday, June 9, 2019 6:20 PM
  • Hi,

    Is your error similar to the link below?

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/033e3bdd-9859-4ff7-aaae-182408935e19/script-cannot-be-loaded-because-running-scripts-is-disabled-on-this-system?forum=csharpgeneral

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 10, 2019 5:52 AM
    Moderator
  • Hi Alex,

    No I don't think it is similar.

      

    I ran this line from VB:
    Process.Start ("powershell", "-noexit -file C:\BMCManager\TestPSCommand.ps1")
    Error message:
    Get-Command : The term 'Get-PcsvDevice>' is not recognized………

    Content file TestPSCommand.ps1:
    c:\windows\system32\WindowsPowerShell\v1.0\PowerShell.exe Get-Command Get-PcsvDevice

    I also ran from VB:

    Shell("cmd.exe /c powershell c:\BMCManager\TestPSCommand.ps1 > c:/temp/out3.txt")
    Result: file out3.txt is empty
    Note, when I leave the last verb "Get-PcsvDevice" out of TestPSCommand.ps1 then I get a list of commands as expected. However the PcsvDevice commands are missing.

    Conclusion: Coming from VB, certain PS commands (e.g. PcsvDevice) are not available. Running directly from a PS window, PcsvDevice commands are available.

    Monday, June 10, 2019 9:51 AM
  • Hi,

    see the following link,it tells us how to run Powershell scripts in VB.Net

    https://stackoverflow.com/questions/11782121/how-to-run-powershell-scripts-in-vb-net

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 12, 2019 5:44 AM
    Moderator
  • Hi Alex,

    I already used the pointer you sent to build my initial solution. It works great, except for the PowerShell PcsvDevice commands which are not available. Other PS commands work fine. So one or another way the PcsvDevice commands are not available in the process (space) started from my VB program. My best guess is that the problem is at the intersection of VB and PS.

    Saturday, June 15, 2019 10:22 AM
  • Hi Alex,

    I already used the pointer you sent to build my initial solution. It works great, except for the PowerShell PcsvDevice commands which are not available. Other PS commands work fine. So one or another way the PcsvDevice commands are not available in the process (space) started from my VB program. My best guess is that the problem is at the intersection of VB and PS.

    Hi,

    Do you resolve the issue?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 17, 2019 7:15 AM
    Moderator
  • No, but I found a workaround (ipmiutil). For now I skip the missing PS commands.

    I found out that different sub versions of PS are used on my server (a VM running W2019) . When using a direct PS command box the PS version is 5.1.17763.1 ($PSVersionTable) Using VB (PowerShell RunSpace) the version is 5.1.17763.134. In the latter (higher version) a number of commands are missing, e.g. get-PCSVDevice (do not show up when running the PowerShell Get-Command)

    In a WinPe .iso with PS the version number is 5.1.18362.1. Also in this version certain commands like get-PCSVDevice are missing. The higher the version number the lower the number of commands. Maybe a lot of changes are taken place because of multi platform developments?

    Btw all versions mentioned above have the same “PSCompatible version” : {1.0, 2.0, 3.0, 4.0}.

    Monday, June 17, 2019 11:56 AM
  • Hi,

    If you have any issue  about PowerShell,ask in the following forum:

    https://social.technet.microsoft.com/Forums/windowsserver/en-US/home?forum=winserverpowershell

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 18, 2019 2:34 AM
    Moderator