program tip

창을 표시하지 않고 PowerShell 스크립트를 실행하는 방법은 무엇입니까?

radiobox 2020. 8. 4. 07:36
반응형

창을 표시하지 않고 PowerShell 스크립트를 실행하는 방법은 무엇입니까?


사용자에게 창이나 다른 기호를 표시하지 않고 PowerShell 스크립트 를 실행하는 방법은 무엇입니까?

다시 말해, 스크립트는 사용자에게 아무런 표시없이 백그라운드에서 조용히 실행되어야합니다.

타사 구성 요소를 사용하지 않는 답변에 대한 추가 크레딧 :)


다음과 같이 실행할 수 있습니다 (그러나 이것은 잠시 동안 창을 보여줍니다).

PowerShell.exe -windowstyle hidden { your script.. }

또는 내가 만든 도우미 파일을 사용하여 정확히 그렇게하는 PsRun.exe라는 창을 피하십시오. PowerShell에서 WinForm GUI를 사용하여 소스 및 exe 파일 실행 예약 된 작업을 다운로드 할 수 있습니다 . 예약 된 작업에 사용합니다.

편집 : Marco가 지적했듯이이 -windowstyle 매개 변수는 V2에서만 사용 가능합니다.


PowerShell 커뮤니티 확장을 사용하여 다음을 수행 할 수 있습니다 .

start-process PowerShell.exe -arg $pwd\foo.ps1 -WindowStyle Hidden

VBScript를 사용하여이 작업을 수행 할 수도 있습니다. http://blog.sapien.com/index.php/2006/12/26/more-fun-with-scheduled-powershell/

( 이 포럼 스레드를 통해 .)


나는이 같은 문제가 발생했습니다. 당신이 가면 내가 발견 작업 에서 작업 스케줄러 실행되고 powershell.exe를 스크립트를, 당신은 "클릭하면 사용자가 로그온 여부 실행을 "그 때 작업 실행 PowerShell 창을 표시하지 않습니다.


다음은 명령 행 인수 또는 별도의 실행기를 필요로하지 않는 접근 방식입니다. 시작할 때 창이 순간적으로 표시되므로 완전히 보이지 않습니다. 그러나 그것은 빨리 사라집니다. 괜찮다면, 이것은 탐색기에서 두 번 클릭하거나 시작 메뉴 바로 가기 (물론 시작 하위 메뉴 포함)를 통해 스크립트를 시작하려는 경우 가장 쉬운 방법이라고 생각합니다. 그리고 나는 그것이 외부 코드가 아닌 스크립트 자체의 코드의 일부라는 것을 좋아합니다.

이것을 스크립트 앞에 놓으십시오.

$t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $t -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)

백그라운드 스크립트를 실행할 때 PowerShell의 콘솔 화면을 숨기는 가장 좋은 방법은 이 코드 ( " Bluecakes "답변) 라고 생각 합니다.

백그라운드에서 실행해야하는 모든 PowerShell 스크립트의 시작 부분에이 코드를 추가합니다.

# .Net methods for hiding/showing the console in the background
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
function Hide-Console
{
    $consolePtr = [Console.Window]::GetConsoleWindow()
    #0 hide
    [Console.Window]::ShowWindow($consolePtr, 0)
}
Hide-Console

If this answer was help you, please vote to "Bluecakes" in his answer in this post.


I was having this problem when running from c#, on Windows 7, the "Interactive Services Detection" service was popping up when running a hidden powershell window as the SYSTEM account.

Using the "CreateNoWindow" parameter prevented the ISD service popping up it's warning.

process.StartInfo = new ProcessStartInfo("powershell.exe",
    String.Format(@" -NoProfile -ExecutionPolicy unrestricted -encodedCommand ""{0}""",encodedCommand))
{
   WorkingDirectory = executablePath,
   UseShellExecute = false,
   CreateNoWindow = true
};

Here's a one-liner:

mshta vbscript:Execute("CreateObject(""Wscript.Shell"").Run ""powershell -NoLogo -Command """"& 'C:\Example Path That Has Spaces\My Script.ps1'"""""", 0 : window.close")

Although it's possible for this to flash a window very briefly, that should be a rare occurrence.


I have created a small tool passing the call to any console tool you want to start windowless through to the original file:

https://github.com/Vittel/RunHiddenConsole

컴파일 한 후 실행 파일 이름을 "<targetExecutableName> w.exe"( "w"추가)로 바꾸고 원본 실행 파일 옆에 두십시오. 그런 다음 일반적인 매개 변수를 사용하여 eG powershellw.exe를 호출하면 창이 팝업되지 않습니다.

누군가가 생성 된 프로세스가 입력을 기다리고 있는지 확인하는 방법을 알고 있다면 솔루션을 포함 시키게되어 기쁩니다 :)


다음은 최소화 및 숨김을 포함하여 콘솔의 다양한 상태를 제어하는 ​​재미있는 데모입니다.

Add-Type -Name ConsoleUtils -Namespace WPIA -MemberDefinition @'
   [DllImport("Kernel32.dll")]
   public static extern IntPtr GetConsoleWindow();
   [DllImport("user32.dll")]
   public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'@

$ConsoleMode = @{
 HIDDEN = 0;
 NORMAL = 1;
 MINIMIZED = 2;
 MAXIMIZED = 3;
 SHOW = 5
 RESTORE = 9
 }

$hWnd = [WPIA.ConsoleUtils]::GetConsoleWindow()

$a = [WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.MAXIMIZED)
"maximized $a"
Start-Sleep 2
$a = [WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.NORMAL)
"normal $a"
Start-Sleep 2
$a = [WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.MINIMIZED)
"minimized $a"
Start-Sleep 2
$a = [WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.RESTORE)
"restore $a"
Start-Sleep 2
$a = [WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.HIDDEN)
"hidden $a"
Start-Sleep 2
$a = [WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.SHOW)
"show $a"

참고 URL : https://stackoverflow.com/questions/1802127/how-to-run-a-powershell-script-without-displaying-a-window

반응형