1

这是 3 个代码,第一个用于创建目录,第二个用于将 .ico 文件保存在该目录中。最后一个代码是使用 .ico 文件作为图标创建快捷方式。

我认为我对脚本的顺序做错了。

$path = "C:\Program Files\icons"
If(!(test-path $path))
{
      New-Item -ItemType Directory -Force -Path $path
}

$Base64String = '@base64string'
$Image = "C:\Program Files\icons\Alphen.ico"
[byte[]]$Bytes = [convert]::FromBase64String($Base64String)
[System.IO.File]::WriteAllBytes($Image,$Bytes)

param (
    [system.string]$ShortcutName     = "portaal",
    [system.string]$ShortcutUrl      = "https://portal.rotterdam.nl",
    [system.string]$IconURL          = "C:\Program Files\icons\Alphen.ico",
    [system.string]$Desktop          = [Environment]::GetFolderPath("Desktop"),
 [system.string]$IntuneProgramDir = "$env:APPDATA\Intune",
    [System.String]$TempIcon         = "$IntuneProgramDir\msedge.exe",
    [bool]$ShortcutOnDesktop         = $True,
    [bool]$ShortcutInStartMenu       = $True
)

#Test if icon is currently present, if so delete it so we can update it
$IconPresent = Get-ChildItem -Path $Desktop | Where-Object {$_.Name -eq "$ShortcutName.lnk"}
If ($null -ne $IconPresent)
{
    Remove-Item $IconPresent.VersionInfo.FileName -Force -Confirm:$False
}

$WScriptShell = New-Object -ComObject WScript.Shell

If ((Test-Path -Path $IntuneProgramDir) -eq $False)
{
    New-Item -ItemType Directory $IntuneProgramDir -Force -Confirm:$False
}

#Start download of the icon 
Start-BitsTransfer -Source $IconURL -Destination $TempIcon


if ($ShortcutOnDesktop)
{
    $Shortcut = $WScriptShell.CreateShortcut("$Desktop\$ShortcutName.lnk")
    $Shortcut.TargetPath = $ShortcutUrl
    $Shortcut.IconLocation = $TempIcon
    $Shortcut.Save()
}

if ($ShortCutInStartMenu)
{
    $Shortcut = $WScriptShell.CreateShortcut("$env:APPDATA\Microsoft\Windows\Start Menu\Programs\$ShortcutName.lnk")
    $Shortcut.TargetPath = $ShortcutUrl
    $Shortcut.IconLocation = $TempIcon
    $Shortcut.Save()
}
4

1 回答 1

3

你的代码很好。参数必须在开头。我已经改变了顺序。建议将整个事情放在try/catch块中,以便您可以捕获异常和错误消息。

param (
    [system.string]$ShortcutName     = "portaal",
    [system.string]$ShortcutUrl      = "https://portal.rotterdam.nl",
    [system.string]$IconURL          = "C:\Program Files\icons\Alphen.ico",
    [system.string]$Desktop          = [Environment]::GetFolderPath("Desktop"),
 [system.string]$IntuneProgramDir = "$env:APPDATA\Intune",
    [System.String]$TempIcon         = "$IntuneProgramDir\msedge.exe",
    [bool]$ShortcutOnDesktop         = $True,
    [bool]$ShortcutInStartMenu       = $True
)
$path = "C:\Program Files\icons"
If(!(test-path $path))
{
      New-Item -ItemType Directory -Force -Path $path
}

$Base64String = '@base64string'
$Image = "C:\Program Files\icons\Alphen.ico" 
[byte[]]$Bytes = [convert]::FromBase64String($Base64String)
[System.IO.File]::WriteAllBytes($Image,$Bytes)



#Test if icon is currently present, if so delete it so we can update it
$IconPresent = Get-ChildItem -Path $Desktop | Where-Object {$_.Name -eq "$ShortcutName.lnk"}
If ($null -ne $IconPresent)
{
    Remove-Item $IconPresent.VersionInfo.FileName -Force -Confirm:$False
}

$WScriptShell = New-Object -ComObject WScript.Shell

If ((Test-Path -Path $IntuneProgramDir) -eq $False)
{
    New-Item -ItemType Directory $IntuneProgramDir -Force -Confirm:$False
}

#Start download of the icon 
Start-BitsTransfer -Source $IconURL -Destination $TempIcon


if ($ShortcutOnDesktop)
{
    $Shortcut = $WScriptShell.CreateShortcut("$Desktop\$ShortcutName.lnk")
    $Shortcut.TargetPath = $ShortcutUrl
    $Shortcut.IconLocation = $TempIcon
    $Shortcut.Save()
}

if ($ShortCutInStartMenu)
{
    $Shortcut = $WScriptShell.CreateShortcut("$env:APPDATA\Microsoft\Windows\Start Menu\Programs\$ShortcutName.lnk")
    $Shortcut.TargetPath = $ShortcutUrl
    $Shortcut.IconLocation = $TempIcon
    $Shortcut.Save()
}
于 2021-03-04T16:18:10.660 回答