0

'''$Session = New-Object -ComObject "Microsoft.Update.Session"

$Searcher = $Session.CreateUpdateSearcher()

$historyCount = $Searcher.GetTotalHistoryCount()

$Result = $Searcher.QueryHistory(0, $historyCount) | Select-Object Date,

@{name="Operation"; expression={switch($_.operation)

{

1 {"Installation"};

2 {"Uninstallation"};

3 {"Other"}

}}},

@{name="Status"; expression={switch($_.resultcode)

{

1 {"In Progress"};

2 {"Succeeded"};

3 {"Succeeded With Errors"};

4 {"Failed"};

5 {"Aborted"}

}}},

@{name="Update"; expression={IF($_.Title.tostring() -match "(.*?)"){$matches[0].replace('(','').replace(')','')}}},

Title $Result | Where{$_.Date -gt (Get-Date).AddDays(-14)} | Sort-Object Date | Select Date,Operation,Status,Update,Title | Export-Csv -NoType "$Env:userprofile\Desktop\WindowsUpdates.csv"| Format-Table

this is script save to notepad i want to get output using python

import subprocess

p = subprocess.run('F:\getwindowupdate.ps1', shell=True)

print(p.stdout)

this is only open the notepad file how to execute this powershell script using python

Raju Hub
  • 1
  • 1
  • plz help i m new in the python . how to get output from this script using python . – Raju Hub Sep 12 '20 at 11:20
  • i want to get information about window patches ,is install or not . but in python i cant do this . give any idea how to get window update information , its update or not . thank you so much – Raju Hub Sep 12 '20 at 11:22
  • Does this answer your question? [Run PowerShell function from Python script](https://stackoverflow.com/questions/14508809/run-powershell-function-from-python-script) – Svirin Sep 12 '20 at 11:27

1 Answers1

1

You can pass a command to PowerShell and retrieve the output in your python script.

Step 1 Write a PowerShell script

    Write-Host 'Hello, World!'

save it as script.ps1

PS: This will output

    Hello, World!

Step 2 Write a python script and call your PowerShell script from there and retrieve the output

    import sys
    import subprocess

    cmd = ["PowerShell", "-ExecutionPolicy", "Unrestricted", "-File", ".\\script.ps1"]
    ec = subprocess.call(cmd)
    print("Powershell returned: {0:d}".format(ec))

This will output:

    Hello, World!
    Powershell returned: 0
Souhailhimself
  • 221
  • 1
  • 10