Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在这种情况下,是否可以通过PowerShell创建阵列?_Powershell - Fatal编程技术网

在这种情况下,是否可以通过PowerShell创建阵列?

在这种情况下,是否可以通过PowerShell创建阵列?,powershell,Powershell,所以我有一个代码: 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\Ca

所以我有一个代码:

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
是否可以通过PowerShell制作如下所示的阵列:

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
}

否则我将毫无意义?

鉴于文件扩展名和文件类型之间没有1对1的关系,您可能希望使用哈希表:

$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
}

…你到底在问什么?我的意思是,把这些代码放到数组中是正确的吗?如果你这样做,代码会更少,所以很可能。从根本上说,无论哪种方式,您都会得到相同的结果,因此这取决于您的用例。