I am trying to add a user to windows 10 using a python script that uses powershell to add the user.
Here is what I have so far:
main.py
import subprocess as sub
import os
username = testUser
passwds = passwordTest
command = """
$nusnm = """ + '"{}"'.format(username) + """
$nuspss = ConvertTo-SecureString """ + '"{}"'.format(passwds) + """ -AsPlainText -Force
New-LocalUser -Name $nusnm -Password $nuspss
Add-LocalGroupMember -Group "Administrators" -Member $nusnm
Get-LocalUser
"""
print(command)
exec = sub.Popen(["powershell","& {" + command + "}"])
Also if it helps, this is the Powershell code that I am basing it off of which does work:
$nusnm = Read-Host -Prompt 'What would you like the name of the user to be? '
$nuspss = Read-Host -Prompt -AsSecureString 'Please input a new password for the user '
New-LocalUser $nusnm -Password $nuspss -Confirm
Add-LocalGroupMember -Group "Administrators" -Member $nusnm
Get-LocalUser
When I execute this python code, I do not get any errors, but I also do not get an added user named testUser. What am I doing wrong?
Any help is greatly appreciated
Found out that I had just a bunch of syntax errors. I feel really dumb now. Here is the correct code on how to add a user to windows 10 through python.
main.py
import subprocess as sub
import os
username = testUser
passwds = passwordTest
command = """
$nusnm = """ + '"{}"'.format(username) + """
$nuspss = ConvertTo-SecureString """ + '"{}"'.format(passwds) + """ -AsPlainText -Force
New-LocalUser -Name $nusnm -Password $nuspss
Add-LocalGroupMember -Group "Administrators" -Member $nusnm
Get-LocalUser
"""
print(command)
exec = sub.Popen(["powershell","& {" + command + "}"])