다음과 같이 실행중인 powershell 스크립트가 있습니다.
> $client = New-Object System.Net.Sockets.TCPClient('x.x.x.x',443);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);[Sytem.IO.File]::AppendAllText('<path\to\file>', $data)};$client.Close();
연결하고, 스트림을 읽고, 파일에 쓰지만 종료하지는 않습니다. 그런 다음 Ctrl + C를 사용하려고하는데 작동하지 않습니다.
파일 읽기가 끝나면 종료하고 Linux 에서처럼 Ctrl + C를 사용하여 종료되지 않는 이유를 알고 싶습니다.
PowerShell 세션에 있고이 코드를 실행하면 현재 세션에있는 것입니다. 코드가 완료되면 코드를 시작한 곳에서 Powershell 프롬프트로 돌아갑니다. 따라서 세션이 아니라 코드가 닫힙니다.
CRTL + C는 코드가 실행 된 후 PowerShell을 닫지 않고 종료합니다.
소켓 연결 개체 만 닫습니다. 시작한 위치에서 PowerShell 세션을 종료하지 않습니다.
# Open the TCP socket
$client = New-Object System.Net.Sockets.TCPClient('x.x.x.x',443)
# Initialize the stream
$stream = $client.GetStream()
[byte[]]$bytes = 0..65535 |
ForEach{0}
while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0)
{
# Collect the data
$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i)
# Write data to file
[Sytem.IO.File]::AppendAllText('<path\to\file>', $data)
}
# Clear the stream
$stream.Flush()
# Close the TCP socket
$client.Close()
# Code complete, exiting PowerShell
Exit
PowerShell 세션에서 포 그라운드 코드 블록을 실행하려는 경우 Start-Process를 사용할 수 있습니다.
예를 들면 :
# Start a new foreground PowerShell session to run this code
Start-Process -FilePath PowerShell -ArgumentList Get-Date
코드가 완료되면 세션을 자동 종료하므로이 작업을 종료하거나 CRTL + C 작업을 수행 할 필요가 없습니다.
Start-Process 옵션 및 PowerShell 명령 줄 옵션을 활용하는 방법에 대한 도움말 파일을 참조하십시오.
# Get specifics for a module, cmdlet, or function
(Get-Command -Name Start-Process).Parameters
(Get-Command -Name Start-Process).Parameters.Keys
Get-help -Name Start-Process -Examples
Get-help -Name Start-Process -Full
Get-help -Name Start-Process -Online
Powershell /?
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다