Solved

problem with a query wmi

Posted on 2013-05-29
9
320 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
  • 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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