egkmor
egkmor

Reputation: 152

How to perform a WMI query in a Windows Docker container with many quotes?

I am having a hard time performing WMI queries in a docker container. In my host computer, I can perform the query below without any problems.

Get-WmiObject -Namespace "root\cimv2" -query "select HotfixID from Win32_QuickFixEngineering where HotFixID = `"KB4571756`"" 

However, when I try to perform the same query in a docker container, I am constantly getting errors such as:

docker exec my_container powershell "Get-WmiObject -Namespace `"root\cimv2`" -query `"select HotfixID from Win32_QuickFixEngineering where HotFixID = `"KB4571756`"`""

Note that I tried to escape every quotation inside the command. The error for the above command is:

The string is missing the terminator: ".
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx
   ception
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

I tried playing with different quotes such as " or ' but had no success for any of them. I know that I can perform the same query with docker interactive mode without issues but this queries need to be integrated in such a way that I can perform them with Stdin and read the output from Stdout from another code that I wrote.

Upvotes: 1

Views: 1003

Answers (1)

marsze
marsze

Reputation: 17104

Two possible ways:

A) Use single quotes for strings inside your command

docker exec my_container powershell "Get-WmiObject -Namespace 'root\cimv2' -Query 'select HotfixID from Win32_QuickFixEngineering where HotFixID = ''KB4571756'''"

B) Use EncodedCommand

$command = { Get-WmiObject -Namespace "root\cimv2" -Query "select HotfixID from Win32_QuickFixEngineering where HotFixID = 'KB4571756'" }
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command.ToString())
$encodedCommand = [Convert]::ToBase64String($bytes)

docker exec powershell -EncodedCommand $encodedCommand

Upvotes: 1

Related Questions