0

I'm having a difficult time getting this to work. This script will ask for the location for the GAM.exe, and then the location of a CSV. Then it should run a foreach loop executing GAM with the said variables to add bulk users to google docs. An example of working code:

$Gamfile 'create user' crashtestdummy 'firstname' "Crash" 'lastname' "Test Dummy" 'password' "BuckleUp"

The above works, and will add the user [email protected]

The issue I am having is in the foreach loop. I just get:

Unexpected token '$User'
Unexpected token ' 'password' '

Thanks in advance!

Function Get-GamFile($initialDirectory)
{   
  [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
  Out-Null

  $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
  $OpenFileDialog.initialDirectory = $initialDirectory
  $OpenFileDialog.filter = "GAM File (gam.exe)| gam.exe"
  $OpenFileDialog.ShowDialog() | Out-Null
  $OpenFileDialog.filename
} #end function Get-GamFile

$GamFile = Get-GamFile


Function Get-CSV($initialDirectory)
{   
  [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
  Out-Null

  $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
  $OpenFileDialog.initialDirectory = $initialDirectory
  $OpenFileDialog.filter = "CSV (*.csv)| *.csv"
  $OpenFileDialog.ShowDialog() | Out-Null
  $OpenFileDialog.filename
} #end function Get-CSV

$GetCSV = Get-CSV
$CSV = Import-Csv $GetCSV

Write-Host $GamFile
Write-Host $GetCSV

Function AddUsers {    
  foreach ($User in $CSV) {
    Write-Host Making User $User.firstname $user.lastname
    $GamFile 'create user' $User.firstname 'firstname' $User.firstname 'lastname' $User.lastname 'password' $User.Password    
  }
}

AddUsers


#$Gamfile 'create user' crashtestdummy 'firstname' "Crash" 'lastname' "Test Dummy" 'password' "BuckleUp"
3
  • 1
    If you just wrote this to screen "'$GamFile' create user '$($User.firstname)' firstname '$($User.firstname)' lastname '$($User.lastname)' password '$($User.Password)'" does that look ok? If it does should just be able to put an & infront of it to make it execute. Any of your last names have spaces maybe? Just spit balling. Commented Dec 22, 2014 at 20:04
  • Thanks for the response. Well, I had forgot to put a & in the loop and now it's no longer producing errors!!! but it's still acting like I just ran GAM.exe And not: GAM.exe 'create user' BobTest 'first name' bob "last name" test 'Password' temppass Commented Dec 22, 2014 at 20:51
  • perhaps 'create user' is being interpeted as one element when it really is two. Remove the quotes. & $GamFile create user $User.firstname .... Commented Dec 22, 2014 at 21:10

1 Answer 1

1

It looks like you just forgot the & required to execute an expression as an exe. Try this:

& $GamFile 'create user' $User.firstname 'firstname' $User.firstname 'lastname' $User.lastname 'password' $User.Password

As @Matt suggested, maybe the quotes are overzealous?

& $GamFile create user $User.firstname 'firstname' $User.firstname 'lastname' $User.lastname 'password' $User.Password
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. Well, I had forgot to put a & in the loop and now it's no longer producing errors!!! but it's still acting like I just ran GAM.exe And not: GAM.exe 'create user' BobTest 'first name' bob "last name" test 'Password' temppass
I do: Write-host $GamFile 'create user' $User.firstname 'firstname' $User.firstname 'lastname' $User.lastname 'password' $User.Password The copy what is spit out past it into a shell it creates the users. So it seems like my filtering is corrected. It got to be something simple that I'm over looking that powershell doesn't like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.