2017-08-16 4 views
1

So habe ich einen Code:Kann in diesem Fall ein Array über PowerShell erstellt werden?

ForEach ($type in @(
".bmp" 
".jpg" 
".jpe" 
"jpeg" 
".png" 
)) 
{ 
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows Photo Viewer\Capabilities\FileAssociations" -Name $type -Type String -Value $something -Force 
} 

oder ich sinnlos:

New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows Photo Viewer\Capabilities\FileAssociations" -Name .bmp -Type String -Value PhotoViewer.FileAssoc.Bitmap -Force 
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows Photo Viewer\Capabilities\FileAssociations" -Name .jpg -Type String -Value PhotoViewer.FileAssoc.Jpeg -Force 
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows Photo Viewer\Capabilities\FileAssociations" -Name .jpe -Type String -Value PhotoViewer.FileAssoc.Jpeg -Force 
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows Photo Viewer\Capabilities\FileAssociations" -Name .jpeg -Type String -Value PhotoViewer.FileAssoc.Jpeg -Force 
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows Photo Viewer\Capabilities\FileAssociations" -Name .png -Type String -Value PhotoViewer.FileAssoc.Png -Force 

und ist es möglich, eine Reihe über Powershell wie so etwas zu machen?

+0

... Was fragst du eigentlich hier? – arco444

+0

Ich meine, wird es richtig sein, diesen Code in etwas mit Array ausgeben? – farag

+1

Es ist weniger Code, wenn Sie es tun, so wahrscheinlich. Grundsätzlich erhalten Sie das gleiche Ergebnis, also hängt es von Ihrem Anwendungsfall ab. – arco444

Antwort

1

Da es keine 1: 1-Beziehung zwischen der Dateierweiterung und Dateityp, könnte man mit einem Hash-Tabelle gehen wollen:

$PVPrefix = 'PhotoViewer.FileAssoc' 
$Assocs = @{ 
    ".bmp" = "Bitmap" 
    ".jpg" = "Jpeg" 
    ".jpe" = "Jpeg" 
    ".jpeg" = "Jpeg" 
    ".png" = "Png" 
} 
foreach($ext in $Assocs.Keys){ 
    New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows Photo Viewer\Capabilities\FileAssociations" -Name $ext -Type String -Value "$PVPrefix.$($Assocs[$ext])" -Force 
} 
Verwandte Themen