Unable to use calculated properties with wmi - powershell

I'm trying to capture a few details regarding some processes on a windows server and as I understand it, performance counters are the way to go. Since the processes can be stopped/started at times, I wanted to make it a bit dynamic and give me the details of the running processes and with the commandline as well. Also, the process can have multiple instances running on the server with different command lines and process names. e.g. T-Process#1 , T-Process#2,T-Process#3.
Looking into WMI, I can't get everything from a single class and have to rely on two(Win32_Process and Win32_PerfFormattedData_PerfProc_Process). Since I can't use a single query to get details from two classes, I was going to use powershell and found a few posts about how to do so but I can't seem to make it work.
Get-CimInstance -Namespace root/cimv2 -Class Win32_process -Filter 'Name like "%T%"' -Property Name,Commandline,ProcessId | select-object Name,Commandline,ProcessId,#{Name='ProcessName';expression={(Get-CimInstance -CimSession (Get-CimSession | Where-Object ComputerName -eq $_.PSComputerName) -Namespace root/cimv2 -ClassName Win32_PerfFormattedData_PerfProc_Process -Filter 'IDProcess = $_.ProcessId' -Property Name).Name}}
I expected the code above to give me the process name(to be used in the performance counter), and the name, commandline and I do get everything but not the process name which is a calculated property.
Any help on this would be great. I've tried a few different iterations of the expressions and nothing seems to work.
Thanks,
Karan

Related

Case sensitive usage of Get-WmiObject -Filter

I know powershell defaults to case insensitive, but in my case i need a case sensitive filtering on Wmi objects properties.
Currently I do:
Get-WmiObject -Class Win32_Product -Filter "Vendor like '%MYNAME%'"
but this also finds 'MyName' and 'myname' occurences.
How is it possible to Filter case sensitive?
I heard of the -clike filter, but it seems not to work inside those -Filter switch, e.g. "Vendor clike '%MYNAME%' throws an error.
I chose to use the -Filter option over the Get-WmiObject Win32_Service | where-object {...} clause, because I read that the where-clause would transfer ALL information via network to the remote machine and THEN starts filtering.
By using the -Filter switch, only the already filtered information is transferred.
Because I use that command to obtain installed product details via remote execution on several machines, that's why I do not want to go with the Where-clause unless there is no other way.
thanks guys!
best regards
MRT

Powershell query - Querying computer for listed Programs and filtering

I'm hoping you all can help me with this. I'm trying to use PowerShell to see what software is currently installed on a machine, but i want results of specific software not the entire library of installed software if that makes sense? Note - I'm new to PowerShell and I'm doing my best to learn and learn how to use/create scripts. I've tried Google!
I'm using the following to query a computer on the local network and i get a result of all software listed.
Get-Wmiobject -class Win32_product -computername "PC1" | Select-Object name,version
Could someone help me filter the results for specific software? I also want to see the version of the software.
Any help would be appreciated! It'll certainly help towards my learning.
There are two ways to do it.
if you know what application you looking for, you can use the following:
Get-Wmiobject -class Win32_product -filter "name='Application Name'" | Select-Object name,version
Take note that it must be the same (no case sensitive)
if you are looking for a pattern you can use the following (which is longer to execute):
Get-Wmiobject -class Win32_product | Select-Object name,version | where {$_.Name -like "*Google*"}
you can add the -ComputerName parameter if you need.
let me know how it worked for you.

Powershell - check if a CD is in CD-ROM drive

Is this possible?
My first guess would be something like:
C:> Get-WmiObject Win32_CDROMDrive
But when I tried this, it only tells me Caption, Drive, Manufacturer,VolumeName
No information on whether or not there is a CD in the disc drive.
You can get this information by
(Get-WMIObject -Class Win32_CDROMDrive -Property *).MediaLoaded
You can see what properties are available for that WMI class by
Get-WMIObject -Class Win32_CDROMDrive -Property * | Get-Member
and more detailed documentation from
Get-WMIHelp -Class Win32_CDROMDrive
In general, you will find that liberal use of the Get-Help, Get-Member, Get-Command, and Get-WMIHelp cmdlets will provide you with a great deal of information, and possibly eliminate the need to ask questions like this here and wait for an answer that may or may not come.

Get-WmiObject from AD OU

I have a simple problem that I can't seem to work through. I need to know what servers are still running server 2008/R2.
I know that Win32_OperatingSystem's Name property contains the information that I'm looking for. I would like to be able to run Get-WmiObject against a collection of servers in an OU.
There are two problems that I'm having:
I can't figure out how to redirect the output of Get-ADComputer to something that Get-WmiObject -ComputerName can use. I think Get-ADComputer is outputting objects of type Microsoft.ActiveDirectory.Management.ADComputer, and Get-WmiObject is looking for type System.Management.ManagementObject. Here's what I came up with but it doesn't appear to work.
Get-WmiObject Win32_OperatingSystem -ComputerName (Get-ADComputer -filter * -SearchBase "OU=Member Servers,DC=Company,DC=Com" | select #{L="ComputerName";e={$_."name"}}) -Property name, csname | select csname, name | Format-Table -AutoSize
My temp workaround: I was able to create a CSV that contains the list of server names. I was able to use the CSV to run Get-WmiObject against. However, the OU contains "dead" servers. So when I try to run Get-WmiObject using the CSV-list of servers that came from AD there are connection timeouts and PowerShell waits a period of time to see if the dead server will respond. This really slows down the operation & we are working to clean this up. Until that happens, Is there a way to only pass the server names that pass a Test-Connection to Get-WmiObject?
Get-WmiObject win32_operatingsystem -ComputerName (Get-Content C:\Users\user1\Desktop\Servers.csv) -Property name, csname | select csname, name | Format-Table -AutoSize
Pick the name component first then it will pass it to the next pipeline object (select -object)
Get-WmiObject Win32_OperatingSystem -ComputerName ((Get-ADComputer -filter * -SearchBase "OU=Member Servers,DC=Company,DC=Com").Name)
Note: -ComputerName: accepts a string object so you cannot pass a base type object directly to that.

Any idea why I can't seem to execute two Get-WMIObject commands in the same Scriptblock?

I'm having a hell of a time with what seems like an utterly bizarre issue. I'm basically trying to inventory a bunch of workstations via WinRM and the Invoke-Command PowerShell cmdlet, but I'm running into grief when I try to execute more than one Get-WMIObject call at a time.
In this case, I'm specifically trying to get the model and serial number of the workstations and pipe them out to a CSV, making it important that the two commands are executed in the same Scriptblock, giving me something very similar to the below, basically.
Invoke-Command -ScriptBlock { Get-WmiObject Win32_ComputerSystem | Select Model ; Get-WmiObject win32_SystemEnclosure | Select serialnumber } -ComputerName (Get-ADComputer -Server [domain I care about] -filter 'name -Like "[types of computers I care about]"' | Select-Object -expand Name)
Even when run locally, Get-WmiObject Win32_ComputerSystem | Select Model ; Get-WmiObject win32_SystemEnclosure | Select serialnumber only returns the first command. I've tried swapping them around and the first command executes, while the second does not. Some of my colleagues report that it works just fine for them, others see the same result as me, and it doesn't seem to be a version issue, as one of the people for whom this works is running the same version of PowerShell as I am. (5.0.10240.16384)
Screenshot below of a few different command combinations. Anyone have any idea what's going on here?
If you change the list in the select-object cmdlets so both include the properties output by each, you will get the result you want (I think).
Invoke-Command -ScriptBlock { Get-WmiObject Win32_ComputerSystem | Select Model,SerialNumber ; Get-WmiObject win32_SystemEnclosure | Select model,serialnumber } -ComputerName localhost
That will get you output to the screen which should include all of the info. If you just want objects you can just capture the output and see that all of the properties are there.
$objects=Invoke-Command -ScriptBlock { Get-WmiObject Win32_ComputerSystem | Select Model ; Get-WmiObject win32_SystemEnclosure | Select serialnumber } -ComputerName (Get-ADComputer -Server [domain I care about] -filter 'name -Like "[types of computers I care about]"' | Select-Object -expand Name)
If you execute $objects | format-list * you will see that you have two objects, one with a Model and one with a SerialNumber.
Outputting different objects to a single CSV is another issue altogether. You get columns in the CSV based on the properties in the first object, so you will lose the SerialNumber property in the CSV.