【问题标题】:Use a variable from a previous iteration in a future iteration在未来的迭代中使用来自先前迭代的变量
【发布时间】:2017-11-08 05:09:30
【问题描述】:

如果公司相同,是否可以在未来的迭代中使用 else 语句中的 $SecretFolder。例如。一个公司的列表中存在多个用户,但他们都需要为 1 个文件夹生成一个链接以供公司访问。

#Location of original dataset
$csv = Import-Csv c:\export.csv

#loops through every line of the csv
Foreach ($line in $csv){

    #Generate random folder name (8 Characters long)
    $SecretFolder = -join ((48..57) + (97..122) | Get-Random -Count 8 | % {[char]$_})

    #Create URL
    $url = "www.website.com.au/2017Rates/$SecretFolder"


    #Test: Has the company already had a folder created
    if (Get-Variable $line.CompanyName -Scope Global -ErrorAction SilentlyContinue)
        {
           #Append URL to CSV for a person who already has a company folder
           $report =@()
           $report += New-Object psobject -Property @{CompanyName=$line.CompanyName;FirstName=$line.FirstName;LastName=$line.LastName;EmailAddress=$line.EmailAddress;'Letter Type'=$line.'Letter Type';URL=$URL}
           $report | export-csv testreporting.csv -Append
        }

     else 
     {
        #Create Folder with Random Cryptic name
        mkdir C:\Users\bford\test\$SecretFolder

        #Copy item from FileLocation in CSV to SecretFolder Location
        Copy-Item -Path $line.FileLocation -Destination c:\users\bford\test\$SecretFolder -Recurse -ErrorAction SilentlyContinue

        #Create Variable for Logic test with the Name CompanyName
        New-Variable -Name $line.CompanyName

        #Append csv with the updated details
        $S_report =@()
        $S_report += New-Object psobject -Property @{CompanyName=$line.CompanyName;FirstName=$line.FirstName;LastName=$line.LastName;EmailAddress=$line.EmailAddress;'Letter Type'=$line.'Letter Type';URL=$url}
        $S_report | export-csv testreporting.csv -Append

    }
}

#Cleanup remove all the variables added
Remove-Variable * -ErrorAction SilentlyContinue

【问题讨论】:

    标签: powershell csv scripting powershell-2.0


    【解决方案1】:

    你有任何理由认为这是不可能吗?是的,有可能,您应该 Google 哈希表并发现它们可以完成您尝试使用 get-variable 执行的所有操作,而且效果会更好。

    但您的问题相当于“我如何重写我的脚本以使其正常工作?”重写你的脚本对我来说意味着摆脱重复的@()+= 三行、神秘数字、全局变量、额外变量和 if/else,它最终会变成一个完全不同的脚本。

    一个完全不同且大部分未经测试的脚本:

    # import and group all people in the same company together
    # then loop over the groups (companies)
    Import-Csv -Path c:\export.csv |                      
      Group-Object -Property CompanyName |              
      ForEach-Object {
    
        # This loop is once per company, make one secret folder for this company.
        $SecretFolder = -join ( [char[]]'abcdefghijklmnopqrstuvwxyz1234567890' | Get-Random -Count 8 )
        New-Item -ItemType Directory -Path "C:\Users\bford\test\$SecretFolder"
    
        # Loop through all the people in this company, and copy their files into this company's secret folder
        $_.Group | ForEach-Object {
            Copy-Item -Path $_.FileLocation -Destination c:\users\bford\test\$SecretFolder -Recurse -ErrorAction SilentlyContinue
        }
    
        # Output each person in this company with just the properties needed, and a new one for this company's URL
        $_.Group | Select-Object -Property CompanyName , FirstName, 
                                           LastName, EmailAddress, 'Letter Type', 
                                           @{Name='Url'; Expression={"www.website.com.au/2017Rates/$SecretFolder"}}
    
    } | Export-Csv -Path testreporting.csv -NoTypeInformation
    

    但要编辑脚本以执行您想要的操作,请使用哈希表,例如

    $SecretFolders = @{} #at top of your script, outside loops
    
    # in loops:
    if (-not $SecretFolders.ContainsKey($line.CompanyName))
    {
        $SecretFolders[$line.CompanyName] = -join (random name generation here)    
    }
    
    $SecretFolder = $SecretFolders[$line.CompanyName]
    

    【讨论】:

      猜你喜欢
      • 2022-10-04
      • 2023-02-03
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 2021-08-15
      相关资源
      最近更新 更多