asking for input in powershell
Hi all
I'm trying to automate some activities, like creating / deleting users. I don't know how to create a cmdlet that asks for name and surname, so I have to copy and paste users info
for example:
New-Mailbox -Name 'FIRSTNAME SURNAME' -displayName 'FIRSTNAME SURNAME' -Alias 'FIRSTNAME.SURNAME' -OrganizationalUnit 'test.local/migrated objects/users' -UserPrincipalName 'FIRSTNAME.SURNAME@corp.rcs.group'
-SamAccountName 'FIRSTNAME.SURNAME' -FirstName 'FIRSTNAME' -Initials '' -LastName 'SURNAME'
-ResetPasswordOnNextLogon $false -Database 'ECMISRMBRI\SG15\DB15' | set-mailbox -WindowsEmailAddress FIRSTNAME.SURNAME@test.local -PrimarySmtpAddress FIRSTNAME.SURNAME@test.local -EmailAddressPolicyEnabled $false
I use copy & paste to substitute FISRTNAME and SURNAME with the user's, but I would like a cmdlet that ask for name and surname. I tried with variables but so far without success
can you help me please
MCT MCSE:M|S MCITP MCTS
July 7th, 2010 4:00pm
Hi,
When u just want to run this command for one user then you can get the input in variables like this
$fname = [Console]::ReadLine()
$fname = $fname.Trim()
$surname = [Console]::ReadLine()
$surname = $surname.Trim()
and then can pass this $name and $surname variables to your command like this.
New-Mailbox -Name "$fname $surname" ........
IF you want to create multiple users using powershell command then best idea is to put user information into a CSV file and then import that csv file and then create users.
Your CSV file may contain info like this:
FirstName,Surname
Laeeq, Qazi
User2, SomeSurname
save this as a .csv file somewhere on your server, e.g C: drive
Here first line of csv file is for headers, and below that is data, which u will use like this
Import-csv "C:\Users.csv" | Foreach{
New-Mailbox -Name "$_.FirstName $_.Surname" ..........
}
Hope this help u.
Plz also have a look into:
http://www.exchangepedia.com/blog/2006/11/exchange-server-2007-bulk-creation-of.html
http://www.exchangeninjas.com/bulkcreatemailboxes
Regards,
Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM)
www.HostingController.com
Free Windows Admin Tool Kit Click here and download it now
July 7th, 2010 5:04pm
Hi Laeeq,
thanks for answering, but I already know how to bulk import. my need is to create users... one user at time! that's why I tryed something like
$firstname
$lastname
New-Mailbox -Name "$FirstName $lastname"...
my problem is twofold: how to have PS asks for firstname and lastname and how to insert them in powershell command
thanks againLuigi MCT MCSE:M|S MCITP MCTS
July 7th, 2010 5:19pm
Hi,
Dear i have already told u how to take input in ps
$fname = [Console]::ReadLine();
$fname = $fname.Trim() ;
$surname = [Console]::ReadLine();
$surname = $surname.Trim();
and how to use this in ps command
New-Mailbox -Name "$fname $surname" .....
Have u tried it.
Regards,
Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM)
www.HostingController.com
Free Windows Admin Tool Kit Click here and download it now
July 7th, 2010 5:27pm
You can also use the read-host cmdlet:
$firstname = Read-Host "Enter first name"
$lastname = Read-Host "Enter last name"
July 7th, 2010 5:35pm
Hi Laeeq,
again thank you for your help. I tried to input
fname = [Console]::ReadLine();
$fname = $fname.Trim() ;
$surname = [Console]::ReadLine();
$surname = $surname.Trim();
and I receive the following error. What's wrong?
You cannot call a method on a null-valued expression.
Alla :linea:2 carattere:21
+ $fname = $fname.Trim <<<< ();
Luigi MCT MCSE:M|S MCITP MCTS
Free Windows Admin Tool Kit Click here and download it now
July 7th, 2010 6:09pm
Hi Laeeq,
again thank you for your help. I tried to input
fname = [Console]::ReadLine();
$fname = $fname.Trim() ;
$surname = [Console]::ReadLine();
$surname = $surname.Trim();
and I receive the following error. What's wrong?
You cannot call a method on a null-valued expression.
Alla :linea:2 carattere:21
+ $fname = $fname.Trim <<<< ();
Luigi MCT MCSE:M|S MCITP MCTS
fname = [Console]::ReadLine();
Is it your mistake or did u really ran it like this.
Above statement is wrong, u have to pur dollor sign ($) before a variable name like this:
$fname = [Console]::ReadLine();Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM)
www.HostingController.com
July 7th, 2010 6:29pm
sorry bad cut & paste
this is the command that produced the error
$fname = [Console]::ReadLine();
$fname = $fname.Trim();
$surname = [Console]::ReadLine();
$surname = $surname.Trim();
and here the error
You cannot call a method on a null-valued expression.
Alla :linea:2 carattere:21
+ $fname = $fname.Trim <<<< ();Luigi MCT MCSE:M|S MCITP MCTS
Free Windows Admin Tool Kit Click here and download it now
July 7th, 2010 6:40pm
I think you are not inputting any thing, thats why error occuring. I have tested it on powershell 2.0, and its running fine.
Ok run below commands to get the input and see if it works
$fname = Read-Host "Enter first name"
$fname = $fname.Trim();
$surname = Read-Host "Enter surname"
$surname = $surname.Trim();
you can check whether there is something in the variables or not by just typing variable name and then pressing enter after you execute
above commands, like this:
$fname [press Enter]
Regards,Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM)
www.HostingController.com
July 7th, 2010 7:00pm
thanks, it worked!Luigi MCT MCSE:M|S MCITP MCTS
Free Windows Admin Tool Kit Click here and download it now
July 8th, 2010 11:31am
this way it worked! thanks a lot!Luigi MCT MCSE:M|S MCITP MCTS
July 8th, 2010 11:31am