PowerShell commands and VB.Net Application

~OSD~ 2,151 Reputation points
2021-03-19T13:21:15.12+00:00

Hi,

I have following tasks and currently I am able to perform required tasks using PowerShell as:

**

  • PowerShell >

** Rename-LocalUser -Name LocalAdmin -NewName LAdmin -ErrorAction SilentlyContinue

Following code I am using but desired action isn't performed by the code, no errors either.

Dim p As Process = New Process()
        p.StartInfo.FileName = "PowerShell.exe"
        p.StartInfo.Arguments = "Rename-LocalUser -Name LocalAdmin -NewName LAdmin -ErrorAction SilentlyContinue -windowstyle hidden "
        p.StartInfo.UseShellExecute = False
        p.StartInfo.CreateNoWindow = True
        p.StartInfo.RedirectStandardError = True
        p.Start()
        p.WaitForExit()
        Dim sStdErr_psRename As String = p.StandardError.ReadToEnd()
        Console.WriteLine("Exit code : {0}", p.ExitCode)
        Console.WriteLine("StdErr : {0}", sStdErr_psRename)

Another task that I am currently performing using CMD is to change the password from command line as:
net user %UserName% NewPassword

But same as above, following code didn't performed desired task:

Dim cUsrPsw As Process = New Process()
        cUsrPsw.StartInfo.FileName = "net.exe"
        cUsrPsw.StartInfo.Arguments = "user %UserName% NewPassword"
        cUsrPsw.StartInfo.UseShellExecute = False
        cUsrPsw.StartInfo.CreateNoWindow = True
        cUsrPsw.StartInfo.RedirectStandardError = True
        cUsrPsw.Start()
        cUsrPsw.WaitForExit()
        Dim sStdErr_cUsrPsw As String = cUsrPsw.StandardError.ReadToEnd()
        Console.WriteLine("Exit code : {0}", cUsrPsw.ExitCode)
        Console.WriteLine("StdErr : {0}", sStdErr_cUsrPsw)
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,666 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 83,201 Reputation points
    2021-03-19T15:08:07.007+00:00

    This test with PowerShell by adapting your code works for me (Windows 10) as Admin (Manifest with level="requireAdministrator")

    Dim bWow64 As Boolean = False
    IsWow64Process(System.Diagnostics.Process.GetCurrentProcess().Handle, bWow64)
    If (bWow64) Then
        Dim OldValue As IntPtr = IntPtr.Zero
        Dim bReturn As Boolean = Wow64DisableWow64FsRedirection(OldValue)
    End If
    Dim p As Process = New Process()
    p.StartInfo.FileName = "PowerShell.exe"
    p.StartInfo.Arguments = "Rename-LocalUser -Name LocalAdmin -NewName LAdmin"
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    p.StartInfo.UseShellExecute = False
    p.StartInfo.CreateNoWindow = True
    p.StartInfo.RedirectStandardError = True
    p.Start()
    p.WaitForExit()
    Dim sStdErr_psRename As String = p.StandardError.ReadToEnd()
    Console.WriteLine("Exit code : {0}", p.ExitCode)
    Console.WriteLine("StdErr : {0}", sStdErr_psRename)
    

    Declarations :

    <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
    Public Shared Function IsWow64Process(ByVal hProcess As IntPtr, <Out> ByRef Wow64Process As Boolean) As Boolean
    End Function
    
    <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
    Public Shared Function Wow64DisableWow64FsRedirection(<Out> ByRef OldValue As IntPtr) As Boolean
    End Function
    

2 additional answers

Sort by: Most helpful
  1. Castorix31 83,201 Reputation points
    2021-03-21T08:09:18.75+00:00

    Otherwise, you can use one of the other methods like Net APIs.
    Test on Windows 10 1909 as Admin =>

    Dim ui As USER_INFO_0 = New USER_INFO_0()
    Dim sOldUser As String = "toto", sNewUser As String = "titi"
    ui.usri0_name = sNewUser
    Dim pUI As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(USER_INFO_0)))
    Marshal.StructureToPtr(ui, pUI, False)
    Dim nError As UInteger = 0
    Dim nReturn = NetUserSetInfo(Nothing, sOldUser, 0, pUI, nError)
    If (nReturn <> 0) Then
        Dim sErrorMessage As String = New System.ComponentModel.Win32Exception(nReturn).Message
        System.Windows.Forms.MessageBox.Show("Error : " & nReturn.ToString() & Environment.NewLine & sErrorMessage & Environment.NewLine & "User name : " & sOldUser, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Else
        System.Windows.Forms.MessageBox.Show("User : " & sOldUser & Environment.NewLine & "renamed into : " & sNewUser, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
    Marshal.FreeHGlobal(pUI)
    

    Declarations :

    <DllImport("NetApi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
    Public Shared Function NetUserSetInfo(servername As String, username As String, level As UInteger, buf As IntPtr, ByRef parm_err As UInteger) As Integer
    End Function
    
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
    Public Structure USER_INFO_0
        Public usri0_name As String
    End Structure
    
    1 person found this answer helpful.

  2. ~OSD~ 2,151 Reputation points
    2021-03-22T19:57:50.467+00:00

    Thanks for providing working example.

    0 comments No comments