title: Convert PowerShell output to Python and store it in variable
body: <p>Sorry in advance for silly question.</p>
  <p>I am trying to write simple python script that runs virtual machine using PowerShell commands.</p>
  <p>I have got a little problem with converting output from PowerShell command to a variable in Python.</p>
  <p>The idea is:</p>
  <p>I launch virtual machine, after that I check the state of it and, if the state is Running - start all the activity.</p>
  <p>It is not a problem to do it in PowerShell, I wrote all the commands (launch VM, check state, if statement etc), but it is a problem to do it from py file.</p>
  <p>My script looks like that:</p>
  <pre><code>import subprocess
  import time
  import os
  
  
  class Utils(object):
      def __init__(self):
          self.ps_exec = r&quot;C:\path\PsExec.exe&quot;
          self.power_shell = r&quot;C:\path\powershell.exe&quot;
  
      def vm(self, apply_vm_script):
          subprocess.Popen([self.power_shell, apply_vm_script])
  
  util = Utils()
  
  def turn_on_vm(vm_name, checkpoint_name):
      apply_vm_script = 'Invoke-Command -Computername name -ScriptBlock ' \
                             '{ Get-VM ''&quot;' + vm_name + '&quot;'' | get-vmsnapshot -Name ' + '&quot;' + checkpoint_name + '&quot; | ' \
                             'Restore-VMSnapshot -Confirm:$false ; Start-VM -Name ''&quot;' + vm_name + '&quot;''}'
      util.vm(apply_vm_script)
      time.sleep(10)
      
  def check_if_vm_on(vm_name):
      check_vm_script = 'Invoke-Command -Computername name -ScriptBlock { Get-VM | where {$_.Name -eq ' + vm_name + ' } | where { $_.State -eq &quot;Running&quot; } | select Name,State}'
      util.vm(check_vm_script)
      time.sleep(3)
      
  def test():    
      turn_on_vm('VM_name', 'checkpoint_name')
      if(check_if_vm_on('VM_name')):
          Do my activity
      
  def main():
      test()
      
  if __name__ == '__main__':
      main()
  </code></pre>
  <p>Also, I can perform all if actions in PowerShell, but also can't convert bool output into Python:</p>
  <pre><code>if($State -like '*Running*') { Write-Output &quot;True&quot; }
  </code></pre>
  <p>State was defined earlier, no problem with variables.</p>
  <p>Any ideas how to solve it?</p>
  <p>Thank you in advance!!!</p>