명령줄에서 Powershell 스크립트를 실행하고 디렉터리를 매개 변수로 전달하는 방법
PowerShell -Command .\Foo.ps1
Foo.ps1
:Function Foo($directory) { echo $directory } if ($args.Length -eq 0) { echo "Usage: Foo <directory>" } else { Foo($args[0]) }
불구하고
Foo.ps1
Powershell을 호출하는 디렉토리에 있으면 다음과 같은 결과가 발생합니다.The term '.\Foo.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
- 편집: PowerShell이 다음과 같은 이유로 디렉터리를 변경하는 중이었기 때문에 작동하지 않았습니다.
profile.ps1
재중cd C:\
- 편집: PowerShell이 다음과 같은 이유로 디렉터리를 변경하는 중이었기 때문에 작동하지 않았습니다.
그런 다음 스크립트 파일에 대한 전체 경로를 지정하여 호출하려고 했지만, 어떤 방법을 시도해도 실행할 수 없습니다.스크립트에 인수로 전달해야 하는 파일 이름과 마찬가지로 공백이 포함되어 있기 때문에 경로를 따옴표로 묶어야 한다고 생각합니다.
지금까지의 최선의 추측:
PowerShell -Command "'C:\Dummy Directory 1\Foo.ps1' 'C:\Dummy Directory 2\File.txt'"
출력 오류:
Unexpected token 'C:\Dummy Directory 2\File.txt' in expression or statement. At line:1 char:136.
사용해 보십시오.
powershell "C:\Dummy Directory 1\Foo.ps1 'C:\Dummy Directory 2\File.txt'"
명령이 아닌 스크립트 파일을 호출하는 경우 -file eg:
powershell -executionPolicy bypass -noexit -file "c:\temp\test.ps1" "c:\test with space"
PS V2용
powershell.exe -noexit &'c:\my scripts\test.ps1'
(이 기술 세트 페이지 하단을 확인하십시오. http://technet.microsoft.com/en-us/library/ee176949.aspx )
플래그 사용-Command
PowerShell 프롬프트의 명령처럼 전체 PowerShell 라인을 실행할 수 있습니다.
powershell -Command "& '<PATH_TO_PS1_FILE>' '<ARG_1>' '<ARG_2>' ... '<ARG_N>'"
이를 통해 Visual Studio Post-Build 및 Pre-Build 이벤트에서 PowerShell 명령을 실행하는 문제가 해결되었습니다.
ps1 파일의 맨 위에 매개 변수 선언 추가
test.ps1
param(
# Our preferred encoding
[parameter(Mandatory=$false)]
[ValidateSet("UTF8","Unicode","UTF7","ASCII","UTF32","BigEndianUnicode")]
[string]$Encoding = "UTF8"
)
write ("Encoding : {0}" -f $Encoding)
결과
C:\temp> .\test.ps1 -Encoding ASCII
Encoding : ASCII
코드를 다음으로 변경합니다.
Function Foo($directory)
{
echo $directory
}
if ($args.Length -eq 0)
{
echo "Usage: Foo <directory>"
}
else
{
Foo([string[]]$args)
}
다음과 같이 호출합니다.
powershell - 실행 정책 원격 서명 - 파일 "c:\foo.ps1" "c:\Documents and Settings" "c:\test"
유형이 있고 Enter 키를 누릅니다.
PowerShell - 명령
언급URL : https://stackoverflow.com/questions/13724940/how-to-run-a-powershell-script-from-the-command-line-and-pass-a-directory-as-a-p
'programing' 카테고리의 다른 글
파워셸을 사용하여 xml에서 속성 값을 가져오는 방법은 무엇입니까? (0) | 2023.07.26 |
---|---|
IN 절에 대한 매개 변수가 있는 Oracle 저장 프로시저 (0) | 2023.07.26 |
MySQL에서 여러 열의 MAX() 선택 (0) | 2023.07.26 |
Mariadb 10.4 부 버전 검토 (0) | 2023.07.26 |
Spring Boot (0) | 2023.07.26 |