Jump to content

Using visual basic to run powershell code or script


Recommended Posts

Posted

My aim is to update my existing visual basic program that is design (in short) to reset a student password on Active Directory however this doesn't update their office 365 email credentials so we have to wait for the system to sync it self which can take a while to do (plus wasn't really great when school is were doing remote lessons recently etc.), so I was asked if there was a way to reset both AD and Office 365 credentials at the same time.

 

Now I've tried myself with google and while I got a line of code for PowerShell and I can get a test VB program to create the line of code (even save it as a .ps1 file etc.), I can not get it to run. I believe the issue is it thinks it needs the MSOnline module for PowerShell but I have all that installed already. I tried simplifying the command for it to "write-host hello (username)' and even start notepad but it doesn't want to work. it does if i run the script manually... Alternatively I has the test program create a batch file/code to call the .ps1 file but again it doesn't want to do it. It looks like it wants to launch something but nothing happens (no hello (username) and no notepad etc.).

 

I've tried various methods but I can't figure out why it won't run the code/script (PS or Bat > PS etc.)

 

my latest code;

   [color=#0000ff]Dim [/color]BATCH_FILE_NAME [color=#0000ff]As String[/color] = [color=#b22222]"network path\test.bat"
[/color]    [color=#0000FF]Dim [/color]Bat_FILE_NAME [color=#0000ff]As String[/color] = [color=#b22222]"mapped drive path\test.bat"
[/color]    [color=#0000FF]Dim [/color]PS_FILE_NAME As String =[color=#b22222] "network path\test.ps1"
[/color]    [color=#0000FF]Dim [/color]BATCH_Code [color=#0000ff]As String[/color] = [color=#b22222]"Powershell.exe -ExecutionPolicy Bypass -noexit  -file " [/color]& [color=#b22222]""""[/color] & [color=#b22222]"test.ps1"[/color] & [color=#b22222]""""
[/color]    [color=#0000FF]Dim [/color]PS_Code [color=#0000ff]As String
[/color]    [color=#0000FF]Dim [/color]Name [color=#0000ff]As String
[/color]
   [color=#0000ff]Private Sub[/color] Button1_Click(sender [color=#0000ff]As Object[/color], e [color=#0000ff]As [/color][color=#40e0d0]EventArgs[/color]) [color=#0000ff]Handles[/color] Button1.Click
       PS_Code = [color=#b22222]"Write-Host "[/color] & [color=#b22222]""""[/color] & cboNames.SelectedItem &[color=#b22222] """"[/color] & [color=#b22222]" | pause" [/color]& vbNewLine & [color=#b22222]"notepad.exe"
[/color]        [color=#808080]System[/color].IO.[color=#40e0d0]File[/color].WriteAllText(PS_FILE_NAME, PS_Code)
       [color=#808080]System[/color].IO.[color=#40e0d0]File[/color].WriteAllText(BATCH_FILE_NAME, BATCH_Code)
   [color=#0000ff]End Sub

   Private Sub[/color] Form1_Load(sender [color=#0000ff]As Object[/color], e [color=#0000ff]As [/color][color=#40e0d0]EventArgs[/color]) [color=#0000ff]Handles MyBase[/color].Load
       [color=#0000ff]With[/color] cboNames
           .Items.Add([color=#b22222]"dpenfold"[/color])
       [color=#0000ff]End With
   End Sub[/color]


   [color=#0000ff]Private Sub[/color] Button2_Click(sender [color=#0000ff]As Object[/color], e [color=#0000ff]As [/color]EventArgs) [color=#0000ff]Handles [/color]Button2.Click
       [color=#008000]'System.Diagnostics.Process.Start("mapped drive path\test.bat")
[/color]        Shell([color=#b22222]"mapped drive path\test.bat"[/color])
   [color=#0000ff]End Sub
[/color]

Posted

Ignoring the actual question for a minute, but "however this doesn't update their office 365 email credentials so we have to wait for the system to sync it self which can take a while to do" the password sync from AADC runs separate to the normal one every 2 minutes, so as long as when you reset it you aren't ticking the "must reset at login" etc it shouldn't be causing many problems like that as within 2 minutes it's reset and thus renders this unnecessary really unless you need it instant?

 

Depending on how long your script is though I wouldn't even bother using external files, you could just do it within the code

 

       Dim script As String = "echo 'HelloWorld'"
       Dim objProcess As New System.Diagnostics.Process()
       objProcess.StartInfo.FileName = "powershell.exe"
       objProcess.StartInfo.Arguments = script
       objProcess.Start()

 

etc which will just fire it off

 

Or if you wanted to retrieve info, can redirect output to label/text boxes:

       Dim script As String = "echo HelloWorld"

       Dim objProcess As New System.Diagnostics.Process()

       objProcess.StartInfo.FileName = "powershell.exe"
       objProcess.StartInfo.Arguments = script
       objProcess.StartInfo.RedirectStandardOutput = True
       objProcess.StartInfo.RedirectStandardError = True
       objProcess.StartInfo.UseShellExecute = False
       objProcess.StartInfo.CreateNoWindow = True
       objProcess.Start()

       LabelOutput.Text = objProcess.StandardOutput.ReadToEnd()
       LabelError.Text = objProcess.StandardError.ReadToEnd()

 

Steve

  • Thanks 1
Posted (edited)
Ignoring the actual question for a minute, but "however this doesn't update their office 365 email credentials so we have to wait for the system to sync it self which can take a while to do" the password sync from AADC runs separate to the normal one every 2 minutes, so as long as when you reset it you aren't ticking the "must reset at login" etc it shouldn't be causing many problems like that as within 2 minutes it's reset and thus renders this unnecessary really unless you need it instant?

 

Depending on how long your script is though I wouldn't even bother using external files, you could just do it within the code

 

       Dim script As String = "echo 'HelloWorld'"
       Dim objProcess As New System.Diagnostics.Process()
       objProcess.StartInfo.FileName = "powershell.exe"
       objProcess.StartInfo.Arguments = script
       objProcess.Start()

 

etc which will just fire it off

 

Or if you wanted to retrieve info, can redirect output to label/text boxes:

       Dim script As String = "echo HelloWorld"

       Dim objProcess As New System.Diagnostics.Process()

       objProcess.StartInfo.FileName = "powershell.exe"
       objProcess.StartInfo.Arguments = script
       objProcess.StartInfo.RedirectStandardOutput = True
       objProcess.StartInfo.RedirectStandardError = True
       objProcess.StartInfo.UseShellExecute = False
       objProcess.StartInfo.CreateNoWindow = True
       objProcess.Start()

       LabelOutput.Text = objProcess.StandardOutput.ReadToEnd()
       LabelError.Text = objProcess.StandardError.ReadToEnd()

 

Steve

 

The simple doe (echo etc.) works but trying to get it to call or use the line of code I need it complains;

[color=#0000ff]Connect-MsolService[/color] -Credential (New-Object -TypeName [color=#800080]System.Management.Automation.PSCredential[/color] -ArgumentList [color=#a52a2a]"my-email"[/color], ([color=#0000ff]Get-Content[/color] -Path [color=#a52a2a]"C:\dpenfold.txt"[/color] | [color=#0000ff]ConvertTo-SecureString[/color]))
[color=#0000ff]Set-MsolUserPassword[/color] -UserPrincipalName [color=#800080]student-email[/color] -NewPassword [color=#800080]p/w[/color] -ForceChangePassword [color=#a52a2a]$False[/color]

 

The system don't sync as quickly to office 365 and it takes a while for the email/password to sync with AD, so i need both passwords to be reset at the same time, thought it will be come more tricky if they student changes their password once they are logged in

to explain further the program I originally made calls in info from AD based on what the user selects;

Staff/Tutor selects the year from dropdown menu, then the program collates all the names from AD into another dropdown menu for the staff/tutor to select, then it shows in label the select students' info(forename and surname, email address in full and their username) and generates buttons that will open their homedrive (based on homedrive path in AD) and resets user password to the original default (it also messages the staff/tutor that the student will need to make a new password when they log in).

 

If you like I can send the program your way it has been quite useful but I see the homedrive will be replaced down the line due to SharePoint (which I'm fine with tbh).

Edited by DPenfold
Posted

I'm still not sure what you mean in regards to why this is needed if you're using the sync tools provided by MS

 

Do you have AADC setup? If so it's a 2 minute sync cycle with passwords (not the normal 30+ mins delta), so you don't want to be manually forcing passwords to change outside of the sync, as it'll just get overwritten by the on-site password/change forcing etc

 

Obviously this can't change it instantly if you're expecting a user to reset and login and access emails within 2 minutes, but you could sort that by just not forcing the password to change at logon and generating a default password etc until they change it

 

Regarding the code you said above, that's not a full script, if you're doing this via a script you'll need to use the entire Import-Module, Credential Provider etc so it runs automated without any requests

 

Steve

  • Thanks 1
Posted

spoke to colleague he isn't sure how our syncs are set up, this is where my technician skills get weak on the network side of things sadly I'll try and find out more. If you know where I can find out (as I do have access to everything) then I can go look/check

 

The program will force a default password we use on new accounts or when we manually reset passwords (the program does the same specifying a password), the user themselves would need to update their password when they have logged in

Posted

Update: There was an issue with our system where the passwords weren't syncing (ticket raised by a staff member who changed their password etc.) this has no been fixed by Microsoft so I don't need to update my program to run the PowerShell Script/Code (thank god!) as the system will sync about every 2 minutes or so.

 

Thanks to Steve21 for their input!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...