성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>(WMI 쿼리를 위한) PowerShell 문자열 escape 처리</h1> <p> 지난 글에,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# - WMI 논리 디스크가 속한 물리 디스크의 정보를 얻는 방법 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13228'>https://www.sysnet.pe.kr/2/0/13228</a> </pre> <br /> 설명한 내용을 <a target='tab' href='https://www.sysnet.pe.kr/2/0/13228#16472'>dsscrolls 님이 다시 쿼리를 구성한 답글</a>을 남기셨는데요, 그래서 가벼운 마음으로 PowerShell로 해당 쿼리를 그대로 구현해 다시 덧글을 달려고 했더니... 이게 쉽지가 않습니다. ^^;<br /> <br /> 우선, 첫 번째 쿼리부터 문제입니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Select Antecedent from Win32_LogicalDiskToPartition where Dependent="Win32_LogicalDisk.DeviceID="C:"" </pre> <br /> 위의 쿼리를 PowerShell에 전달하면 문자열 내에 사용한 인용 부호 때문에,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > PS> Get-WmiObject -Query "SELECT Antecedent FROM Win32_LogicalDiskToPartition WHERE Dependent="Win32_LogicalDisk.DeviceID="C:""" Get-WmiObject : A positional parameter cannot be found that accepts argument 'Win32_LogicalDisk.DeviceID=C:"'. At line:1 char:1 + Get-WmiObject -Query "SELECT Antecedent FROM Win32_LogicalDiskToParti ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand </pre> <br /> 오류가 발생합니다. 당연히, 저 인용 부호(")를 escape 처리해야 하는데요, <br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Escaping in PowerShell ; <a target='tab' href='https://www.rlmueller.net/PowerShellEscape.htm'>https://www.rlmueller.net/PowerShellEscape.htm</a> </pre> <br /> PowerShell에서 문자열 내부의 인용 부호는 2개로 escape 처리하거나, backtick을 이용할 수 있습니다. 하지만, 그건 단순히 escape 시킨 것에 불과하고, WMI 쿼리 등의 문법과는 맞지 않아 이런 오류가 발생하게 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // 겹따옴표를 2개 연속으로 escape 처리 PS> Get-WmiObject -Query "SELECT Antecedent FROM Win32_LogicalDiskToPartition WHERE Dependent=""Win32_LogicalDisk.DeviceID=""C:""""" Get-WmiObject : Invalid query "SELECT Antecedent FROM Win32_LogicalDiskToPartition WHERE Dependent="Win32_LogicalDisk.DeviceID="C:""" At line:1 char:1 + Get-WmiObject -Query "SELECT Antecedent FROM Win32_LogicalDiskToParti ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ManagementException + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand" </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // 겹따옴표를 backtick으로 escape 처리 PS> Get-WmiObject -Query "SELECT Antecedent FROM Win32_LogicalDiskToPartition WHERE Dependent=`"Win32_LogicalDisk.DeviceID=`"C:`"`"" Get-WmiObject : Invalid query "SELECT Antecedent FROM Win32_LogicalDiskToPartition WHERE Dependent="Win32_LogicalDisk.DeviceID="C:""" At line:1 char:1 + Get-WmiObject -Query "SELECT Antecedent FROM Win32_LogicalDiskToParti ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ManagementException + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand </pre> <br /> 2가지 처리 방식 모두 저렇게 오류가 발생합니다. 왜냐하면, Dependent 조건 절의 내용이 다음과 같이 평가되기 때문입니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Dependent="Win32_LogicalDisk.DeviceID="C:"" </pre> <br /> 따라서 이런 경우에는 홑따옴표를 곁들여서 다음과 같이 해결할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Get-WmiObject -Query "SELECT Antecedent FROM Win32_LogicalDiskToPartition WHERE Dependent=`"Win32_LogicalDisk.DeviceID='C:'`"" </pre> <br /> 일단, 저렇게 해결할 수 있었지만, 한 단계 더 중첩된 경우가 나오면 그때는 어떻게 escape 처리를 해야 할지 모르겠습니다. ^^; (아마도 WMI 쿼리에서 그런 경우는 없을 거라 생각되지만.)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 어쨌든, 문자열 내부에 인용 부호가 2중첩 되는 것까지는 해결할 수 있었습니다. 이를 바탕으로 "<a target='tab' href='https://www.sysnet.pe.kr/2/0/13228'>C# - WMI 논리 디스크가 속한 물리 디스크의 정보를 얻는 방법</a>" 글에서 나온 쿼리를 마저 포팅해 보겠습니다.<br /> <br /> 우선 위의 쿼리에서 나온 Antecedent 값은,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > PS> <span style='color: blue; font-weight: bold'>$result = Get-WmiObject -Query "SELECT Antecedent FROM Win32_LogicalDiskToPartition WHERE Dependent=`"Win32_LogicalDisk.DeviceID='C:'`""</span> PS> <span style='color: blue; font-weight: bold'>$result.Antecedent</span> \\TESTPC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #1" </pre> <br /> 이후의 "SELECT * FROM Win32_DiskDriveToDiskPartition ..." 쿼리에 바로 사용할 수 없습니다. 아쉽게도 cimv2 경로가 들어가서는 안 되고, 그 이후의 값을 Dependent 절에 다음과 같이 설정해야 합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Get-WmiObject -Query "Select * from Win32_DiskDriveToDiskPartition where Dependent=Win32_DiskPartition.DeviceID="Disk #1, Partition #1"" ==> escape 처리 Get-WmiObject -Query "Select * from Win32_DiskDriveToDiskPartition where Dependent=`"Win32_DiskPartition.DeviceID='Disk #1, Partition #1'`"" </pre> <br /> 저 결과로 물리 드라이브 경로를 얻을 수 있는데요,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > PS> <span style='color: blue; font-weight: bold'>$result = Get-WmiObject -Query "Select * from Win32_DiskDriveToDiskPartition where Dependent=`"Win32_DiskPartition.DeviceID='Disk #1, Partition #1'`""</span> PS> <span style='color: blue; font-weight: bold'>$result.Antecedent</span> \\TESTPC\root\cimv2:Win32_DiskDrive.DeviceID="\\\\.\\PHYSICALDRIVE1" </pre> <br /> 이 값 역시 cim 경로가 나오는데 그것을 제외한 부분을 Win32_DiskDrive 쿼리에 전달하면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > PS> Get-WmiObject -Query "Select SerialNumber from Win32_DiskDrive where DeviceID='\\\\.\\PHYSICALDRIVE1'" __GENUS : 2 __CLASS : Win32_DiskDrive __SUPERCLASS : __DYNASTY : __RELPATH : __PROPERTY_COUNT : 1 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : <span style='color: blue; font-weight: bold'>SerialNumber : 0025_38A1_0100_2C3D</span>. PSComputerName : </pre> <br /> 모든 과정이 완료됩니다. ^^ 이것을 약간 더 자동화하면 다음과 같이 정리할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > $result = Get-WmiObject -Query "SELECT Antecedent FROM Win32_LogicalDiskToPartition WHERE Dependent=`"Win32_LogicalDisk.DeviceID='C:'`"" $result = $result.Antecedent.SubString($result.Antecedent.IndexOf(':') + 1) $result = Get-WmiObject -Query "Select * from Win32_DiskDriveToDiskPartition where Dependent='$result'" $result = $result.Antecedent.SubString($result.Antecedent.IndexOf('=') + 1) Get-WmiObject -Query "Select SerialNumber from Win32_DiskDrive where DeviceID=$result" </pre> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
9487
(왼쪽의 숫자를 입력해야 합니다.)