Get-WmiObject from AD OU - powershell

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.

Related

PowerShell - Pass array as an optional parameter value

I'm pretty new to PowerShell.
I'm trying to pass an array of server names as an optional parameter value to get the last bootup time for a list of servers.
Example 1
$serverList = #('server1"', '"server2"', '"server3"', '"server4"', '"server5"')
Get-CimInstance -ComputerName $serverList -ClassName win32_operatingsystem | Select-Object csname, lastbootuptime
Example 2
Get-CimInstance -ComputerName server1,server2,server3,server4,server5 -ClassName win32_operatingsystem | Select-Object csname, lastbootuptime
In example 1, I get an error that says it can't connect to the
servers.
In example 2, it works.
I'm likely missing a key piece of fundamental knowledge as to why what I'm doing wasn't working. What am I missing?
Thanks for the help.
In the first example you are over-doing things with the quotes. (also server1 has an ending double quote, but no starting double quote..)
By putting the servers inside single quotes ', the text inside it is taken literally, so you are feeding the cmdlet with names like "server2", so including the double-qoute characters.
These quotes obviously don't belong to the server name.
BTW: Not an error, but you don't need the #() when creating the server names array.
This would be a better way of setting up your string array, where you can use either single or double quote characters, but not both:
$serverList = 'server1', 'server2', 'server3', 'server4', 'server5'
Get-CimInstance -ComputerName $serverList -ClassName win32_operatingsystem | Select-Object csname, lastbootuptime
You have also noticed that when used as parameters to a cmdlet, you don't even need the quotes, and the elements are interpreted as strings, as long as they do not contain space characters:
Get-CimInstance -ComputerName server1,server2,server3,server4,server5 -ClassName win32_operatingsystem | Select-Object csname, lastbootuptime

Get-WMIObject include computer name

I'm trying out a script to go grab installed software on servers remotely. Problem is I want it to output certain attribs including the computer name but I can't seem to figure out how to get the name inserted.
Here is what I have so far...
$servers = Get-QADComputer -SearchRoot "OU=servers,OU=mydomain:-),DC=COM" | Select Name
...which works fine of course. Then...
$servers | % {Get-WMIObject -Class Win32Reg_AddREmovePrograms} | select Displayname,Version,InstallDate,PSComputerName
... which provides the full list of software installed on all servers in that OU but the PSComputerName becomes MY COMPUTER (the computer I run the query from - not the computername of the system being queried). The goal is to have the servername the software is installed on on each line item of software. I've asked professor Google and don't seem to see anything helpful (or anything that I understand anyway).
Hope this makes sense. semi-amateur PS script writer so hopefully this is easy for you guys. Thanks in advance for your help
Your command:
Get-WMIObject -Class Win32Reg_AddREmovePrograms
Does not specify computer to query, so it just query computer command being executed on. Thus PSComputerName display MY COMPUTER, as MY COMPUTER is computer being queried. You have to specify -ComputerName parameter to Get-WMIObject cmdlet to query specific computer. And -ComputerName parameter accept array of computer names, so you can put array of computer names to it instead of using ForEach-Object cmdlet and query one computer at time.
Since the object returned from the WMI call doesn't contain the computer you made the request on, you need to include it yourself from include your ForEach-Object (%) block. You could use Add-Member to add it yourself, then do your Select-Object outside like you're doing now:
$servers | % {
Get-WMIObject -Class Win32Reg_AddREmovePrograms -ComputerName $_ |
Add-Member -MemberType NoteProperty -Name ComputerName -Value $_ -PassThru
} | select Displayname,Version,InstallDate,ComputerName
Another way is to move the Select-Object to inside the block and do it within there, by creating a new property on the fly with a hashtable:
$servers | % {
Get-WMIObject -Class Win32Reg_AddREmovePrograms -computername $_ |
Select-Object Displayname,Version,InstallDate,#{Name='ComputerName';Expression={$_}}
}

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.

gwmi Win32_UserAccount freezes PowerShell

So this is the core of my "Get Local Admin Account Info" script:
Get-WmiObject -Class Win32_UserAccount -ComputerName $server -Credential $Creds -Filter "Domain='$env:USERDOMAIN' AND SID LIKE '%500'"
Running this freezes both PS Console and the ISE, requiring Task Manager to kill them.
I understand based on other questions here on stackoverflow that querying Win32_UserAccount can result in a lot of data being pulled over the network. But I would think that specifying a single computer and a single SID would filter that volume of data down to a miniscule amount. I certainly don't have this problem querying other WMI Objects on remote servers.
Am I missing something?
EDIT: This freezes PS too:
gwmi win32_useraccount -computername $server "SID LIKE '%500'"
Need to specify the server name as the domain in the query. I.e "Domain='$server' AND SID LIKE '%500'"
This may still perform poorly though. See http://msdn.microsoft.com/en-us/library/aa394507(v=vs.85).aspx
Could also try filtering on LocalAccount property I.e "LocalAccount='$True'"
If you could do domain = and name =, it would be fast, because those are the indexes.
# fast
Get-WmiObject Win32_UserAccount -Filter "Domain='$env:USERDOMAIN' AND
name = 'js2010'"
Strangely, specifying localaccount by itself is fast, but not with a name or sid too.
# slow (AD joined)
Get-WmiObject Win32_UserAccount -Filter "localaccount = 'true' and
name = 'js2010'"
# fast
Get-WmiObject Win32_UserAccount -Filter "localaccount = 'true'" |
where name -eq js2010
# fast
get-wmiobject win32_useraccount -filter "localaccount = 'true'" |
where sid -like *500
# fast
get-wmiobject win32_useraccount -filter "domain = '$env:computername' and
sid like '%500'"
There's also a get-localuser:
# fast
Get-Localuser | where sid -like *500

Powershell - Pumping Locally Installed Applications into Listbox

So I'm trying to get all the locally installed applications and put them into a listbox. However, I'm having some problems. Whenever I use the below code:
$prog = (get-wmiobject win32_product -computer $current_hostname.text -property Name).Name
foreach($program in $prog)
{
$program_list_current.items.add($program)
}
Whats returned in the listbox is the applications plus some other text/string at the beginning of each app. In some cases, where '-property Name' is replaced with ' | select Name', nothing is returned at all.
I'm using the above syntax because the below code works (which gets the AD groups for a machine and puts each group into a listbox):
$processnames_t = (Get-ADComputer -Identity $current_hostname.text -Property MemberOf).MemberOf
foreach ($processname in $processnames_t)
{
[void]$AD_list_current.Items.Add($processname)
}
Any ideas as to why it works for the AD groups but not the installed apps? Maybe something to do with the nature of get-wmiobject?
Thanks
I couldn't really repro the issue you are seeing. But, for the program names, you can replace
$prog = (get-wmiobject win32_product -computer $current_hostname.text -property Name).Name
with
$prog = get-wmiobject win32_product -computer $current_hostname.text | Select -Exp Name
Select Name alone won't work as it returns the object and not a string. In this case, to use it as a listbox item, you need the string.