Powershell运行ForEach循环时丢失数组中的值

时间:2014-08-14 13:55:03

标签: powershell powershell-v2.0

运行下面的foreach命令后,我留下数组中的最后一个值,其他值丢失。有人可以帮忙吗?

$InternationalSupportedList = @("GBR","USA","SAU","HKG","AUS", "AUT")

    foreach ($InternationalSupportedList in $InternationalSupportedList) {

    run-MySQLQuery -ConnectionString "$DatabaseSrvIP;$MysqlUid;$MysqlPwd;$MysqlDatabase;" -Query "
    SELECT 'Title', 'Location', 'Company', 'URL', 'Description', 'Salary', 'Type', 'Source', 'SourceType', 'Date', 'Rank_Score', 'Title_Keyword'
    UNION ALL
    SELECT Title,Location,Company,URL,Description,Salary,Type,Source,SourceType,Date,Rank_Score,Title_Keyword
    FROM ${ProjectName}
    Where Country = '${InternationalSupportedList}'
    INTO OUTFILE 'U:/test/Csv-Output/${ProjectName}/${ProjectName}_${InternationalSupportedList}.csv'
    FIELDS TERMINATED BY ','
    ENCLOSED BY '`"'
    LINES TERMINATED BY '\r\n';";

    }

因此,如果我在Powershell ISE中查看$ InternationalSupportedList,我会得到AUT,其余的都会丢失..

1 个答案:

答案 0 :(得分:0)

您在foreach子句中使用了两次相同的变量。这使得它在枚举完成后引用集合中的最后一项。

尝试

foreach ($InternationalSupportedItem in $InternationalSupportedList)

并修改查询字符串以引用$InternationalSupportedItem而不是$InternationalSupportedList

该脚本是否有效?我不确定使用"${variablename}"形式的引用变量是否有效。通常的表单只是"$variablename"或子表达式"$($variablename)"