正如標題已經說的那樣,我對我的簡單 PowerShell 腳本感到非常困惑
function create_dir([array]$folders)
{
$dir = "D:\test\New Folders"
foreach ($folder in $folders)
{
Write-Host $folder
New-Item -Path $dir -Name $folder -ItemType "directory" -Force
}
}
$test_path = "D:\test\New Folders"
$my_folders = @("txt","png","jpg", "c")
create_dir($folders)
當我運行它時,一切正常并按預期運行(檔案夾被創建)。
模式 LastWriteTime 長度 名稱
d----- 20.04.2022 11:11 txt
png d----- 20.04.2022 11:11 png
jpg d----- 20.04.2022 11:11 jpg
c d----- 20.04。 2022 年 11 點 11 分
但是當我現在像這樣向引數串列添加一個字串時:
function create_dir([string]$test, [array]$folders)
{
$dir = "D:\test\New Folders"
foreach ($folder in $folders)
{
Write-Host $folder
New-Item -Path $test -Name $folder -ItemType "directory" -Force
}
}
$test_path = "D:\test\New Folders"
$my_folders = @("txt","png","jpg", "c")
create_dir($test_path, $my_folders)
它停止作業,但我沒有收到任何錯誤。因為我的陣列會突然變空或什么...
$test_path = "D:\test\New Folders" $my_folders = @("txt","png","jpg", "c")
創建目錄($test_path,$my_folders)
你們能幫幫我嗎?
uj5u.com熱心網友回復:
我不確定你的第一個腳本是如何作業的,因為你發送的引數“$folders”沒有在任何地方定義。
但是要回答您的問題,只需傳遞引數/呼叫您的函式,而不使用括號和逗號。
前任。create_dir $test_path $my_folders
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/460726.html
下一篇:如何僅列出已啟用的本地管理員
