INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Call Powershell script from within another

Call Powershell script from within another

(OP)
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 --> Powershell

$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 --> Powershell

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?

RE: Call Powershell script from within another

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.

RE: Call Powershell script from within another

Like blister911 suggested:
1. make from your script for creating new user a function
2. and then for every line of your CSV-processing script call this function.
Look for program skeleton example here:
http://www.tek-tips.com/viewthread.cfm?qid=1639679

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close