powershelltobase64string

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


These are 3 codes, first one to make a directory and second one to save the .ico file in that directory. The last code is to make a shortcut with the .ico file as icon.

I think i did something wrong with the order of the script.

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

Solution

  • Your code is fine. The param must be in the beginning. I have changed the order. Recommendation is to put the entire thing in try/catch block so that you can catch the exceptions and error messages.

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