2

I'm in the process of writing a Powershell script that will create my ConfigMgr 2012 applications for me. One part of this process is associating an icon with the application.

The method I'm doing this does, in fact, attach the .ICO file that I'm telling it but the quality is horrible. I attach this same .ICO file to an existing application in the GUI and it looks great. I've tried all kinds of tricks using System.Drawing.Bitmap, System.Drawing.Image, etc but it either throws an exception because it can't convert it to a Byte type or when it doesn't do that, the application crashes my ConfigMgr console when I try to view it.

Here's my current code:

$icon = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.Icon
$x = [System.Drawing.Icon]::ExtractAssociatedIcon($FilePathToIcon)
$icon.Data = $x
return $icon

I then go onto add the $Icon object to my Application object. I already have the .ICO file. My thinking at this point is that I shouldn't have to extract an icon if I already have an ICO file but I can't, for the life of me, figure out how to get this thing in a decent quality.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
Adam Bertram
  • 3,858
  • 4
  • 22
  • 28
  • Hmmmm that's interesting. I've had problems in the past around importing icons that are too big in size. I wonder if it's an issue with the Icon class in the Base Class Library? Can you confirm that the Icon class isn't messing with it during the "extract?" –  Jan 13 '14 at 00:33

3 Answers3

1

The problem appears to stem from the fact that, if you call ExtractAssociatedIcon, you will get a 32-pixel icon at most. If you want a larger version of the original icon file, you will have to use one of the System.Drawing.Icon class' constructors. All of the constructors can be found at this link.

Limited to 32px

Once again, you'll notice that, using the ExtractAssociatedIcon static method limits you to having a 32px icon, even if the original file is larger than that.

$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\Users\Trevor\Downloads\Aaron-Sinuhe-Tv-Movie-Folder-Lord-Of-The-Rings-1.ico");
$Icon.Height; # 32px
$Icon.Width;  # 32px

Getting a larger Icon instance

One of the constructors allows you to specify a [int] Width, and [int] Height parameter after the [String] FileName. Assuming that you have a large (128px+ square) ICO file in c:\test\icon.ico, check out the following code.

$Icon = New-Object -TypeName System.Drawing.Icon -ArgumentList 'C:\test\icon.ico', 128, 128;
$Icon.Height; # 128px
$Icon.Width;  # 128px

Alternatively, another constructor allows you to specify a [String] FileName, and [System.Drawing.Size] Size parameter.

$Size = New-Object -TypeName System.Drawing.Size -ArgumentList 128, 128;
$Icon = New-Object -TypeName System.Drawing.Icon -ArgumentList 'C:\test\icon.ico', $Size;
$Icon.Height; # 128px
$Icon.Width;  # 128px
1

I'm trying add a icon to a sccm application but i'm confused how can I do that. So I have the following code to add the application:

create the application.

$app = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.Application

set the application properties.

$app.Title = $name
$app.Publisher = $publisher
$app.SoftwareVersion = $release

prepare the localised display info.

$info = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.AppDisplayInfo

set the localised application name.

$info.Title = $name
$info.Language = "en-US"

save the display properties.

$app.DisplayInfo.Add($info)

I can figure import the icon to the object with the code mentioned on the 1st answer:

"

$icon = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.Icon
$x = [System.Drawing.Icon]::ExtractAssociatedIcon($FilePathToIcon)
$icon.Data = $x
$info.icon = $icon

"

But as already mentioned, add the icon from this way the icon gets bad quality on software catalog of SCCM.

I can't use the code mentioned for large icons in powershell.

0

I ran into this same question recently with getting the icon file and came across this post. Figured out that the .data property was asking for "byte[]" data. So, found something that could turn the file into a Byte. System.IO.File seemed to do the job. Only tested against one Icon file so far, but it seemed to work. Try it and let me know if it works for you.

$SCCMIcon = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.Icon
$SCCMIcon.data = [System.IO.File]::ReadAllBytes("$Icon")
$info.Icon = $SCCMIcon