How to get installed certificates on a windows machine remotely with Python? -
i wish query remotely windows machines, , certificate store details (installed certificates names, expiry , thumbprints). i'm using python 2.7.
how can done? didn't have luck pywin32
or wmi
packages.
sadly, easiest solution spwan powershell process , request data through invoke-command
cmdlet:
def run_powershell(scriptblock, cwd=os.getcwd()): log.debug("running powershell block:\r\n%s", scriptblock) log.debug("current directory: %s\r\n" % cwd) psproc = subprocess.popen([r'c:\windows\system32\windowspowershell\v1.0\powershell.exe', '-executionpolicy', 'bypass', '-noprofile', '-c', '-',], cwd=cwd, stdin=subprocess.pipe, stdout=subprocess.pipe, stderr=subprocess.pipe) stdoutdata, stderrdata = psproc.communicate(scriptblock) if stdoutdata: log.debug("script output:\r\n%s" % stdoutdata) elif not stderrdata: log.debug("script completed succssfully (no stdout/stderr).") if stderrdata: log.error("script error:\r\n%s" % stderrdata) return stdoutdata, stderrdata def get_certificates(server_list, location="localmachine", store="my"): cmd = ''' $sb = { ls cert:\%s\%s | select subject,thumbprint } $servers = '%s' | convertfrom-json invoke-command -computername $servers -scriptblock $sb -authentication negotiate | convertto-json -depth 999 ''' % (location, store, json.dumps(server_list)) stdoutdata, stderrdata = run_powershell(cmd) return json.loads(stdoutdata)
Comments
Post a Comment