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

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.

Related

Unable to use calculated properties with wmi

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

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.

Finding path of process on remote machine

You can use the following in Powershell to obtain the full path to where a specific process is running:
Get-Process | where{$_.Name -like "*iexplore*"} | Select Path
If I want to find this path for a service on a remote machine, I thought I could just utilise the following:
Get-Process -ComputerName $MyServer | where{$_.Name -like "*iexplore*"} | Select Path
However, this doesn't return anything. I can see that I can find the service itself with some details on current usage etc. but I cannot find the path for where the .exe file is located. (I also noticed I cannot see how many CPUs the process is using either).
Is there a way to find the path for the process?
Get-Process missing this, but you can use WMI:
Get-WmiObject -Class win32_process -ComputerName $MyServer -Filter 'name like "%iexplore%"' | select path

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.

Powershell: Win32_BIOS Serialnumber validity

In a system i'm making I am saving files from each computer to a directory which has the name of the serialnumber i collect from this code:
$serialnr = gwmi -computer $compname Win32_BIOS | ForEach {$_.SerialNumber}
When I run this code on my computer I get the serialnumber, however when the same code is run on a friends computer the output reads: "FFFFFFF" (I am not sure about the number of F's).
What I'm hoping is that "FFFFFFF" is like a standard output if I cannot get the true serialnumber, then I could atleast use that as a parameter for another command to make "fix".
Q: Anybody has some information about the possible outputs from this command, or possible fixes?
All ideas and all help will be greatly appreciated!
I don't understand the purpose of your ForEach. Am I missing something?
$serialnr = gwmi win32_bios | select -Expand serialnumber | out-string
will give you the serial number.