Reputation: 11
I am very noob with the powershell and everything similar to be honest.
I am trying to achieve a simple batch file to execute and make unlocking user accounts simplified.
Currently I have managed to create the below based on googling, this would simply unlock a user account called "USERNAME".
powershell -Command "& { import-module activedirectory; unlock-adaccount USERNAME }"
What I am trying to achieve is setting that "USERNAME" part as a variable and creating a prompt when executing the batch file to ask for a username in order to be able to unlock any account typed.
I have tried searching related posts but can't seem to find a way to put it together myself. I would appreciate any help that can be offered!
Thank you.
Upvotes: 1
Views: 1044
Reputation: 808
If you have username as a parameter for your command, I think Show-Command could be what you are looking for here.
powershell -Command "& { import-module activedirectory; Show-Command unlock-adaccount }"
It will pop up a little gui and allow your admins to enter in a username or any other params that you define in your commands.
Upvotes: 1
Reputation: 1791
Look into writing re-usable tools. Simple functions and scripts.
Calling various commands from PowerShell.exe will only work for the specific scenario you write them for. Functions and scripts can be used anywhere they make sense. Write something once, use it in many places. It saves a huge amount of time, and will help you learn and improve your PowerShell-ing.
Here's a quick example! Save this code as C:\Unlock-ADAccountSimple.ps1
param (
$Username = (Read-Host "Enter Username")
)
Import-Module ActiveDirectory
Unlock-ADAccount $Username
Now you can use this in your scenario. This would prompt for a username:
PowerShell.exe -File C:\Unlock-ADAccountSimple.ps1
You could also run it for a specific user:
PowerShell.exe -File C:\Unlock-ADAccountSimple.ps1 -Username jdoe
You could use it in scripts you write
#Inside a script, you could run it like this, with various logic before or after:
& "C:\Unlock-ADAccountSimple.ps1" -Username Someone
Down the line, you can write more interesting and helpful functions, and combine them into larger solutions. But you need to start somewhere, and building small re-usable tools (functions/scripts) will start you down the path!
Good luck!
Upvotes: 0
Reputation: 201692
Use the Read-Host
command e.g.:
PowerShell -Command "& {$username = Read-Host "Enter username"; Import-Module ActiveDirectory; Unlock-ADAccount $username}"
Upvotes: 2