-
Oct 26th, 2022, 02:18 PM
#1
Thread Starter
Addicted Member
Improve code with better syntax
Hello, can someone help me improve this query syntax to be better input without so much """ i use
Code:
Using batFile As New StreamWriter(batFileName)
'User1 Create
batFile.WriteLine($"net user " & TxtAutoUser.Text & " " & TxtAutoPass.Text & " /ADD /FULLNAME:" & """" & TxtAutoFullName.Text & """" & " /COMMENT:""Created User by System Tool"" /PASSWORDCHG:NO /ACTIVE:Yes /EXPIRES:Never")
batFile.WriteLine($"wmic path Win32_UserAccount WHERE Name=" & """" & TxtAutoUser.Text & """" & " set PasswordExpires=False")
batFile.WriteLine($"net localgroup Administrators " & TxtAutoUser.Text & " /ADD")
'User2 Create
batFile.WriteLine($"net user " & TxtTabletUser.Text & " " & TxtTabletPassword.Text & " /ADD /FULLNAME:" & """" & TxtTabletFullName.Text & """" & " /COMMENT:""Created User by System Tool"" /PASSWORDCHG:NO /ACTIVE:Yes /EXPIRES:Never")
batFile.WriteLine($"wmic path Win32_UserAccount WHERE Name=" & """" & TxtTabletUser.Text & """" & " set PasswordExpires=False")
batFile.WriteLine($"net localgroup Administrators " & TxtTabletUser.Text & " /ADD")
'End Users
batFile.WriteLine($"reg add " & """HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon""" & " /v AutoAdminLogon /t REG_SZ /d " & """1""" & " /f")
batFile.WriteLine($"reg add " & """HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon""" & " /v DefaultDomainName /t REG_SZ /d %USERDOMAIN% /f")
batFile.WriteLine($"reg add " & """HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon""" & " /v DefaultUsername /t REG_SZ /d " & TxtAutoUser.Text & " /f")
batFile.WriteLine($"reg add " & """HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon""" & " /v DefaultPassword /t REG_SZ /d " & TxtAutoPass.Text & " /f")
batFile.WriteLine($"icacls " & """" & FileSavePath & "TestFolder" & """" & " /grant " & TxtAutoUser.Text & ":(OI)(CI)F /T " & "/grant " & TxtTabletUser.Text & ":(OI)(CI)F /T")
batFile.WriteLine($"net share " & """ShareFolder""" & "=" & """" & FileSavePath & "TestFolder" & """" & " /Remark:""Shared by System Tool"" /GRANT:Everyone,Read /GRANT:" & TxtAutoUser.Text & ",Full " & "/GRANT:" & TxtTabletUser.Text & ",Full " & "/UNLIMITED")
End Using
-
Oct 26th, 2022, 03:06 PM
#2
Re: Improve code with better syntax
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 26th, 2022, 03:24 PM
#3
Re: Improve code with better syntax
Also, you don't need to do this:
Code:
WriteLine($"reg add " & """HKLM\SOFTWARE\Microso
When this will suffice:
Code:
WriteLine($"reg add ""HKLM\SOFTWARE\Microso
-tg
-
Oct 26th, 2022, 03:33 PM
#4
Thread Starter
Addicted Member
Re: Improve code with better syntax
thank you, forgot about {TxtField.Text} option
-
Oct 26th, 2022, 03:33 PM
#5
Re: Improve code with better syntax
Also, strings accept line breaks now. You don't need all those WriteLine calls:
Code:
Dim message = $"net user {TxtAutoUser.Text} {TxtAutoPass.Text} /ADD /FULLNAME: "" {TxtAutoFullName.Text} "" /COMMENT:""Created User by System Tool"" /PASSWORDCHG:NO /ACTIVE:Yes /EXPIRES:Never
wmic path Win32_UserAccount WHERE Name="" {TxtAutoUser.Text} "" set PasswordExpires=False
etc..."
batFile.WriteLine(message)
Fiddle: https://dotnetfiddle.net/Wt0q4j
-
Oct 26th, 2022, 03:35 PM
#6
Thread Starter
Addicted Member
Re: Improve code with better syntax
Originally Posted by techgnome
Also, you don't need to do this:
Code:
WriteLine($"reg add " & """HKLM\SOFTWARE\Microso
When this will suffice:
Code:
WriteLine($"reg add ""HKLM\SOFTWARE\Microso
-tg
the problem is that cmd needs to have the "Text" for the reg command that's why i input like that
-
Oct 26th, 2022, 03:36 PM
#7
Re: Improve code with better syntax
I have also moved this to the Code It Better forum.
-
Oct 26th, 2022, 03:55 PM
#8
Thread Starter
Addicted Member
Re: Improve code with better syntax
i got a question regarding my last 2 rows codes
Code:
batFile.WriteLine($"icarls ""{FileSavePath}TestFolder"" /grant {TxtAutoUser.Text}:(OI)(CI)F /T /grant {TxtTabletUser.Text}:(OI)(CI)F /T")
batFile.WriteLine($"net share SharedFolder=""{FileSavePath}TestFolder"" /Remark:""Shared by System Tool"" /GRANT:Everyone,Read /GRANT:{TxtAutoUser.Text},Full /GRANT:{TxtTabletUser.Text},Full /UNLIMITED")
so i have issue with this i think because it's already shared and not allow me to add users in advanced sharing option
-
Nov 23rd, 2024, 07:59 AM
#9
Re: Improve code with better syntax
I know this is an old thread. I just had some time on my hands:
Code:
' Function to write the user creation commands
Private Sub CreateUserCommands(ByVal batFile As StreamWriter, ByVal userName As String, ByVal userPassword As String, ByVal fullName As String)
Dim userCommands As String = $"net user {userName} {userPassword} /ADD /FULLNAME:""{fullName}"" /COMMENT:""Created User by System Tool"" /PASSWORDCHG:NO /ACTIVE:Yes /EXPIRES:Never"
userCommands &= $" wmic path Win32_UserAccount WHERE Name=""{userName}"" set PasswordExpires=False"
userCommands &= $" net localgroup Administrators {userName} /ADD"
batFile.WriteLine(userCommands)
End Sub
' Function to add registry entries for auto login
Private Sub AddAutoLoginRegistry(ByVal batFile As StreamWriter, ByVal userName As String, ByVal userPassword As String)
' Added line breaks to make the command more readable and ensure proper formatting
Dim registryCommands As String = $"reg add ""HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v AutoAdminLogon /t REG_SZ /d ""1"" /f"
registryCommands &= vbCrLf & $"reg add ""HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v DefaultDomainName /t REG_SZ /d %USERDOMAIN% /f"
registryCommands &= vbCrLf & $"reg add ""HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v DefaultUsername /t REG_SZ /d {userName} /f"
registryCommands &= vbCrLf & $"reg add ""HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v DefaultPassword /t REG_SZ /d {userPassword} /f"
batFile.WriteLine(registryCommands)
End Sub
' Function to handle folder permissions and sharing
Private Sub ConfigureFolderPermissionsAndShare(ByVal batFile As StreamWriter, ByVal fileSavePath As String, ByVal autoUser As String, ByVal tabletUser As String)
' Delete any existing share with the same name to avoid conflict
batFile.WriteLine("net share SharedFolder /delete")
' The rest of the commands for setting permissions and sharing
Dim permissionsAndShareCommands As String = $"icacls ""{fileSavePath}\TestFolder"" /grant {autoUser}:(OI)(CI)F /T /grant {tabletUser}:(OI)(CI)F /T"
permissionsAndShareCommands &= vbCrLf & $"net share SharedFolder=""{fileSavePath}\TestFolder"" /Remark:""Shared by System Tool"" /GRANT:Everyone,Read /GRANT:{autoUser},Full /GRANT:{tabletUser},Full /UNLIMITED"
batFile.WriteLine(permissionsAndShareCommands)
End Sub
' Main code to create the batch file
Sub CreateBatchFile()
Using batFile As New StreamWriter(batFileName)
' User1 Creation
CreateUserCommands(batFile, TxtAutoUser.Text, TxtAutoPass.Text, TxtAutoFullName.Text)
' User2 Creation
CreateUserCommands(batFile, TxtTabletUser.Text, TxtTabletPassword.Text, TxtTabletFullName.Text)
' Auto-login registry entries
AddAutoLoginRegistry(batFile, TxtAutoUser.Text, TxtAutoPass.Text)
' Folder permissions and sharing
ConfigureFolderPermissionsAndShare(batFile, FileSavePath, TxtAutoUser.Text, TxtTabletUser.Text)
End Using
End Sub
Last edited by Peter Porter; Nov 23rd, 2024 at 01:22 PM.
Tags for this Thread
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
|