Powershell Get
I am trying to run Powershell command from batch file test.bat. I am actually calling this command from Python Popen not from test.bat.(I am using test.bat just for validation)
powershell.exe (Get-NetAdapterAdvancedProperty -Name "SLOT 1" -DisplayName "Jumbo Packet").DisplayValue
Same command works with out second argument -DisplayName "Jumbo Packet" from python and batch
Error I get when I use second argument is below:
Get-NetAdapterAdvancedProperty : A positional parameter cannot be found that accepts argument '1'. At line:1 char:2 + (Get-NetAdapterAdvancedProperty -Name SLOT 1 -DisplayName:Jumbo Packet).DisplayV ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-NetAdapterAdvancedProp erty], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Get-NetAdapterAdvanc edProperty
* But if I run the same command from Powershell window I get my expected result.
(Get-NetAdapterAdvancedProperty -Name "SLOT 1" -DisplayName "Jumbo Packet").DisplayValue
I am new to Powershell.Thanks for your help
Python code:
iface="SLOT 1"
cmd= 'powershell.exe (Get-NetAdapterAdvancedProperty -Name "'+ iface +'" -DisplayName "Jumbo Packet").DisplayValue'
conn.modules.os.popen(cmd).read()
Brackets have special meaning in command. Escape them with a caret. The quotes may also need escaping with a caret.
But why the indirection. PS uses WMI. WMI is also available via COM. Python can do COM.
This is VBScript pulling out nics. You should be able to do this in any language that supports COM (nearly all).
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!.rootcimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_NetworkAdaptor")
For Each objItem in colItems
msgbox objitem.name
Next
Put double quotes around the whole command string (to make the entire command a string for CMD). Use single quotes inside the double-quoted string to define the nested strings for PowerShell:
powershell.exe "Get-NetAdapterAdvancedProperty -Name 'SLOT 1' -DisplayName 'Jumbo Packet').DisplayValue"
Alternatively escape nested double quotes with backslashes:
powershell.exe "Get-NetAdapterAdvancedProperty -Name "SLOT 1" -DisplayName "Jumbo Packet").DisplayValue"
However, in general it's less troublesome to simply put PowerShell commands in a script and run that script with PowerShell:
powershell.exe -File "C:pathtoyour.ps1"
链接地址: http://www.djcxy.com/p/37880.html
上一篇: Powershell中模块功能的参数
下一篇: Powershell获取