我正在 VSCode 中練習 Powershell。我注意到當我按 F5 運行我的腳本時,Powershell 運行腳本的先前版本(在我的更改之前)。我肯定會在編輯后保存檔案。因此,例如,我可能會更改此行中的文本:
Add-Content -Path "D:\Text_File.txt" -value "$_ - MP3 File count is: $Cnt"
但它會繼續顯示相同的輸出,直到我第二次運行它。
這是我正在使用的腳本,以防萬一它是相關的:
Param(
[string]$Path = 'D:\WMA - BU of wma music files',
[string]$DestinationPath = "D:\WMA-Only",
[string]$OutputFileDest = "D:\Text_File.txt"
)
$MainDirList = Get-ChildItem -Path $Path -Directory | ForEach-Object {$_.FullName}
$MainDirList | Out-File -FilePath D:\Text_File.txt
$MainDirList | WriteFolder
function WriteFolder
{
process
{
$FilteredList = Get-ChildItem -Path $_ -Force -Recurse -Filter "*.mp3"
$Cnt = $FilteredList.count
Add-Content -Path "D:\Text_File.txt" -value "$_ - MP3 File count is: $Cnt"
}
}
Visual Studio 代碼版本是 1.62.2 Powershell 擴展是 v2021.10.2
我想知道我是否打算在腳本末尾清除變數或類似內容?我正在查看最新版本的 notepad 中的輸出
uj5u.com熱心網友回復:
可能是因為您在另一個函式中定義了一個函式?通常,當您運行腳本時,它會在運行腳本之前找到任何函式并對其進行預處理。但在你的情況下,它只處理外部函式,它只在運行時創建內部函式。
我不確定你的父函式被呼叫了什么,但你可以嘗試并排使用這兩個函式:
Function myFunction{
Param(
[string]$Path = 'D:\WMA - BU of wma music files',
[string]$DestinationPath = "D:\WMA-Only",
[string]$OutputFileDest = "D:\Text_File.txt"
)
Process{
$MainDirList = Get-ChildItem -Path $Path -Directory | ForEach-Object {$_.FullName}
$MainDirList | Out-File -FilePath D:\Text_File.txt
$MainDirList | WriteFolder
}
}
function WriteFolder
{
process
{
$FilteredList = Get-ChildItem -Path $_ -Force -Recurse -Filter "*.mp3"
$Cnt = $FilteredList.count
Add-Content -Path "D:\Text_File.txt" -value "$_ - MP3 File count is: $Cnt"
}
}
或者,您可以將 WriteFolder 函式移動到它自己的 Powershell 模塊 (*.psm1) 中并將其加載到您的主腳本中:
Import-Module "WriteFolder.psm1" -Force
該-Force引數是可選的,但如果您定期更改模塊檔案,則需要它來確保加載更新,而不僅僅是快取舊版本的模塊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/362251.html
上一篇:替換匯入但保留字串
