我如何 运行 将变量中的内容与 PowerShell 上的哈希表进行比较
How can I run compare content in a Variable against a hashtable on PowerShell
我有一个哈希表如下:
$ProjectType = @{
CSharp = 'FAE04EC0-301F-11D3-BF4B-00C04F79EFBC'
Web_Application = '349C5851-65DF-11DA-9384-00065B846F21'
Windows_Communication_Foundation = '3D9AD99F-2412-4246-B90B-4EAA41C64699'
Windows_Presentation_Foundation = '60DC8134-EBA5-43B8-BCC9-BB4BC16C2548'
Test = '3AC096D0-A1C2-E12C-1390-A8335801FDAB'
Silverlight = 'A1591282-1198-4647-A2B1-27E5FF5F6F3B'
}
我想根据变量 $file 中的内容 运行 哈希表,如果在 $file 中找到值 (guid),则从哈希表中获取项目类型名称的 return。
翻转键和值,使引用 GUID 成为键:
$ProjectType = @{
'FAE04EC0-301F-11D3-BF4B-00C04F79EFBC' = 'CSharp'
'349C5851-65DF-11DA-9384-00065B846F21' = 'Web_Application'
'3D9AD99F-2412-4246-B90B-4EAA41C64699' = 'Windows_Communication_Foundation'
'60DC8134-EBA5-43B8-BCC9-BB4BC16C2548' = 'Windows_Presentation_Foundation'
'3AC096D0-A1C2-E12C-1390-A8335801FDAB' = 'Test'
'A1591282-1198-4647-A2B1-27E5FF5F6F3B' = 'Silverlight'
}
现在您可以轻松查找任何值:
$file = '3AC096D0-A1C2-E12C-1390-A8335801FDAB'
if($ProjectType.ContainsKey($file)){
Write-Host "Found project type guid for '$($ProjectType[$file])'"
}
您很可能想颠倒 Keys 和 Values 在 Hash Table 上的顺序:
$ProjectType = @{
'FAE04EC0-301F-11D3-BF4B-00C04F79EFBC' = 'CSharp'
'349C5851-65DF-11DA-9384-00065B846F21' = 'Web_Application'
'3D9AD99F-2412-4246-B90B-4EAA41C64699' = 'Windows_Communication_Foundation'
'60DC8134-EBA5-43B8-BCC9-BB4BC16C2548' = 'Windows_Presentation_Foundation'
'3AC096D0-A1C2-E12C-1390-A8335801FDAB' = 'Test'
'A1591282-1198-4647-A2B1-27E5FF5F6F3B' = 'Silverlight'
}
# using this as example
$exampleFile = @'
349C5851-65DF-11DA-9384-00065B846F21
60DC8134-EBA5-43B8-BCC9-BB4BC16C2548
A1591282-1198-4647-A2B1-27E5FF5F6F3B
00000000-1198-4647-A2B1-27E5FF5F6F3B
'@ -split '\r?\n'
foreach($line in $exampleFile)
{
if($val = $ProjectType[$line])
{
"$line => $val"
continue
}
"$line => could not be found on reference table."
}
我有一个哈希表如下:
$ProjectType = @{
CSharp = 'FAE04EC0-301F-11D3-BF4B-00C04F79EFBC'
Web_Application = '349C5851-65DF-11DA-9384-00065B846F21'
Windows_Communication_Foundation = '3D9AD99F-2412-4246-B90B-4EAA41C64699'
Windows_Presentation_Foundation = '60DC8134-EBA5-43B8-BCC9-BB4BC16C2548'
Test = '3AC096D0-A1C2-E12C-1390-A8335801FDAB'
Silverlight = 'A1591282-1198-4647-A2B1-27E5FF5F6F3B'
}
我想根据变量 $file 中的内容 运行 哈希表,如果在 $file 中找到值 (guid),则从哈希表中获取项目类型名称的 return。
翻转键和值,使引用 GUID 成为键:
$ProjectType = @{
'FAE04EC0-301F-11D3-BF4B-00C04F79EFBC' = 'CSharp'
'349C5851-65DF-11DA-9384-00065B846F21' = 'Web_Application'
'3D9AD99F-2412-4246-B90B-4EAA41C64699' = 'Windows_Communication_Foundation'
'60DC8134-EBA5-43B8-BCC9-BB4BC16C2548' = 'Windows_Presentation_Foundation'
'3AC096D0-A1C2-E12C-1390-A8335801FDAB' = 'Test'
'A1591282-1198-4647-A2B1-27E5FF5F6F3B' = 'Silverlight'
}
现在您可以轻松查找任何值:
$file = '3AC096D0-A1C2-E12C-1390-A8335801FDAB'
if($ProjectType.ContainsKey($file)){
Write-Host "Found project type guid for '$($ProjectType[$file])'"
}
您很可能想颠倒 Keys 和 Values 在 Hash Table 上的顺序:
$ProjectType = @{
'FAE04EC0-301F-11D3-BF4B-00C04F79EFBC' = 'CSharp'
'349C5851-65DF-11DA-9384-00065B846F21' = 'Web_Application'
'3D9AD99F-2412-4246-B90B-4EAA41C64699' = 'Windows_Communication_Foundation'
'60DC8134-EBA5-43B8-BCC9-BB4BC16C2548' = 'Windows_Presentation_Foundation'
'3AC096D0-A1C2-E12C-1390-A8335801FDAB' = 'Test'
'A1591282-1198-4647-A2B1-27E5FF5F6F3B' = 'Silverlight'
}
# using this as example
$exampleFile = @'
349C5851-65DF-11DA-9384-00065B846F21
60DC8134-EBA5-43B8-BCC9-BB4BC16C2548
A1591282-1198-4647-A2B1-27E5FF5F6F3B
00000000-1198-4647-A2B1-27E5FF5F6F3B
'@ -split '\r?\n'
foreach($line in $exampleFile)
{
if($val = $ProjectType[$line])
{
"$line => $val"
continue
}
"$line => could not be found on reference table."
}