运行foreach循环powershell

时间:2014-12-22 19:29:34

标签: powershell csv doc

我很难让这个工作起来。此脚本将询问GAM.exe的位置,然后询问CSV的位置。然后它应该运行一个foreach循环执行带有所述变量的GAM,以将批量用户添加到谷歌文档。工作代码的一个例子:

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

以上工作,并将添加用户Crashtestdummy@School.org

我遇到的问题是在foreach循环中。我得到:

  

意外的令牌'$ User'
   意外的令牌''密码''

提前致谢!

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"

1 个答案:

答案 0 :(得分:1)

看起来您忘记了将表达式作为exe执行所需的&。试试这个:

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

正如@Matt建议的那样,引言可能过于热心了?

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