我有一個應該執行powershell腳本的批處理檔案,我還想從批處理檔案中將檔案路徑傳遞給腳本。然后應將檔案路徑插入腳本中的某個點。
$Berechtigung = Get-ACL -Path "C:\_TEST\TEST.txt"
你有什么想法?
uj5u.com熱心網友回復:
從批處理檔案 (
cmd.exe) 中,您必須呼叫 Windows PowerShell CLI,powershell.exe才能通過引數執行 PowerShell 腳本 (.ps1),可以選擇使用-File引數。在您的 PowerShell 腳本中,您可以參考呼叫者傳遞的引數:
- 要么:純粹是位置,通過自動
$args變數,其中$args[0]包含第一個引數,$args[1]第二個,依此類推。 - 或:通過宣告的引數,例如- 請參閱概念about_Functions幫助主題
param([string] $Path)的相關部分。
- 要么:純粹是位置,通過自動
所以:
假設您的 PowerShell 腳本已命名foo.ps1并包含以下內容:
# Content of foo.ps1
param([string] $Path) # Declare a string parameter named -Path
$Berechtigung = Get-ACL -Path $Path
然后,您可以按如下方式從批處理檔案中呼叫此腳本(假設foo.ps1位于當前目錄中 - 根據需要進行調整):
@echo off
:: ...
powershell.exe -NoProfile -File .\foo.ps1 "C:\_TEST\TEST.txt"
請注意,如果您宣告引數(使用param(...)),您還可以將引數作為命名引數傳遞,即將目標引數名稱放在引數之前:
:: Note the -Path before the file path.
:: Only works if you have declared this parameter via param(...)
powershell.exe -NoProfile -File .\foo.ps1 -Path "C:\_TEST\TEST.txt"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/520301.html
標籤:电源外壳批处理文件
