2016-07-15 161 views
0

我需要創建一個腳本,要求用戶輸入文件夾名稱\路徑$File。從$File獲得子文件夾名稱,例如$File\docs\ref\$host。然後將$host複製到新的網絡共享中,並創建該文件夾名稱$host並將其內容粘貼到那裏。讀取主機根文件夾的Aquire子文件夾名稱

我不能只是複製$host的原因是因爲$host名稱發生更改,與$File一樣,但兩者之間的子文件夾相同。 我希望這是有道理的?

這是我有:

$file = Read-Host -Prompt 'What is your folder?' 
# This is where I'm stuck 
$host = Read-Host $file\docs\ref\$host 
Test-Path \\Hulk\note\$host 
If false 
MD \\Hulk\note\$host 
Copy-Item –Path $file\docs\ref\$host\ -Destination \\Hulk\note\$host\ –Recurse 
+1

'$ file \ docs \ ref \ $ host'中的$ host是從哪裏來的?你爲什麼使用'Read-Host'呢?另外,'$ host'是一個[自動變量](https://technet.microsoft.com/en-us/library/hh847768.aspx),它包含有關主機進程的信息。你不應該修改它。 –

+0

'$ file = Read-Host -Prompt'你的文件夾是什麼?''將包含文件夾名稱不在$ Host – DisplayName

+0

啊,我沒有意識到$ host是一個自動變量 –

回答

0

假設\綠巨人\記\裁判是目標,你可以根據輸入主機上該目錄中創建文件夾,複製相同的文件夾在任何地方試試這個

$location="\\Hulk\note\ref" 
    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null 
    $folderName = Read-Host -Prompt 'Input folder name' 
    $target = Join-Path $location $folderName 
    if (-not (Test-Path '$target' -PathType Container)) { new-item "\\Hulk\note\$foldername" -type directory } 
Copy-Item –Path \\path\$foldername -Destination \\where you need –Recurse 
相關問題