본문 바로가기

Server Story..../PowerShell

파워쉘의 출력에 대하여


커맨드라인과 파워셀의 출력 차이점


커멘드라인과 파워쉘은 언뜻 살펴보기에는 동일한 방식으로 동작하는 것 처럼 보이지만 실제로는 입출력 대상에 큰 차이점이 있다, 먼저 이부분을 살펴보자

커멘드라인의 출력


커맨드라인은 에러와 기본출력 두가지를 가지고 있다, 그냥 단순하게 생각해서 문자열을 출력한다고 생각하면된다

dir > result.txt => 디렉토리 목록을 조회하여 result.txt로 저장
copy result.txt con => result.txt를 con로 카피
dir | more => 디렉토리 목록을 출력하는데 페이지 단위로 나누어서 출력한다
copy con test.txt => 콘솔로 입력받은 내용을 text.txt로 저장한다

위 예제에서 보이는것 처럼 모든 입출력은 문자열로 출력, 입력된다

파워쉘의 출력


ls 즉 get-childitem의 alias를 실행 해보면 보기에는 dir과 같은 내용이 출력되는 것 같지만

실제로는 get-childitem으로 얻어진 객체(.NETFRAMEWORK상의 객체) 리스트가 만들어지고 각 객체들의 프로퍼티중 Console로 출력되도록 설정된 프로퍼티가 출력되는 것이다

즉 cmd의 dir은 화면에 보여지는 정보가 전부이며 그 형식은 문자열이다
하지만 파워쉘은 눈에 보이는 출력이 전부가 아니라 각 객체의 프로퍼티중 보여지도록 설정된 부분만 보여지는 것이다

객체의 타입 살펴보기



사실 출력만 객체가 아니라 대부분 모든것이 객체이며 그 형식과 멤버를 가지고 있다


PS C:\Users\dirtyvictory> "test".gettype()
IsPublic IsSerial Name                                     BaseType                                           

-------- -------- ----                                     --------                                           
True     True     String                                   System.Object                                     



PS C:\Users\dirtyvictory> (get-item .\Desktop).gettype()
IsPublic IsSerial Name                                     BaseType                                           

-------- -------- ----                                     --------                                           
True     True     DirectoryInfo                            System.IO.FileSystemInfo                            

모든게 객체라니..

눈에 보이지 않는 모든 속성을 살펴보기


$item = get-item test.txt
get-member -inputobject $item

위 형식으로 특정파일을 get-item 으로 얻어온 객체를 $item이란 변수에 저장한 이후
get-member -inputobject로 출력해보면 실제로 눈에 보이는것 이외에 모든 멤버들을 볼 수 있다


PS C:\temp> Get-Member -InputObject $item


   TypeName: System.IO.FileInfo
Name                      MemberType     Definition                                                                                           
 

----                      ----------     ----------                                                                                            Mode                      CodeProperty   System.String Mode{get=Mode;}                                                                         AppendText                Method         System.IO.StreamWriter AppendText()                                                                 
 

CopyTo                    Method         System.IO.FileInfo CopyTo(string destFileName), System.IO.FileInfo CopyTo(string destFileName, bool...Create                    Method         System.IO.FileStream Create()                                                                         CreateObjRef              Method         System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)                                       CreateText                Method         System.IO.StreamWriter CreateText()                                                                   Decrypt                   Method         System.Void Decrypt()                                                                                 Delete                    Method         System.Void Delete()                                                                                 
 

Encrypt                   Method         System.Void Encrypt()                                                                                 Equals                    Method         bool Equals(System.Object obj)                                                                       
 

GetAccessControl          Method         System.Security.AccessControl.FileSecurity GetAccessControl(), System.Security.AccessControl.FileSe...GetHashCode               Method         int GetHashCode()                                                                                   
 

GetLifetimeService        Method         System.Object GetLifetimeService()                                                                   
 

GetObjectData             Method         System.Void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Seria...GetType                   Method         type GetType()                                                                                       
 

InitializeLifetimeService Method         System.Object InitializeLifetimeService()                                                           
 

MoveTo                    Method         System.Void MoveTo(string destFileName)                                                               Open                      Method         System.IO.FileStream Open(System.IO.FileMode mode), System.IO.FileStream Open(System.IO.FileMode mo...OpenRead                  Method         System.IO.FileStream OpenRead()                                                                       OpenText                  Method         System.IO.StreamReader OpenText()                                                                   
 

OpenWrite                 Method         System.IO.FileStream OpenWrite()                                                                     
 

Refresh                   Method         System.Void Refresh()                                                                                 Replace                   Method         System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName), System.IO...SetAccessControl          Method         System.Void SetAccessControl(System.Security.AccessControl.FileSecurity fileSecurity)                 ToString                  Method         string ToString()                                                                                   
 

PSChildName               NoteProperty   System.String PSChildName=test.txt                                                                   
 

PSDrive                   NoteProperty   System.Management.Automation.PSDriveInfo PSDrive=C                                                    PSIsContainer             NoteProperty   System.Boolean PSIsContainer=False                                                                   
 

PSParentPath              NoteProperty   System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\temp                              PSPath                    NoteProperty   System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\test.txt                           PSProvider                NoteProperty   System.Management.Automation.ProviderInfo PSProvider=Microsoft.PowerShell.Core\FileSystem             Attributes                Property       System.IO.FileAttributes Attributes {get;set;}                                                       
 

CreationTime              Property       System.DateTime CreationTime {get;set;}                                                             
 

CreationTimeUtc           Property       System.DateTime CreationTimeUtc {get;set;}                                                           
 

Directory                 Property       System.IO.DirectoryInfo Directory {get;}                                                             
 

DirectoryName             Property       System.String DirectoryName {get;}                                                                   
 

Exists                    Property       System.Boolean Exists {get;}                                                                         
 

Extension                 Property       System.String Extension {get;}                                                                       
 

FullName                  Property       System.String FullName {get;}                                                                       
 

IsReadOnly                Property       System.Boolean IsReadOnly {get;set;}                                                                  LastAccessTime            Property       System.DateTime LastAccessTime {get;set;}                                                           
 

LastAccessTimeUtc         Property       System.DateTime LastAccessTimeUtc {get;set;}                                                          LastWriteTime             Property       System.DateTime LastWriteTime {get;set;}                                                              LastWriteTimeUtc          Property       System.DateTime LastWriteTimeUtc {get;set;}                                                           Length                    Property       System.Int64 Length {get;}                                                                            Name                      Property       System.String Name {get;}                                                                             BaseName                  ScriptProperty System.Object BaseName {get=if ($this.Extension.Length -gt 0){$this.Name.Remove($this.Name.Length -...VersionInfo               ScriptProperty System.Object VersionInfo {get=[System.Diagnostics.FileVersionInfo]::GetVersionInfo($this.FullName);} 



모든 리다이렉션과 출력, 입력은 객체로 이루어진다 문자열을 입력하더라도 결국 문자열은 .NETFRAMEWORK의 string객체로 바뀌어 처리된다

이후 파워쉘의 리다이렉션과 스크립트상의 문법에 대하여 알아보자


'Server Story.... > PowerShell' 카테고리의 다른 글

파워쉘로 리모트에 gui화면 실행  (0) 2013.03.29
파워쉘의 기본문법1  (0) 2013.03.29
스크립트 파일을 실행하기 위한 설정  (0) 2013.03.29
파워쉘 실행  (0) 2013.03.29
파워쉘 비밀번호 암호화  (0) 2013.03.29