Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

How to run New-LocalUser in Python with subproces

$
0
0

I'm working on a final project for a scripting class at my community college. Recently I used Powershell to add users, give them a password, a description and assign them to a group that was created. Many of these things are assigned to variables within a while loop. One of the key cmdlets was New-LocalUser and the options that followed that as I applied the variables from user input there. We didn't cover subprocesses this semester so I'm at a loss. I've see subprocess.call and using the file path to powershell.exe and I've seen subprocess.run with the comdlet but I'm not having success. The only reason my group is being created is I used an online code converter and it changed it into a try/except setup I'd like to stick as true to my code as I can.

I've tried playing with the subprocess.run and I get the code to run w/o errors but the users aren't created when I go back to Powershell and run the Get-LocalUser cmdlet. I've also had errors related to security with passwords and the echo in terminal but I can get around that with some admin usage.

I'll attach my ps1 code that I'm trying to convert here for some clarity.

echo "Hello, I am C-3PO. I understand you'd like to add some new users to your system today. I'll be happy to assist you."#This echos out a greeting and let's the user know what we're doing today.$Count = Read-Host "How many users are we creating today?"#I'm creating a variable called Count that I will use later to control a loop.$Count = [int]$Count#I'm casting the type of Count to be an integer for simplicity.   $Total = 0#This is another variable to control my loop, I'll increase it with each loop.$Group = Read-Host "We want to be sure all of these new users can find each other. What would you like to call their group?" #As we're putting all these users into a group I asked the user what we should call it.New-LocalGroup -Name $Group -ErrorAction SilentlyContinue#I've created a group with the name provided by the user, if that name already exists they won't see an errorwhile ($Total -ne $Count)#This is the start of my while loop{    $FirstName = Read-Host "What is the first name of the user you'd like to create?"    $MiddleName = Read-Host "What is thier middle name, if they don't have one kindly press enter."    $LastName = Read-Host "Also, may I know thier last name?"    #I've taken steps to get the new users full name as it will be eaiser to use in the subsequent sections.    $Username = ($FirstName.Substring(0,1) + $LastName)    #I've created a username variable based on the prior inputs    $FullName = ($FirstName +" " + $MiddleName +" " + $LastName)    #I've combined the answers above to get their full name    $Password = Read-Host -AsSecureString "What password would you like to give " $FullName "?"    #I've assigned user input to the variable Password    $Description = Read-Host "Would you like to tell me a little about " $FullName "?" " If not simply press enter."    New-LocalUser -Name $Username -Password $Password -FullName $FullName -Description $Description    #I've created a new user with the informaiton the current user has provided    Add-LocalGroupMember -Group $Group -Member $Username    #I've added the most recent user to the group that was created before the while loop.    $Total = $Total+1    #I've moved my Total up by 1 so that this loop won't run forever.}echo "`n It's been a pleasure in helping you add these $Count users today.I do hope you'll inform Master James of my performance."#I've added a farewell to include my name, I did this in place of naming the group myself.

Here's the, VERY ROUGH, Python code as I have it right now; I know some commented lines will be removed. We haven't covered the try/except stuff and that's from a converter and I refuse to use things I don't understand.

import getpass, subprocess#def creat_local_user(Name, Password, FullName, Description):print("""Hello, I am C-3PO. I understant you'd like to add some new users to your system todayI'll be happy to assist you with this task.""")Count = int(input("How many users are we creating today?"))Total = 0GroupName = input("""We want to be sure all of the new users can find each other; comradery is so important you know.Let’s create a group for them. What would you like the group to be called?""")#try:subprocess.run(["net", "localgroup", GroupName, "/add"], check=True)#except subprocess.CalledProcessError as e:#    print(f"Error creating local group '{GroupName}': {e}")total = 0#count = int(input("How many users would you like to create? "))while total != Count:    first_name = input("What is the first name of the user you'd like to create? ")    middle_name = input("What is their middle name, if they don't have one kindly press enter. ")    last_name = input("Also, may I know their last name? ")    username = (first_name[0] + last_name).lower()    full_name = first_name +" " + middle_name +" " + last_name    password = getpass.getpass(f"What password would you like to give {full_name}? ")    description = input(f"Would you like to tell me a little about {full_name}? If not simply press enter. ")    group = GroupName    subprocess.run("net", "localuser", username, "/add",)# "-Name $username", "-Password $password", "-FullName $full_name", "-Description $description"#    subprocess.call("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe New-LocalUser", shell=True)    total += 1print("\nIt's been a pleasure in helping you add these {count} users today.")print("I do hope you'll inform Master James of my performance.")

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>