所以我的问题是,如何在python中运行PowerShell脚本,并从python中传递变量,然后让python运行PowerShell脚本,再从PowerShell脚本中传递特定变量给python脚本。
我所拥有的是每个代码单独工作。
正如你所看到的,我从一个
.ini
文件中传入隐藏变量,然后从那里我试图把
compList
传给Powershell。
我不确定我是否运行了正确的命令来调用PowerShell并在我的Python脚本中传递args
preferences = configparser.ConfigParser()
preferences.read('config.ini')
hostName = preferences.get('Directory', 'hostName')
port = preferences.get('Directory', 'port')
serviceName = preferences.get('Directory', 'serviceName')
usr = preferences.get('Credentials', 'userName')
pswrd = preferences.get('Credentials', 'password')
compList = preferences.get('ComputerList', 'compList')
compList = list(compList.split(","))
p = subprocess.Popen(["powershell.exe", r"C:\Users\pecorgx\TestPS1.ps1", 'arg1 @("compList")'], stdout=sys.stdout)
p.communicate()
My PowerShell Script:
$ArrComputers = $arg1
Clear-Host
foreach ($Computer in $ArrComputers)
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
$computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
$computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
$computerCPU = get-wmiobject Win32_Processor -Computer $Computer
$hdds = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
"-------------------------------------------------------"
"Manufacturer: " + $computerSystem.Manufacturer
"Model: " + $computerSystem.Model
"Serial Number: " + $computerBIOS.SerialNumber
"CPU: " + $computerCPU.Name
foreach ($computerhdd in $hdds) { "HDD Drive Letter: " + $computerhdd.DeviceID + "HDD Capacity:" + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB" + " HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) }
"RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
"Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
"User logged In: " + $computerSystem.UserName
"Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
"-------------------------------------------------------"
我认为我的传递是正确的,但我想让Powershell传递回我的Python脚本
$computerSystem.TotalPhysicalMemory/1GB
$computerSystem.Manufacturer
$computerSystem.Model
$computerBIOS.SerialNumber
$computerCPU.Name
我想把这个var $computerHDD.Size/1GB
传到一个列表中,然后再传给我的python脚本
编辑:我决定只使用.txt
来传递命令。我做了$myOBJ = "" | Select "computer"
,然后做了$myOBJ.computer = [string](....)
,其中(...)
是我想在脚本运行后传回的东西。
我很想知道在没有第三方帮助的情况下可以使用的解决方案。