.NET Framework - how to compare in this situation

Asked By IT Staff
20-Jul-07 02:19 AM
$dictionary = @{}
$dictionary["v1.0."] = "Version 1.0"
$dictionary["v1.1"] = "Version 1.1"
$dictionary["v2.0"] = "Version 2.0"
$dictionary["v3.0"] = "Version 3.0"


$strComputer = $args[0]
$dir = dir "\\$strcomputer\admin$\microsoft.net\Framework"
$folders = $dir | where {$_.mode -match "d" -and $_.name -like "v*"} |
select name

foreach ($folder in $folders)
{
$folder
}
==============================================================

I want to do a $folder comparison with the $dictionary hash tables. If a
$folder name exists in  $dictionary hash tables, it will display the
$dictionary.values

How do it do that ?
PsIsContainer
(1)
RichS
(1)
StrComputer
(1)
Siddaway
(1)
Dreeschkind
(1)
Containment
(1)
Directory
(1)
Warranty
(1)
  Edengundam replied...
20-Jul-07 03:05 AM
I think -contains operator will accomplish ur problem and I believe there
are more effective solution for this problem.

just like :

PS C:\Documents and Settings\Eden> $hash = @{}
PS C:\Documents and Settings\Eden> @($hash.keys) -contains 'a'
False
PS C:\Documents and Settings\Eden> $hash['a'] = "test"
PS C:\Documents and Settings\Eden> @($hash.keys) -contains 'a'
True
PS C:\Documents and Settings\Eden>


Edengundam
  Kiron replied...
20-Jul-07 03:11 AM
Use the containment operator -contains. Also use .psIsContainer instead of
.mode -match 'd'.
Here is the one-liner:

dir "\\$strcomputer\admin$\microsoft.net\Framework" | where
{$_.psIsContainer -and $_.name -like "v*" -and $dictionary.keys -contains
$_.name} | foreach {$dictionary."$($_.name)"}

--
Kiron
  Kiron replied...
20-Jul-07 03:21 AM
The double quotes are not necessary in the foreach loop.

foreach {$dictionary.$($_.name)}

--
Kiron
  IT Staff replied...
20-Jul-07 05:04 AM
Perhaps i m not clear on my questions.

I want to list the remote machine directory and once found it will display
the VALUES.

Version 1.0
Version 1.1
etc
  Rich replied...
20-Jul-07 05:24 AM
Part of the problem may be in the names you have in the dictionary.  For
instance on my machine the folders are
v1.0.3705
v1.1.4322
v2.0.50215
v2.0.50727
v3.0
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk
  Rich replied...
20-Jul-07 05:38 AM
Try this

$dictionary = @{}
$dictionary["v1.0"] = "Version 1.0"
$dictionary["v1.1"] = "Version 1.1"
$dictionary["v2.0"] = "Version 2.0"
$dictionary["v3.0"] = "Version 3.0"

$dir = dir "c:\windows\microsoft.net\Framework"
$folders = $dir | where {$_.mode -match "d" -and $_.name -like "v*"} |
select name

Foreach ($folder in
$folders){$dictionary[$folder.Name.Tostring().SubString(0,4) ]}

You will need to alter the $dir = ....    line for remote machines
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk
  IT Staff replied...
20-Jul-07 05:40 AM
$dictionary = @{}
$dictionary["v1.0.3705"] = "Version 1.0"
$dictionary["v1.1.4322"] = "Version 1.1"
$dictionary["v2.0.50727"] = "Version 2.0"
$dictionary["v3.0"] = "Version 3.0"

I can create the hash table to pre-determine the folder name.

However after running the directory listing, it does not display the
dictionary.values. What i need is the values not keys.
  Jacques Barathon [MS] replied...
20-Jul-07 08:23 AM
Is this what you want?

dir C:\windows\Microsoft.net\Framework | where {$dictionary.keys -contains
$_} | foreach {$dictionary["$_"]}

Jacques
  dreeschkin replied...
21-Jul-07 07:14 PM
Another approach would be:

$dictionary = @{}
$dictionary.'v1.0.3705' = 'Version 1.0'
$dictionary.'v1.1.4322' = 'Version 1.1'
$dictionary.'v2.0.50727' = 'Version 2.0'
$dictionary.'v3.0' = 'Version 3.0'

gci $env:windir\Microsoft.net\Framework | ForEach { $dictionary."$_" }

--
greetings
dreeschkind
  Matthias Tacke replied...
22-Jul-07 09:32 AM
If usage of the dictionary isn't essential:

gci $env:windir\Microsoft.net\Framework\v?.?* |
where {$_.psIsContainer}|
ForEach {write-Host "Version "$_.name.SubString(1,3)}

or

gci \\$strComputer\admin$\Microsoft.net\Framework\v?.?* |
where {$_.psIsContainer} |
ForEach {write-Host "Version "$_.name.SubString(1,3)}


--
Greetings
Matthias
Create New Account
help
locaux") Else msgbox "toto" 'Gestion liste à faire End If 'Set objNetwork = CreateObject("WScript.Network") 'strComputer = objNetwork.ComputerName strComputer = nom_machine strGroup = "Administrateurs" 'Création du fichier de log machine If not FSO.FileExists(oWshShell.CurrentDirectory & " \ Machines \ " & strComputer & ".txt") Then Set machine_log = fso.CreateTextFile(oWshShell.CurrentDirectory & else Set machine_log = fso.OpenTextFile(oWshShell.CurrentDirectory Machines \ " & strComputer & ".txt", ForWriting) end if Set objGroup = GetObject("WinNT: / / " & strComputer & " / " & strGroup & For Each objMember In objGroup.Members machine_log.WriteLine(objMember.Name) Next machine_log.close msgbox la machine en cours Loger les groupes / comptes de la machine avant et apres modif (strComputer.txt et strComputer_del.txt) Loger les machines gérées (Deploiement.csv) Loger les machines terminées (Machines_Ok strGroup = "Administrateurs" 'Corps du script MsgBox "CleanLocalGroup", vbInformation, "Lancement du Scan" while not ts1.AtEndOfStream strComputer = ts1.ReadLine msgbox strComputer, , "Ligne lue dans le fichier des machines a faire" For j = 0 To UBound(arrComputerOk
my code : Option Explicit 'On Error Resume Next Dim objGroup, _ arrMemberOf, _ strMember, _ strComputer, _ strOut Set objGroup = GetObject("LDAP: / / cn = Reboot, ou = Groups, dc = somewhere, dc = com") 'objGroup Member) > 0 Then '< = = = = passing the object. arrMemberOf = objGroup.GetEx("member") For Each strMember in arrMemberOf strComputer = Mid(strMember, 4, instr(strMember, ", ")-4) Next Else strOut = strOut & "Aucun ordinateur à processer. " & Date the collection. It might not even be a computer. When the script finishes, the variable strComputer will be the name of the last member in the group (if there is at ou = Groups, dc = somewhere, dc = com") ' Keep track of all computer members of the group. strComputer = "" ' Enumerate all members. The Members method returns a collection of ' object references for each direct the Common Name of the computer to a comma delimited string ' of computer names. If (strComputer = "") Then strComputer = objMember.cn Else strComputer = strComputer & ", " & objMember.cn End If End If Next Call MsgBox(strComputer) - - Richard Mueller MVP Directory Services
Discussions System.DirectoryServices.DirectorySearcher (1) Database (1) Searcher.FindAll (1) Search.FindOne (1) ScriptFanatic (1) RichS (1) Searcher.Filter (1) Search.Filter (1) Hi H4mm3r, Like ADAM? - -- Shay Levy Windows PowerShell ADSI] "LDAP: / / localhost:389 / dc = xyz, dc = com" then create the searcher as normal - - Richard Siddaway All scripts are supplied "as is" and with no warranty PowerShell MVP Blog: http: / / richardsiddaway the root, it would be great. Thanks in advance, Thomas. On 7 juil, 21:31, RichS [MVP] <RichS. . .@> 8. ntation e The search is the same as an AD search $struser = "BOSCH Herbert more on LDAP filters at http: / / msdn.microsoft.com / en-us / library / aa746475.aspx - - Richard Siddaway All scripts are supplied "as is" and with no warranty PowerShell MVP Blog: http: / / richardsiddaway a CN is in the database. Thanks in advance, Thomas. On 7 juil, 21:31, RichS [MVP] <RichS. . .@> 8. ntation e Hi all, Thanks Richard. Based on your example, I was able to all the basic documentation of the PowerShell language and API ? On 8 juil, 14:39, RichS [MVP] <RichS. . .@> 2008. cumentation tance Hi all, Thanks Richard. Based on your example, I was
function dir / o-d { dir $args | sort LastWriteTime -Descending} function dir / os { dir $args | ? { !$_.PSIsContainer } | sort Length } function dir / o-s { dir $args | ? { !$_.PSIsContainer } | sort Length -Descending} function dir / os / a-d { dir $args | ? { !$_.PSIsContainer } | sort Length } function dir / o-s / a-d { dir $args | ? { !$_ PSIsContainer } | sort Length -Descending} function dir / ad { dir $args | ? { $_.PSIsContainer } } function dir / a-d { dir $args | ? { !$_.PSIsContainer } } function dir / ah { dir $args -force | ? { $_.Attributes ?band [IO.FileAttributes]::Hidden } } function dir / od / a-d { dir $args | ? { !$_.PSIsContainer } | sort LastWriteTime } function dir / o-d / a-d { dir $args | ? { !$_ PSIsContainer } | sort LastWriteTime -Descending} - Larry Powershell Discussions UNIX (1) PSIsContainer (1) LastWriteTime (1) Attributes (1) Directory
NO SUBS DIRECTORYS" $MonitorDir = "D: \ test \ 0" $SubDirs = Get-ChildItem -Recurse -Path $MonitorDir | Where {$_.psIsContainer -eq $true} write-host "Length :" $SubDirs.Length if ( $SubDirs.Exists ) { write-host "Exists = TRUE" } else ONE SUB FOLDER" $MonitorDir = "D: \ test \ 1" $SubDirs = Get-ChildItem -Recurse -Path $MonitorDir | Where {$_.psIsContainer -eq $true} write-host "Length :" $SubDirs.Length if ( $SubDirs.Exists ) { write-host "Exists = TRUE" } else MORE SUB FOLDERS" $MonitorDir = "D: \ test \ 2+" $SubDirs = Get-ChildItem -Recurse -Path $MonitorDir | Where {$_.psIsContainer -eq $true} write-host "Length :" $SubDirs.Length if ( $SubDirs.Exists ) { write-host "Exists = TRUE" } else Discussions NDIRECTORY (1) System.IO.DirectoryInfo (1) SubDirs.Exists (1) SubDirs.Length (1) GetDirectories (1) PsIsContainer (1) DirectoryInfo (1) MonitorDir (1) This is because you are getting three different kind of MonitorDir = 3D "C: \ test \ 0" [array]$SubDirs = 3D Get-ChildItem -Recurse -Path $MonitorDir | where{$_.psIsContainer -eq $true} Write-Host "Length :" $SubDirs.Length if ($SubDirs.Exists) { Write-Host "Exists = 3DTRUE" } else MonitorDir = 3D "C: \ test \ 1" [array]$SubDirs = 3D Get-ChildItem -Recurse -Path $MonitorDir | where{($_.psIsContainer) -eq $true} Write-Host "Length :" $SubDirs.Length if ($SubDirs.Exists) { Write-Host "Exists = 3DTRUE" } else MonitorDir = 3D "C: \ test \ 2+" [array]$SubDirs = 3D Get-ChildItem -Recurse -Path $MonitorDir | where{ ($_.psIsContainer) -eq $true } Write-Host "Length :" $SubDirs.Length if ($SubDirs.Exists) { Write-Host "Exists = 3DTRUE" } else has an operator to force the result into an array: $Subdirs = @(gci $monitordir -r | ?{$_.PSIsContainer}) - - Keith In this particular case, you can use .NET itself to give precise information to