배치 파일에서 PS1 스크립트를 호출하면 외부 스크립트가 실행되지 않는 이유는 무엇입니까?

배치 파일에서 PS1 스크립트를 호출하면 외부 스크립트가 실행되지 않는 이유는 무엇입니까?

배치 파일에서 일부 PowerShell(.ps1) 스크립트를 호출하는 일부 스크립트를 사용하고 있습니다. 이 특정 상황에서는 이 기능을 사용할 수 없습니다.

다음 배치 파일이 있습니다.

@echo off
TITLE Modifying Quick Access Pinned Items
color f0
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'r:\Tools\QuickAccessPin.ps1'";

QuickAccessPin.ps1:

.\Set-QuickAccess.ps1 -Action Unpin -Path "$env:userprofile\Desktop"
.\Set-QuickAccess.ps1 -Action Unpin -Path "$env:userprofile\Downloads"
.\Set-QuickAccess.ps1 -Action Unpin -Path "$env:userprofile\Documents"
.\Set-QuickAccess.ps1 -Action Pin -Path "$env:userprofile"
.\Set-QuickAccess.ps1 -Action Pin -Path "$env:userprofile\Desktop"
.\Set-QuickAccess.ps1 -Action Pin -Path "$env:userprofile\Documents"
.\Set-QuickAccess.ps1 -Action Pin -Path "$env:userprofile\Downloads"

배치 파일을 실행할 때 오류가 발생했습니다:(이를 캡처하기 위해 배치 파일 끝에 '일시 중지'를 삽입했습니다. 이 중 8개가 표시되며, 이것이 .ps1:8로 표시된 마지막 파일입니다.)

.\Set-QuickAccess.ps1 : The term '.\Set-QuickAccess.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.
At R:\Tools\QuickAccessPin.ps1:7 char:1
+ .\Set-QuickAccess.ps1 -Action Pin -Path "$env:userprofile\Downloads
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\Set-QuickAccess.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS1 스크립트에 문제가 있을 수 있다고 생각하여 PowerShell ISE에서 파일을 열고 F5를 눌러 스크립트를 실행했습니다. 그렇게 하면 다음과 같은 일이 발생하며 QuickAccessPin.ps1에서 호출된 각 명령에 대해 한 번씩 "한 번 실행"을 7번 클릭해야 합니다.

여기에 이미지 설명을 입력하세요

따라서 PS1을 직접 실행하면 예상대로 작동하지 않지만 배치 파일에서 호출하면 전혀 작동하지 않습니다. 또한 현재 테스트를 위해 내 실행 정책은 다음과 같습니다.무제한(대개원격 서명됨). 또한 문제를 제거하기 위해 PowerShell이 ​​GUI에서 파일을 실행하라는 메시지를 표시하지 않도록 아래 실행도 시도했습니다.

@echo off
TITLE Unblocking PowerShell Tools
color f0
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'r:\Tools\UnblockThisFolder.ps1'";
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser -Force}"

UnblockThisFolder.ps1은 단순히

Get-ChildItem r:\Tools | Unblock-File

...하지만 PowerShell ISE에서는 파일이 안전하다면 파일을 "차단 해제"할 수 있다고 말하면서 실행하라는 메시지를 계속 표시하므로 아무런 작업도 수행되지 않는 것 같습니다(이 작업은 이미 13번 정도 수행했습니다). 정책을 RemoteSigned로 설정한 다음 위의 스크립트를 실행하고 Get-ExecutionPolicy를 다시 실행하면 Unrestricted가 표시되므로 4번째 줄은 작동하고 3번째 줄은 아무 작업도 수행하지 않는 것처럼 보입니다.

따라서 매번 메시지가 표시된다는 사실은 사소한 짜증일 뿐입니다. 왜냐하면 이 모든 것이 자동화되고 GUI를 전혀 사용하지 않을 것이기 때문입니다. 하지만 지금은 배치 파일에서 PS1 스크립트를 호출할 수 없습니다. 이를 수행해야 하는데, 스크립트를 실행할 수 있는 유일한 방법은 ISE 내에서 수동으로 수행하는 것입니다.

이 문제를 어떻게 해결할 수 있는지 아는 사람이 있나요?

답변1

귀하가 받고 있는 오류는 귀하가 현재 있다고 생각하는 디렉토리에 없을 가능성이 높으며 전체 경로로 스크립트 파일을 실행해도 해당 디렉토리에 들어가지 않는다는 것을 나타냅니다. 따라서 Set-QuickAccess.ps1에 있다면 R:\Tools다음을 보장할 수 없습니다. R:\Tools배치 파일을 실행할 때 있어야 합니다 .

스크립트가 있는 Set-Location경로에 대해 작업을 수행하여 시작해야 합니다 .Set-QuickAccess.ps1

관련 정보