문제

I am trying to list output names from win32_share and only include those which do not contain a $. I have the following code written, but it passes everything. How can I list those shares which do not contain a $ in their name?

$winshare = Get-WmiObject -computername 127.0.0.1 -class win32_share | Select-Object name
$share = $null

foreach ($i in $winshare.name) { 
if ($i -notcontains '$') {$share = $i + ',' + $share}else{}
}

write-host $share

Thanks!

도움이 되었습니까?

해결책

Use:

if (!$i.Contains('$'))

-notcontains matches the full string

다른 팁

Since you are querying the localmachine, you could leave out the Computername variable.

Additionally, if you are trying to query for administrative shares, its better to look for share names that ends with the $ character. This can be done with the String.EndsWith() test.

Get-WmiObject -Class win32_share | Where-Object { -not $_.Name.EndsWith('$') }

An alternative would be to use the -notmatch operator:

if ($i -notmatch '\$')

If you only want to get visible shares, you should try

Get-WmiObject -Class Win32_Share -Filter "NOT Name like '%$'"

The filter is run before you get the results, which should be faster, and you can skip the if-test later.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top