有人可以帮我修复这个 Powershell 脚本吗?我对此很陌生,不知何故它不起作用

Can someone help me to fix this Powershell script ? i'm new to this and somehow its not working

这是 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()
}

你的代码没问题。参数必须在开头。我已经改变了顺序。 建议将整个内容放在 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()
}