0

Using PowerShell, I want to include the computer name with some of the returned data of the WMI query. I've studied custom columns - need something like that - but I do not know how to pass the computer name into the next loop to be included in the resulting table. For example:

Get-WmiObject -Class Win32_LogicalDisk -ComputerName MailServer01

Actually, I'm in another look where $_ is the computer name:

Get-WmiObject -Class Win32_LogicalDisk -ComputerName $_

This works great, but I want to include the ComputerName ($_) as part of the final output or report. Any ideas?

4 Answers 4

1

The computername is already there (as SystemName). It just isn't part of the default display properties.

Run

Get-WmiObject -Class Win32_LogicalDisk -ComputerName MailServer01 | format-list *

to see all of the properties of the returned objects.

Sign up to request clarification or add additional context in comments.

1 Comment

Huh, didn't know that was there. Better answer than mine, fewer commands to execute.
1

The "__Server" property will always be available for the get-wmiobject objects

Also note:

"Beginning in Windows PowerShell 3.0, the __Server property of the object that Get-WmiObject returns has a PSComputerName alias. This makes it easier to include the source computer name in output and reports."

http://technet.microsoft.com/en-us/library/hh849824.aspx

Comments

0

Try this

Get-WMiObject -Class Win32_LogicalDisk -ComputerName $_ | Add-Member -MemberType NoteProperty -Name ComputerName -Value $_ -PassThru

Then you can output or manipulate your data anyway you see fit, and the properties you add via Add-Member will be available on that object as long as it exists.

Docs for the cmdlet are here

Comments

0

Yeah, I didn't realize there were more properties. Works great: __Server was the property I needed:

  $myServers = @("server1", "server2", "server3")

  "" > space.txt

  $myServers | foreach-object { 
     write-host "Server: $_"
     Get-WmiObject -Class Win32_LogicalDisk -ComputerName $_ | ? { $_.DeviceID -notmatch "  [AR]"} | Select -Property __Server, DeviceID, @{Name=’FreeSpaceMB’;Expression={$_.FreeSpace/1MB} } | Format-Table -AutoSize  >> c:\space.txt

  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.