Solved

problem with a query wmi

Posted on 2013-05-29
9
333 Views
Last Modified: 2013-05-30
A txt file monitoring.env contains a list for exclude windows disk of monitoring

k:\\data\\sql\\:DISK:OFF
k:\\data1\\sql\\:DISK:OFF

With the folowing code, only the first line k:\\data\\sql\\:DISK:OFF is read :
$MONITORING = gci -Recurse ($CONF_INST_CONFIG) -in *.env
$EXCLUDE_DISK=get-content $MONITORING | where-object {$_.contains(":DISK:OFF")} |Foreach-Object {$_ -replace ":DISK:OFF", ""} -ErrorAction silentlycontinue
if ($EXCLUDE_DISK -ne $null) {
$LIST_EXCLUDE_DISK =  [string]::join('', $EXCLUDE_DISK) + ''
}
$LIST_DISK_1 = Get-WmiObject Win32_Volume -Filter "DriveType!='5' and not label like '%OS%'" |where-object {$_.name -notmatch $LIST_EXCLUDE_DISK}

How can I resolve this?

Thanks
0
Comment
Question by:bibi92
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 40

Expert Comment

by:footech
ID: 39206377
I think it's reading everything just fine.  It's what you're trying to do afterward that is having problems.
What are you trying to do with the line below?
$LIST_EXCLUDE_DISK =  [string]::join('', $EXCLUDE_DISK) + ''
That just gives you a result like "k:\\data\\sql\\k:\\data1\\sql\\".

And what are you trying to filter with the Where-Object in
$LIST_DISK_1 = Get-WmiObject Win32_Volume -Filter "DriveType!='5' and not label like '%OS%'" |where-object {$_.name -notmatch $LIST_EXCLUDE_DISK}  ?
0
 

Author Comment

by:bibi92
ID: 39206738
With list_exclude_disk Disks are separeted with space.
With where object, I try to build list_disk_1 without Disks in list_exclude_disk
Thanks
0
 
LVL 40

Expert Comment

by:footech
ID: 39207137
If you wanted them separated by a space you could do either of the following:
$LIST_EXCLUDE_DISK =  [string]::join(' ', $EXCLUDE_DISK)
$LIST_EXCLUDE_DISK =  $EXCLUDE_DISK -join " "

Open in new window

But I don't see what that gets you.  You don't have any volumes with the name "k:\\data\\sql\\ k:\\data1\\sql\\" do you?  Or even "k:\\data\\sql\\"?  I'm quite certain you're just looking to get the drive letter like "k:\".

If I'm correct you can use the following:
$MONITORING = gci -Recurse ($CONF_INST_CONFIG) -in *.env
$EXCLUDE_DISK = @(Select-String $MONITORING -pattern "^([A-Z]:\\).+:DISK:OFF" | ForEach-Object {$_.matches} | ForEach-Object {$_.groups[1].value} | Select -Unique)
$LIST_DISK_1 = Get-WmiObject Win32_Volume -Filter "DriveType!='5' and not label like '%OS%'" | Where-Object {$EXCLUDE_DISK -notcontains $_.name}

Open in new window

BTW, in my testing if you want to return drives that don't have a label you would have to adjust the WMI filter to include "label = NULL".
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:bibi92
ID: 39207842
For answer :
But I don't see what that gets you.  You don't have any volumes with the name "k:\\data\\sql\\ k:\\data1\\sql\\" do you?  Or even "k:\\data\\sql\\"?  I'm quite certain you're just looking to get the drive letter like "k:\".

--> I have the volumes k:\\data\\sql\\ k:\\data1\\sql\\ but I want to exclude them from $LIST_DISK_1.

I have tested the code.
The result from $EXCLUDE_DISK is k:\ and not k:\\data\\sql\\ k:\\data1\\sql\\.

Thanks
0
 
LVL 40

Assisted Solution

by:footech
footech earned 500 total points
ID: 39208278
OK, if you have the volumes "k:\\data\\sql\\" and "k:\\data1\\sql\\", the below will exclude them.  I just changed the matching pattern slightly.
$MONITORING = gci -Recurse ($CONF_INST_CONFIG) -in *.env
$EXCLUDE_DISK = @(Select-String $MONITORING -pattern "^([A-Z]:\\.+):DISK:OFF" | ForEach-Object {$_.matches} | ForEach-Object {$_.groups[1].value} | Select -Unique)
$LIST_DISK_1 = Get-WmiObject Win32_Volume -Filter "DriveType!='5' and not label like '%OS%'" | Where-Object {$EXCLUDE_DISK -notcontains $_.name}

Open in new window

0
 

Author Comment

by:bibi92
ID: 39208294
Sorry unless I'm mistaken I don't see any difference between the code in posted ID: 39208278 and the code in posted  ID: 39207137.

Thanks
0
 

Author Comment

by:bibi92
ID: 39208295
Sorry I'm tired, the difference is ([A-Z]:\\.+)
0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 39208343
Correct.  If that works for you, it could even be simplified to "^(.+):DISK:OFF$".
0
 

Author Closing Comment

by:bibi92
ID: 39208592
Thanks
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question