Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

am facing an issue while creating new user in active directory from power shell, In this we need to provide values from batch file..am new to this please provide some work around how to pass parameters from batch file to shell script.

i tried this will it work?

###batch file: test.bat###
@echo off
set /p uname=%1
set /p sname=%2
set /p dname=%3
set /p pwd=%4
set /p upname=%5
powerShell.exe -File TestPs.ps1 %1 %2 %3 %4 %5
pause
###ps file:TestPs.ps1###
Import-Module ActiveDirectory
New-ADUser `
-Name "$args[0]" `
-Path "CN=Users,DC=some,DC=Local" `
-SamAccountName "$args[1]" `
-DisplayName "$args[2]" `
-AccountPassword (ConvertTo-SecureString "$args[3]" -AsPlainText -Force) `
-ChangePasswordAtLogon $true `
-Enabled $true
-userprincipalname ("$args[4]" + "incess.local")

[edit from="http://www.codeproject.com/script/Membership/View.aspx?mid=11010562"]

Sorry for troubling you i am new to this please never mind.
Here is the script.
Batch:test1.bat

JavaScript
@echo off
set /p uname=Enter user name:
set /p sname=Enter samAccount name:
set /p dname=Enter display name:
set /p pwd=Enter password:
set /p upname=Enter user principal name:
powershell.exe -File "testPs.ps1" %uname% %sname% %dname% %pwd% %upname%


and shellscript:

JavaScript
Import-Module ActiveDirectory

New-ADUser `

 -Name "args[0]" 

 -ChangePasswordAtLogon $true 

 -SamAccountName "args[1]" 

 -DisplayName "args[2]"

 -Enabled $true 

 -Path "CN=Users,DC=lab,DC=local" 

 -givenname user 

 -surname test 

 -AccountPassword (ConvertTo-SecureString "args[3]" -AsPlainText -Force) 

 -userprincipalname ("args[4]" + "@lab.local")


[/edit]
Posted
Updated 18-Aug-14 22:04pm
v3

1 solution

The command set /p displays a prompt string and sets a variable to the value input by the user. So it should read something like:
set /p uname="Enter the user name: "
set /p sname="Enter the sname: "
' ...
'
' then call powershell with the parameters thus
'
powerShell.exe -File TestPs.ps1 %uname% %sname%" ...
 
Share this answer
 
Comments
Member 11010562 14-Aug-14 5:14am    
Thanks for the quick response!! so those %uname% and %sname% kinda arguments can be taken as $args[0] like that in power shell right??
Richard MacCutchan 14-Aug-14 5:22am    
Should be, yes. A quick test with a simple script would confirm it.
Member 11010562 19-Aug-14 2:10am    
hi Richard, Those $args[o] parameters are not working in script..
showing an error message like missing expression after unary operator `-` at the specified path.
Richard MacCutchan 19-Aug-14 3:13am    
Sorry, but I cannot guess what you have done. Please edit your question and show the full script.
Member 11010562 19-Aug-14 3:22am    
Content moved to question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900