psobject results not as expected

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
russ
Posts: 12
Last visit: Mon Sep 19, 2016 6:03 pm

psobject results not as expected

Post by russ »

Hi all

I have the script below, running against PowerShell v2.0. Unfortunately I can't get the filtering working. for example the DriveLetter object returns all the WMI members and I only want the drive letter in that example. Plus the same thing is happening with other WMI queries, and I really don't know what I am doing wrong. The end goal is to run against multiple servers and export to CSV but I need to get the output fixed up first.

Thanks to all for your help in advance....

$ServerBuildresults = @()

$hosts = 'localhost'

ForEach($h in $hosts) {

$Services = Get-WmiObject Win32_Service
$LogicalDisks = Get-WmiObject Win32_Volume -Filter "DriveType='3'"
$WindowsVersion = Get-WmiObject Win32_OperatingSystem | Select-Object name
$OSVersion = Get-WmiObject Win32_OperatingSystem
$Domain = Get-WmiObject Win32_ComputerSystem
$LocalGroups = Get-WmiObject win32_group -Filter "LocalAccount='True'"

$obj = new-object psobject
$obj | Add-Member -membertype NoteProperty -Name DriveLetter -Value (($LogicalDisks | Where-object { $_.DriveLetter } ) | out-string).Trim()
$obj | Add-Member -membertype NoteProperty -Name DriveSize -Value (($LogicalDisks | Select-Object capacity | % {$_.Capacity / 1GB -as [int]}) | out-string).Trim()
$obj | Add-Member -membertype NoteProperty -Name Label -Value (($LogicalDisks.Label) | out-string).Trim()
$obj | Add-Member -membertype NoteProperty -Name FileSystem -Value (($LogicalDisks.FileSystem) | out-string).Trim()
$obj | Add-Member -membertype NoteProperty -Name BlockSize -Value (($LogicalDisks.Blocksize) | out-string).Trim()
$obj | Add-Member -membertype NoteProperty -Name Monitoring.health -Value ($Services | Where-Object { $_.Name -eq 'healthservice'})
$obj | Add-Member -membertype NoteProperty -Name Monitoring.ccmexec -Value ($Services | Where-Object { $_.Name -eq 'ccmexec' })
$obj | Add-Member -membertype NoteProperty -Name Monitoring.masvc -Value ($Services | Where-Object { $_.Name -eq 'masvc' })
$obj | Add-Member -membertype NoteProperty -Name SQLServices -Value (($Services | Where-Object { $_.StartName -notlike '*Local*' -and $_.Name -like '*SQL*' }) | out-string).Trim()
$obj | Add-Member -membertype NoteProperty -Name WindowsVersion -Value (($OSVersion.Caption) | out-string).Trim()
$obj | Add-Member -membertype NoteProperty -Name MemberDomain -Value (($Domain.Domain) | out-string).Trim()
$obj | Add-Member -membertype NoteProperty -Name LocalGroups.Caption -Value (($LocalGroups.Caption) | out-string).Trim()
$obj | Add-Member -membertype NoteProperty -Name LocalGroups.Name -Value (($LocalGroups.name) | out-string).Trim()

$ServerBuildresults += $obj

}

$ServerBuildresults
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: psobject results not as expected

Post by jvierra »

The first thing I see as that you are only querying the local server. To query remote servers you need to add the "Computername" parameter.

Fix your code and try again.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: psobject results not as expected

Post by jvierra »

Also I willnote that his will always return all drives:

Add-Member -membertype NoteProperty -Name DriveLetter -Value (($LogicalDisks | Where-object { $_.DriveLetter } ) | out-string).Trim()

It will not return a drive letter. It cannot ever return a drive letter because there are multiple drives. To understand this use this code only.

Get-WmiObject Win32_Volume -Filter "DriveType='3'"

There is no "LogicalDisk" parameter and the call will return more then one disk on many systems
User avatar
russ
Posts: 12
Last visit: Mon Sep 19, 2016 6:03 pm

Re: psobject results not as expected

Post by russ »

Hi

I have the Get-WmiObject Win32_Volume -Filter "DriveType='3'" defined at the variable layer.

Also if I run Get-WmiObject Win32_Volume | get-member I get the following...there is a DriveLetter Property.

TypeName: System.Management.ManagementObject#root\cimv2\Win32_Volume

Name MemberType Definition
---- ---------- ----------
AddMountPoint Method System.Management.ManagementBaseObject AddMountPoint(System.String Directory)
Chkdsk Method System.Management.ManagementBaseObject Chkdsk(System.Boolean FixErrors, System.Boolean VigorousIndexCheck, System.Boolean SkipFolderCycle, System.Boolean ForceDismount, System.Boolean RecoverBadSectors, System.Bool...
Defrag Method System.Management.ManagementBaseObject Defrag(System.Boolean Force)
DefragAnalysis Method System.Management.ManagementBaseObject DefragAnalysis()
Dismount Method System.Management.ManagementBaseObject Dismount(System.Boolean Force, System.Boolean Permanent)
Format Method System.Management.ManagementBaseObject Format(System.String FileSystem, System.Boolean QuickFormat, System.UInt32 ClusterSize, System.String Label, System.Boolean EnableCompression, System.UInt32 Version)
Mount Method System.Management.ManagementBaseObject Mount()
Reset Method System.Management.ManagementBaseObject Reset()
SetPowerState Method System.Management.ManagementBaseObject SetPowerState(System.UInt16 PowerState, System.String Time)
Access Property System.UInt16 Access {get;set;}
Automount Property System.Boolean Automount {get;set;}
Availability Property System.UInt16 Availability {get;set;}
BlockSize Property System.UInt64 BlockSize {get;set;}
BootVolume Property System.Boolean BootVolume {get;set;}
Capacity Property System.UInt64 Capacity {get;set;}
Caption Property System.String Caption {get;set;}
Compressed Property System.Boolean Compressed {get;set;}
ConfigManagerErrorCode Property System.UInt32 ConfigManagerErrorCode {get;set;}
ConfigManagerUserConfig Property System.Boolean ConfigManagerUserConfig {get;set;}
CreationClassName Property System.String CreationClassName {get;set;}
Description Property System.String Description {get;set;}
DeviceID Property System.String DeviceID {get;set;}
DirtyBitSet Property System.Boolean DirtyBitSet {get;set;}
DriveLetter Property System.String DriveLetter {get;set;}
DriveType Property System.UInt32 DriveType {get;set;}
ErrorCleared Property System.Boolean ErrorCleared {get;set;}
ErrorDescription Property System.String ErrorDescription {get;set;}
ErrorMethodology Property System.String ErrorMethodology {get;set;}
FileSystem Property System.String FileSystem {get;set;}
FreeSpace Property System.UInt64 FreeSpace {get;set;}
IndexingEnabled Property System.Boolean IndexingEnabled {get;set;}
InstallDate Property System.String InstallDate {get;set;}
Label Property System.String Label {get;set;}
LastErrorCode Property System.UInt32 LastErrorCode {get;set;}
MaximumFileNameLength Property System.UInt32 MaximumFileNameLength {get;set;}
Name Property System.String Name {get;set;}
NumberOfBlocks Property System.UInt64 NumberOfBlocks {get;set;}
PageFilePresent Property System.Boolean PageFilePresent {get;set;}
PNPDeviceID Property System.String PNPDeviceID {get;set;}
PowerManagementCapabilities Property System.UInt16[] PowerManagementCapabilities {get;set;}
PowerManagementSupported Property System.Boolean PowerManagementSupported {get;set;}
Purpose Property System.String Purpose {get;set;}
QuotasEnabled Property System.Boolean QuotasEnabled {get;set;}
QuotasIncomplete Property System.Boolean QuotasIncomplete {get;set;}
QuotasRebuilding Property System.Boolean QuotasRebuilding {get;set;}
SerialNumber Property System.UInt32 SerialNumber {get;set;}
Status Property System.String Status {get;set;}
StatusInfo Property System.UInt16 StatusInfo {get;set;}
SupportsDiskQuotas Property System.Boolean SupportsDiskQuotas {get;set;}
SupportsFileBasedCompression Property System.Boolean SupportsFileBasedCompression {get;set;}
SystemCreationClassName Property System.String SystemCreationClassName {get;set;}
SystemName Property System.String SystemName {get;set;}
SystemVolume Property System.Boolean SystemVolume {get;set;}
__CLASS Property System.String __CLASS {get;set;}
__DERIVATION Property System.String[] __DERIVATION {get;set;}
__DYNASTY Property System.String __DYNASTY {get;set;}
__GENUS Property System.Int32 __GENUS {get;set;}
__NAMESPACE Property System.String __NAMESPACE {get;set;}
__PATH Property System.String __PATH {get;set;}
__PROPERTY_COUNT Property System.Int32 __PROPERTY_COUNT {get;set;}
__RELPATH Property System.String __RELPATH {get;set;}
__SERVER Property System.String __SERVER {get;set;}
__SUPERCLASS Property System.String __SUPERCLASS {get;set;}
ConvertFromDateTime ScriptMethod System.Object ConvertFromDateTime();
ConvertToDateTime ScriptMethod System.Object ConvertToDateTime();
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: psobject results not as expected

Post by jvierra »

Sorry yes but you will still likely return all drives only from the local machine.
User avatar
russ
Posts: 12
Last visit: Mon Sep 19, 2016 6:03 pm

Re: psobject results not as expected

Post by russ »

The machines are handled by the ForEach.

I'm still trying to figure out what I'm returning all members and not just the DriveLetter like I would like to.

Cheers.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: psobject results not as expected

Post by jvierra »

To return only DriveLetter for all returned drives:

(Get-WmiObject Win32_Volume -Filter "DriveType='3'").DriveLetter
User avatar
russ
Posts: 12
Last visit: Mon Sep 19, 2016 6:03 pm

Re: psobject results not as expected

Post by russ »

jvierra wrote:
(Get-WmiObject Win32_Volume -Filter "DriveType='3'").DriveLetter
doesn't work, my variable is empty. I'm on PS v2.0 remember.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: psobject results not as expected

Post by jvierra »

$x=Get-WmiObject Win32_Volume -Filter "DriveType='3'"
$x.DriveLetter
User avatar
MikeFRobbins
Posts: 8
Last visit: Thu Apr 11, 2019 1:55 pm

Re: psobject results not as expected

Post by MikeFRobbins »

Where-Object in the first line of adding members in your script doesn't do anything. Changing it to ForEach-Object at least narrows it down to only the drive letter property:

Code: Select all

$obj | Add-Member -membertype NoteProperty -Name DriveLetter -Value (($LogicalDisks | ForEach-Object { $_.DriveLetter } ) | out-string).Trim()
Mike F Robbins
Microsoft MVP, SAPIEN MVP
This topic is 7 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked