I've got a notepad.exe started in my session :
gwmi -Query "Select CommandLine from Win32_Process where CommandLine='C:\Windows\system32\notepad.exe'"
gives
Get-WmiObject : Demande non valide
Au niveau de ligne : 1 Caractère : 5
+ gwmi <<<< -Query "Select CommandLine from Win32_Process where CommandLine='C:\Windows\system32\notepad.exe'"
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
I test :
gwmi -Query "Select CommandLine from Win32_Process where CommandLine='C:\\Windows\\system32\\notepad.exe'"
It gives nothing
gwmi -Query "Select CommandLine from Win32_Process where CommandLine LIKE '%C:\\Windows\\system32\\notepad.exe%'"
Works perfectly
__GENUS : 2
__CLASS : Win32_Process
__SUPERCLASS :
__DYNASTY :
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
CommandLine : "C:\Windows\system32\notepad.exe"
Perhaps it's a trouble with wildcards caracters between PowerShell and WMI, but anyone can help me make filter CommandLine='C:\Windows\system32\notepad.exe' working
The value of the CommandLine property contains quotes, so they need to be escaped as well.
A working, but horrible string is:
gwmi -Query "Select * from Win32_Process where CommandLine = '`"c:\\windows\\system32\\notepad.exe`"'"
You need to include the quotes, but as I can't recall how to escape them in WQL, I would do it in PSH:
gwmi -class Win32_Process -filter "CommandLine like '`"C:\\Windows\\system32\\notepad.exe`"'"
Filter expression is in double quotes, with the string argument to LIKE in single quotes. The double quotes that are part of that argument need to be quoted from PowerShell.
Get-Process | ? {$_.Path -eq 'C:\Windows\system32\notepad.exe'}
Get-Process | ? {$_.processname -eq 'notepad'}
Related
I'm struggling to create a WQL query for SCCM as I'm really new and rarely use it in a complex manner.
My goal is to list 3 things : Computer name - Display Name ("Google Chrome") - Display Version (of that Google Chrome entry)
I'm starting with this :
$SiteCode = "XXX"
$SiteServer = "XXX"
$query = #"
select SMS_R_System.Name, SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName, SMS_G_System_ADD_REMOVE_PROGRAMS.Version
from SMS_R_System
inner join SMS_G_System_ADD_REMOVE_PROGRAMS
on SMS_G_System_ADD_REMOVE_PROGRAMS.ResourceID = SMS_R_System.ResourceId
where SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName = "Google Chrome"
"#
Get-WmiObject -namespace root\sms\site_$SiteCode -computer $SiteServer -query $query
The output is a bit strange to me :
__GENUS : 2
__CLASS : __GENERIC
__SUPERCLASS :
__DYNASTY : __GENERIC
__RELPATH :
__PROPERTY_COUNT : 2
__DERIVATION : {}
__SERVER : XXX
__NAMESPACE : root\sms\site_PR1
__PATH :
SMS_G_System_ADD_REMOVE_PROGRAMS : System.Management.ManagementBaseObject
SMS_R_System : System.Management.ManagementBaseObject
PSComputerName : XXX
What am I missing here? Again i'm really new at this so I must be missing a key part of the logic.
If I remove :
, SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName, SMS_G_System_ADD_REMOVE_PROGRAMS.Version
The query works and shows me all the computers that have Chrome installed:
__GENUS : 2
__CLASS : SMS_R_System
__SUPERCLASS : SMS_Resource
__DYNASTY : SMS_BaseClass
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {SMS_Resource, SMS_BaseClass}
__SERVER : XXX
__NAMESPACE : root\sms\site_XXX
__PATH :
Name : PXXX
PSComputerName : XXX
but I want those 2 properties too, not just the computer name so I can confirm the version numbers.
Much appreciated.
To expand on my comments in an alternate way to handle the problem at hand:
$wmiParams = #{
Namespace = 'root/sms/site_XXX'
ComputerName = 'XXX'
Query = #'
SELECT SMS_R_System.Name, SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName, SMS_G_System_ADD_REMOVE_PROGRAMS.Version
FROM SMS_R_System
INNER JOIN SMS_G_System_ADD_REMOVE_PROGRAMS
ON SMS_G_System_ADD_REMOVE_PROGRAMS.ResourceID = SMS_R_System.ResourceId
WHERE SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName = "Google Chrome"
'#
}
$smsResults = Get-WmiObject #wmiParams
foreach ($object in $smsResults) {
[pscustomobject]#{
ComputerName = $object['SMS_R_System']['Name']
DisplayName = $object['SMS_G_System_ADD_REMOVE_PROGRAMS']['DisplayName']
Version = $object['SMS_G_System_ADD_REMOVE_PROGRAMS']['Version']
}
}
The answer is to simply "expand" the dictionaries like pointed out by #TheIncorrigible.
So here is how I ended up doing it using the Name/Expression method in Select-Object:
$query = #"
select SMS_R_System.Name, SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName, SMS_G_System_ADD_REMOVE_PROGRAMS.Version
from SMS_R_System
inner join SMS_G_System_ADD_REMOVE_PROGRAMS
on SMS_G_System_ADD_REMOVE_PROGRAMS.ResourceID = SMS_R_System.ResourceId
where SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName = "Google Chrome"
"#
Get-WmiObject -namespace root\sms\site_$SiteCode -computer $SiteServer -query $query | Select-Object #{name='ComputerName';expression={$_.SMS_R_System.Name} }, #{name='DisplayName';expression={$_.SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName} }, #{name='Version';expression={$_.SMS_G_System_ADD_REMOVE_PROGRAMS.Version} }
I followed the Scripting Guy instructions from Microsoft but even with this I still get the same error Scripting Guy article
here is my script:
$p2=Get-CimInstance -N root\cimv2\power -Class win32_PowerPlan -Filter "ElementName = 'Balanced'"
Invoke-CimMethod -InputObject $p2-MethodName Activate
which results in:
Invoke-CimMethod : This method is not implemented in any class
At line:1 char:1
+ Invoke-CimMethod -InputObject $p2 -MethodName Activate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (Win32_PowerPlan...2-f694-41f0...):CimInstance) [Invoke-CimMethod], CimException
+ FullyQualifiedErrorId : HRESULT 0x80041055,Microsoft.Management.Infrastructure.CimCmdlets.InvokeCimMethodCommand
I cant seem to find answers I have looked in a few locations, I have seen people start to run into this a few months ago but I could not find an answer any advice would be appreciated
my end goal is to write a script where I import a powerplan and then activate it I have the import part working fine it just this last bit. $p contains my imported plan I used $p2 on a default plan for testing purposes.
cheers and thank you in advance for any advice you can offer
I found this code for changing the Power Schema.
Get-CimInstance -N root\cimv2\power -Class win32_PowerPlan | select ElementName, IsActive | ft -a
$p = gwmi -NS root\cimv2\power -Class win32_PowerPlan -Filter "ElementName ='Ultimate Performance'"
$p.Activate()
Get-CimInstance -N root\cimv2\power -Class win32_PowerPlan | select ElementName, IsActive | ft -a
pause
It works on most flavors of Windows O/S. I get this error sometimes.
"Exception calling "Activate" : "This method is not implemented in any class " At line:1 char:1 + $p2.Activate() + ~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WMIMethodException –"
I like to know if there is a way to use PowerShell with WMI to set the MSNdis_currentPacketFilter
PS > Get-WmiObject -class "MSNdis_CurrentPacketFilter" -NameSpace "root\WMI" -Filter "InstanceName='Intel(R) Ethernet Server Adapter I350-T2'"
__GENUS : 2
__CLASS : MSNdis_CurrentPacketFilter
__SUPERCLASS : MSNdis
__DYNASTY : MSNdis
__RELPATH : MSNdis_CurrentPacketFilter.InstanceName="Intel(R) Ethernet Server Adapter I350-T2"
__PROPERTY_COUNT : 3
__DERIVATION : {MSNdis}
__SERVER : HYPERV88
__NAMESPACE : root\WMI
__PATH : \\HYPERV88\root\WMI:MSNdis_CurrentPacketFilter.InstanceName="Intel(R) Ethernet Server Adapter
I350-T2"
Active : True
InstanceName : Intel(R) Ethernet Server Adapter I350-T2
NdisCurrentPacketFilter : 15
PSComputerName : HYPERV88
And I would like to change the NdisCurrentPacketFilter value from 15 to 47.
I tried
Set-WMIInstance -Path ... -Arguments #{NdisCurrentPacketFilter=47}
But, got error. Thanks in advance!
===== added 04/17/2015
Here were the commands I tried:
$p=$(Get-WmiObject -class "MSNdis_CurrentPacketFilter" -NameSpace "root\WMI" -Filter "InstanceName='Intel(R) Ethernet Server Adapter I350-T2'").__Path
Write-Host $p
\\HYPERV88\root\WMI:MSNdis_CurrentPacketFilter.InstanceName="Intel(R) Ethernet Server Adapter I350-T2"
Set-WmiInstance -Path $p -Arguments #{NdisCurrentPacketFilter=47}
and error (not sure why it said command not found, but command was valid)
Set-WmiInstance : Not found
At line:1 char:1
+ Set-WmiInstance -Path $p -Arguments #{NdisCurrentPacketFilter=47}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Set-WmiInstance], ManagementException
+ FullyQualifiedErrorId : SetWMIManagementException,Microsoft.PowerShell.Commands.SetWmiInstance
And I tried this:
$o = Get-WmiObject -class "MSNdis_CurrentPacketFilter" -NameSpace "root\WMI" -Filter "InstanceName='Intel(R) Ethernet Server Adapter I350-T2'"
Write-Host $o
\\HYPERV88\root\WMI:MSNdis_CurrentPacketFilter.InstanceName="Intel(R) Ethernet Server Adapter I350-T2"
Set-WMIInstance -class "MSNdis_CurrentPacketFilter" -InputObject $o -Arguments #{NdisCurrentPacketFilter=47}
Set-WmiInstance : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ Set-WMIInstance -class "MSNdis_CurrentPacketFilter" -InputObject $o -Arguments # ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-WmiInstance], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.SetWmiInstance
I think you are having this issue as according to scriptinternals that value is read-only.
NdisCurrentPacketFilter
Data type: integer/usint32
Access type: Read-only
I found similar information here as well. You should have wrote your error here as well. It might have added context to your issue and it is a best practice when asking questions.
Disclaimer: I know nothing about the class. Just wanted to see if anyone posted details of the class parameters.
I am using Powershell Version 2 on windows 7. I need to run the following command: get-windowsoptionalfeature
but when I run: get-command -Verb Get, the get-windowsoptionalfeature is not listed and as a result when I enter the command I receive an error stating that "get-windowsoptionalfeature" is not recognized as the name of a cmdlet, function.
Am I missing a dll or something?
get-windowsoptionalfeature is only applicable to Windows 8 & Server 2012.
try this in powershell console
PS C:\>$feature=Get-WmiObject -query "select * from Win32_OptionalFeature"
Now $feature is array of object of type ManagementObject.
to prove it try
PS C:\> $feature[0]
this is what I get.
__GENUS : 2
__CLASS : Win32_OptionalFeature
__SUPERCLASS : CIM_LogicalElement
__DYNASTY : CIM_ManagedSystemElement
__RELPATH : Win32_OptionalFeature.Name="OEMHelpCustomization"
__PROPERTY_COUNT : 6
__DERIVATION : {CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER : TTL001343
__NAMESPACE : root\cimv2
__PATH : \\TTL001343\root\cimv2:Win32_OptionalFeature.Name="OEMHelpCustomization"
Caption :
Description :
InstallDate :
InstallState : 2
Name : OEMHelpCustomization
Status :
You can get query specific objects as well.
example:
PS C:\>$feature=Get-WmiObject -query "select * from Win32_OptionalFeature where name = 'RemoteServerAdministrationTools-Roles-AD-Powershell'"
Now this will be single object not an array.
There's the Client Manager Module:
http://archive.msdn.microsoft.com/PSClientManager
I am trying to call the Rename method on the Win32_ComputerSytem class using Invoke-WMI method. Using this syntax works fine
(gwmi win32_ComputerSystem).Rename("NEWNAME")
This also works fine for demo purposes
Invoke-WmiMethod -path win32_process -Name create -ArgumentList notepad
However, when i try the following, I get an error
11 > Invoke-WmiMethod -path win32_computersystem -Name Rename -ArgumentList IwasRenamed
Invoke-WmiMethod : Invalid method Parameter(s)
At line:1 char:17
+ Invoke-WmiMethod <<<< -path win32_computersystem -Name Rename -ArgumentList IwasRenamed
+ CategoryInfo : InvalidOperation: (:) [Invoke-WmiMethod], ManagementExcepti
on
+ FullyQualifiedErrorId : InvokeWMIManagementException,Microsoft.PowerShell.Commands.
InvokeWmiMethod
What am I missing?
You need to specify an instance of the class Win32_ComputerSystem using the Path parameter:
PS C:\Users\ben> $path = "Win32_ComputerSystem.Name='OLDNAME'"
PS C:\Users\ben> Invoke-WmiMethod -Name Rename -Path $path -ArgumentList "NEWNAME"
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
Which is functionally equivalent to the gwmi Rename syntax that you referred to. This syntax implicitly retrieves an instance of the class Win32_ComputerSystem to call the method on:
PS C:\Users\ben> (gwmi win32_computersystem).rename("NEWNAME")
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
Here's another cool syntax:
PS C:\Users\ben> ([wmi]"Win32_ComputerSystem.Name='OLDNAME'").Rename("NEWNAME")
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
The Rename method takes three parameters. I'm guessing Invoke-WmiMethod uses reflection to call the method, so you have to specify all three parameters. Try this:
[String]$newName = "IWasRenamed"
[String]$password = $null
[String]$username = $null
Invoke-WmiMethod -Path Win32_ComputerSystem -Name Rename -ArgumentList $newName, $password, $username