远程执行Powershell命令并使用wmi python打印输出

2024-09-27 04:20:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想远程执行powershell命令并获取输出。 命令是“(New Object-cMicrosoft.Update.Session).createUpdateSearch().Search(“IsInstalled=0”).Updates |选择标题“。
我试过了

import wmi
try:
  connection = wmi.WMI(ip, user=username, password=password)
  print("connection is established")
  connection.Win32_Process.Create(CommandLine='powershell.exe /c (New-Object -c Microsoft.Update.Session).CreateUpdateSearcher().Search("IsInstalled=0").Updates|Select Title  > C:\output.txt')
except:
  print("connection failed")

Tags: 命令newsearch远程objectsessionupdatepassword
1条回答
网友
1楼 · 发布于 2024-09-27 04:20:18

你需要注意转义字符。试试这个:

import wmi
try:
  connection = wmi.WMI(ip, user=username, password=password)
  print("connection is established")
  connection.Win32_Process.Create (CommandLine=" powershell.exe -command \" & {New-Object -c Microsoft.Update.Session).CreateUpdateSearcher(). Search(\"IsInstalled=0\").Updates|Select Title}\"  > C:\output.txt")
except:
  print("connection failed")

相关问题 更多 >

    热门问题