PowerShell에서이 스크립트를 실행하려고합니다. 아래 스크립트를 ps.ps1
데스크탑 과 같이 저장했습니다 .
$query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"
Register-WMIEvent -Query $query -Action { invoke-item "C:\Program Files\abc.exe"}
이 PowerShell 스크립트를 실행하기위한 배치 스크립트를 만들었습니다.
@echo off
Powershell.exe set-executionpolicy remotesigned -File C:\Users\SE\Desktop\ps.ps1
pause
그러나이 오류가 발생합니다.
답변
-ExecutionPolicy
매개 변수 가 필요합니다 .
Powershell.exe -executionpolicy remotesigned -File C:\Users\SE\Desktop\ps.ps1
그렇지 않으면 PowerShell은 인수를 실행하는 라인을 고려하고있는 동안 Set-ExecutionPolicy
이다 cmdlet에, 그것은 더없는 -File
매개 변수를.
답변
배치 파일에서 PowerShell 스크립트를 호출하려는 이유와 블로그 게시물 here에서 수행하는 방법에 대해 설명합니다 .
이것은 기본적으로 당신이 찾고있는 것입니다 :
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\Users\SE\Desktop\ps.ps1'"
PowerShell 스크립트를 관리자 권한으로 실행해야하는 경우 다음을 사용하십시오.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\Users\SE\Desktop\ps.ps1""' -Verb RunAs}"
그래도 PowerShell 스크립트의 전체 경로를 하드 코딩하는 대신 내 블로그 게시물에서 설명하는 것처럼 배치 파일과 PowerShell 스크립트 파일을 동일한 디렉토리에 배치하는 것이 좋습니다.
답변
완전한 경로없이 현재 디렉토리에서 실행하려면 다음을 사용할 수 있습니다.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& './ps.ps1'"
답변
PowerShell을 관리자로 호출하는 배치 파일을 실행하면 다음과 같이 실행하는 것이 좋습니다.
powershell.exe -ExecutionPolicy Bypass -Command "Path\xxx.ps1"
사용하는 것이 좋습니다 Bypass
…
답변
몇 개의 스크립트를 실행하려면을 사용 Set-executionpolicy -ExecutionPolicy Unrestricted
하여 재설정 할 수 있습니다 Set-executionpolicy -ExecutionPolicy Default
.
실행 정책은 실행을 시작할 때만 확인되므로 백그라운드에서 작업을 실행하고 즉시 실행 정책을 재설정 할 수 있습니다.
# Check current setting
Get-ExecutionPolicy
# Disable policy
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
# Choose [Y]es
Start-Job { cd c:\working\directory\with\script\ ; ./ping_batch.ps1 example.com | tee ping__example.com.txt }
Start-Job { cd c:\working\directory\with\script\ ; ./ping_batch.ps1 google.com | tee ping__google.com.txt }
# Can be run immediately
Set-ExecutionPolicy -ExecutionPolicy Default
# [Y]es
답변
배치에서 ps 스크립트를 실행하는 또 다른 쉬운 방법은 ECHO와 리디렉션 문자 (> 및 >>) 사이에 간단히 스크립트를 통합하는 것입니다. 예 :
@echo off
set WD=%~dp0
ECHO New-Item -Path . -Name "Test.txt" -ItemType "file" -Value "This is a text string." -Force > "%WD%PSHELLFILE.ps1"
ECHO add-content -path "./Test.txt" -value "`r`nThe End" >> "%WD%PSHELLFILE.ps1"
powershell.exe -ExecutionPolicy Bypass -File "%WD%PSHELLFILE.ps1"
del "%WD%PSHELLFILE.ps1"
마지막 줄은 생성 된 임시 파일을 삭제합니다.
답변
PowerShell 로그인 스크립트가 2012 년 서버에서 5 분 후에 실행 된 경우 서버에 GPO 설정이 있습니다. ‘로그인 스크립트 지연 구성’기본 설정 ‘구성되지 않음’이 5 분 지연됩니다. 로그인 스크립트를 실행하기 전에
![](http://daplus.net/wp-content/uploads/2023/04/coupang_part-e1630022808943-2.png)