Results 1 to 7 of 7

Thread: Execute PowerShell code in VB.NET

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,373

    Execute PowerShell code in VB.NET

    I am trying to execute PowerShell code to query the registry using System.Management.Automation. The following executes successfully in an elevated PowerShell window and returns the correct key count of 30 on my system:
    Code:
    $Keys = Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tree'
    $Keys.Count
    However, when the same code is executed in VB.NET (as administrator), count is returned as only 1:

    Code:
    Imports System.Collections.ObjectModel
    Imports System.Management.Automation
    
    Dim script As String = "$Keys = Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tree'
    $Keys.Count"
    
    Using powershell As PowerShell = PowerShell.Create()
        powershell.AddScript(script)
        Dim results As Collection(Of PSObject) = powershell.Invoke()
    
        For Each result As PSObject In results
            MessageBox.Show(result.ToString())
        Next
    End Using
    This issue seems to be isolated with this particular registry key as I haven't been able to replicate this behaviour with other HKEY_LOCAL_MACHINE registry keys.

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,366

    Re: Execute PowerShell code in VB.NET

    The difference could be a result of one running as 32bit process and the other as 64bit. Try compiling the .net app as 32bit.

  3. #3
    Member
    Join Date
    Jul 2022
    Posts
    55

    Re: Execute PowerShell code in VB.NET


  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,301

    Re: Execute PowerShell code in VB.NET

    Quote Originally Posted by robertx View Post
    I am trying to execute PowerShell code to query the registry using System.Management.Automation. The following executes successfully in an elevated PowerShell window and returns the correct key count of 30 on my system:
    Code:
    $Keys = Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tree'
    $Keys.Count
    However, when the same code is executed in VB.NET (as administrator), count is returned as only 1:

    Code:
    Imports System.Collections.ObjectModel
    Imports System.Management.Automation
    
    Dim script As String = "$Keys = Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tree'
    $Keys.Count"
    
    Using powershell As PowerShell = PowerShell.Create()
        powershell.AddScript(script)
        Dim results As Collection(Of PSObject) = powershell.Invoke()
    
        For Each result As PSObject In results
            MessageBox.Show(result.ToString())
        Next
    End Using
    This issue seems to be isolated with this particular registry key as I haven't been able to replicate this behaviour with other HKEY_LOCAL_MACHINE registry keys.
    Quick question, is there a reason you are using powershell to do this from you app rather than just having your app query the registry directly?

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,373

    Re: Execute PowerShell code in VB.NET

    Quote Originally Posted by digitalShaman View Post
    The difference could be a result of one running as 32bit process and the other as 64bit. Try compiling the .net app as 32bit.
    This was the issue. However, the .NET application was compiled as 32-bit. After compiling as 64-bit, the correct result is returned. I am wondering whether it is possible to run this code correctly as 64-bit within a 32-bit process.
    Last edited by robertx; Sep 27th, 2023 at 11:15 AM.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,373

    Re: Execute PowerShell code in VB.NET

    Quote Originally Posted by PlausiblyDamp View Post
    Quick question, is there a reason you are using powershell to do this from you app rather than just having your app query the registry directly?
    I was trying to execute a larger and more complex PowerShell script part of which was this registry interaction. The script was failing to execute correctly and I discovered that this was the problematic line of code.

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,882

    Re: Execute PowerShell code in VB.NET

    Quote Originally Posted by robertx View Post
    This was the issue. However, the .NET application was compiled as 32-bit. After compiling as 64-bit, the correct result is returned. I am wondering whether it is possible to run this code correctly as 64-bit within a 32-bit process.
    The following StackOverflow article outlines a solution in C# which should be adaptable to VB.NET with little difficulty:

    https://stackoverflow.com/questions/...it-application

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width