-
Sep 24th, 2023, 12:30 AM
#1
Thread Starter
Frenzied Member
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.
-
Sep 25th, 2023, 01:16 AM
#2
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.
-
Sep 25th, 2023, 12:24 PM
#3
Lively Member
Re: Execute PowerShell code in VB.NET
-
Sep 25th, 2023, 01:33 PM
#4
Re: Execute PowerShell code in VB.NET
 Originally Posted by robertx
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?
-
Sep 27th, 2023, 11:09 AM
#5
Thread Starter
Frenzied Member
Re: Execute PowerShell code in VB.NET
 Originally Posted by digitalShaman
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.
-
Sep 27th, 2023, 11:13 AM
#6
Thread Starter
Frenzied Member
Re: Execute PowerShell code in VB.NET
 Originally Posted by PlausiblyDamp
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.
-
Sep 27th, 2023, 12:11 PM
#7
Re: Execute PowerShell code in VB.NET
 Originally Posted by robertx
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|