
[英]Running foreach loop in powershell prompts for credentials every loop?
[英]Running foreach loop powershell
我很难让它开始工作。 该脚本将询问GAM.exe的位置,然后询问CSV的位置。 然后,应使用上述变量运行一个执行GAM的foreach循环,以将大量用户添加到Google文档中。 工作代码示例:
$Gamfile 'create user' crashtestdummy 'firstname' "Crash" 'lastname' "Test Dummy" 'password' "BuckleUp"
上面的方法有效,并将添加用户Crashtestdummy@School.org
我遇到的问题在foreach循环中。 我得到:
意外的令牌“ $ User”
意外令牌''password''
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"
看来您只是忘记了将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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.