Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call Powershell script from within another

Status
Not open for further replies.

disturbedone

Vendor
Sep 28, 2006
781
AU
I have a Powershell script that users manually run to create new users. It prompts the user for values for name, phone number etc. It works without issue.

I have a CSV file of several hundred new users. One option is to manually run the current script and manually enter all values but that would take considerable time.

Is it possible to create a new script that imports the CSV, assigns values from the CSV, calls the original script and inputs them into specific fields?

For example, the original script has line saying:
Code:
$LOGONALIAS = Read-Host "What is the users USERNAME?"
where the user would normally manually type the new user's username.

I've previously used Powershell scripts to import CSV files using a command such as:
Code:
import-csv C:\CSVFile.csv | foreach {
	$DistGroupName =  $_.CSVColumnName
        etc etc etc

My CSV file has a column called ID so I'd like to import that and use it for the variable that is in the existing script as $LOGONALIAS (as above). There would obviously be other values for first name, surname etc.

Is such a thing possible? If so, how?
 
You can call the script from within a different one, but I wonder how you are going to pass the values from the csv file into the second script. I've never tried it (I'm still pretty new to Powershell).

You should be able to call the second script like this (assuming it's in the same directory and named Create_User.ps1):
. .\Create_User.ps1
^There is a space here

What I have done is something like this:
Copy your script, make the script a function, include the script, then call the function.

Here is an example
If your current Create_User.ps1 script looks like this
Code:
$LOGONALIAS = Read-Host "What is the users USERNAME?"
# The rest of your current script with more user input requests

You'd need to do something like this
Main script
Code:
. .\Create_User.ps1
import-csv C:\CSVFile.csv | foreach {
	$DistGroupName =  $_.CSVColumnName
        $LOGONALIAS = $_.ID
        etc etc etc 
Fcn_Create_User $DistGroupName $LOGONALIAS $etc1 $etc2 $etc3

Then your Create_User.ps1 would basically need to be encapsulated into a function with the user prompts removed
Code:
function Fcn_Create_User{
     param($DistGroupName,$LOGONALIAS,$etc1,$etc2,$etc3)
     #$LOGONALIAS = Read-Host "What is the users USERNAME?"
     # the rest of your current script goes here minus the user prompts (which can just be commented out; like previous line
     }

Hope this helps.


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top