2017-02-09 76 views
1

如何使用PowerShell將文件中的數據從文件讀入變量? 我ListOfFileNames.txt具有都是這樣的一個數組:如何使用Powershell將數組讀入特定文件中的變量以使用Powershell中的變量

#Begin ArrayOneList: 
Filename4.txt 
FilenameD.png 
myone.exe 

#Begin ArrayTwo: 
myFile2.txt 
Filename1.gif 

#Begin ArrayNextOne: 
speficifFile.doc 
ExcelFile.xls 
File3.png 
AnotherFile.png 
logFile.log 

現在我想讀入數組變量和PowerShell中與他們合作:

$ArrayOneList = Get-Content C:\Scripts\ListOfFileNames.txt 
foreach($File in $ArrayOneList) 
{ 
    $Weekday = $File.LastWriteTime.DayOfWeek 
    $Destination = "D:\Test\$Weekday" 

    Copy-Item $File.FullName -Destination $Destination -Recurse  
} 


$ArrayTwo = Get-Content C:\Scripts\ListOfFileNames.txt 
foreach($File in $ArrayTwo) 
{ 
    $Weekday = $File.LastWriteTime.DayOfWeek 
    $Destination = "E:\Dir\Dest" 

    Copy-Item $File.FullName -Destination $Destination -Recurse  
} 


$ArrayNextOne= Get-Content C:\Scripts\ListOfFileNames.txt 
foreach($File in $ArrayNextOne) 
{ 
    $Weekday = $File.LastWriteTime.DayOfWeek 
    $Destination = "E:\Dir\SpecificDirectory" 

    Copy-Item $File.FullName -Destination $Destination -Recurse  
} 

我怎樣才能閱讀所有的數組從一個文件到變量?

回答

0

你可以這樣做:

param(
    [parameter(Mandatory=$true)] 
    [string[]]$myarr 
) 

參考THIS例子也

在你的情況,你可以這樣做:

param(
    [parameter(Mandatory=$False)] 
    [string[]][email protected]("ArrayOneList","ArrayTwo","ArrayNextOne") 
) 
+0

難道我已經像這樣調用它?:Powershell.exe -File「MyScriptPath-ps1」「ArrayOneList,ArrayTwo,ArrayNextOne」如何在不從外部調用數組的情況下使用這些參數,而只能從腳本內部調用這些參數? – Mailo

+0

@Mailo:是的,你必須這樣打電話。我給出的鏈接,你可以看到一些例子 –

+0

沒有例子說明如何在不從外部調用參數的情況下使用它。我只想調用poweeshellfile.ps1 withouth參數 – Mailo

0

如果你可以使用JSON作爲格式你的輸入文件,你可以簡單地「反序列化」數組。示例:

PS C:\temp> [string[]]$array = @("1","2","3") 

PS C:\temp> ConvertTo-Json $array 
[ 
    "1", 
    "2", 
    "3" 
] 

PS C:\temp> ConvertTo-Json $array | Out-File array.json 

PS C:\temp> Get-Content .\array.json | ConvertFrom-Json 
1 
2 
3 

PS C:\temp> [string[]]$array2 = Get-Content .\array.json | ConvertFrom-Json 

PS C:\temp> $array2.GetType() 

IsPublic IsSerial Name          BaseType                               
-------- -------- ----          --------                               
True  True  String[]         System.Array 

PS C:\temp> $array2 
1 
2 
3 

要使用多個數組,您可以創建一個包含這些數組的哈希表。例如:

PS C:\temp> $arr1 = @("1","2") 

PS C:\temp> $arr2 = @("3","4") 

PS C:\temp> $arrays = @{"arr1"=$arr1; "arr2"=$arr2} 

PS C:\temp> $arrays 

Name       Value                                       
----       -----                                       
arr1       {1, 2}                                       
arr2       {3, 4}                                       


PS C:\temp> $arrays | ConvertTo-Json 
{ 
    "arr1": [ 
       "1", 
       "2" 
       ], 
    "arr2": [ 
       "3", 
       "4" 
       ] 
} 

PS C:\temp> $arrays | ConvertTo-Json | Out-File hash.json 

PS C:\temp> $recreatedHash = get-content .\hash.json | ConvertFrom-Json 

PS C:\temp> $recreatedHash 

arr1 arr2 
---- ---- 
{1, 2} {3, 4} 

希望幫助

+0

對不起,這似乎是專家可以理解,但不適合我。我在哪裏打電話給我的ListOfFileNames.txt?我在哪裏聲明陣列「ArrayOneList,ArrayTwo,ArrayNextOne ...」的名稱? – Mailo

0

這裏是閱讀,其中部分由空行分隔的文本文件的內容(小)的解決方案:

# First read the file as one string 
$a = Get-Content 'D:\temp\test.txt' -raw 

# Get your arrays (here on the fact that the separator is an empty line 
$b = $a -split '\n\r' 

# Use array One 
foreach ($file in $($b[0] -split '\n')) 
{ 
} 

# You can use $b[1] for file two etc... 
+0

我的數組之間用#號分開只有用empy行分隔的條目 – Mailo

+0

這不是您在示例中給出的內容嗎?你可以編輯你的樣本,我會編輯我的答案。 – JPBlanc