2017-06-07 13 views
0

会社が同じ場合、将来の反復でelse文から$ SecretFolderを使用することは可能ですか?例えば。複数のユーザーが1つの会社のリストに存在していますが、会社にアクセスするには1つのフォルダに対して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 [email protected]() 
      $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 [email protected]() 
     $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 

答えて

0

は、あなたがそれを不可能だと思うする理由はありますか?うん、それは可能です、あなたはGoogleのハッシュテーブルとget変数でしようとしているすべてを行うことを唯一の良い方法を見つける必要があります。

しかし、あなたの質問には「どのようにスクリプトを書き直してうまくいくのですか?」ということになります。あなたのスクリプトを私に書き直すということは、重複した@()+=三重線、謎の数字、大域変数、余分な変数、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]