Creating Shortcuts via Powershell with ZCM

3 Likes
over 2 years ago

ZCM contains a very limited interface for creating windows shortcut files.

Furthermore, uploading pre-created LNK files can be difficult.

See - https://support.microfocus.com/kb/doc.php?id=3093343

Using a Powershell script to create LNK files avoids the issues of trying to upload a pre-created LNK file as well as removes the limitations imposed by the GUI for creating shortcuts.

Since writing a Powershell script from scratch can be tricky, I've created a sample script and sample bundle that will allow for the easy creation of any shortcut with a wide array of customizable settings.

The script contains 2 sections.

The top section has an array of variables that need to be set to match the requirements of the shortcut.  This is the only section of the script that should need to be updated.

The bottom section performs the actual work and should not need to be modified.

A fully functional bundle has been attached that can be imported into the ZCC.

A copy of the script is pasted below....

#########################################
# Update the Variables Below as Desired #
#########################################

$LNKFile="c:\temp\Readme.lnk" #Name and Path of LNK to Create/Edit
$LNKExe="c:\windows\notepad.exe" #Name of Executable/Script to Run in Shortcut
$LNKParm="c:\windows\readme.txt" #Command line Parameter to Pass to Executable
$LNKWorkDir="c:\windows" #Working Directory if Desired
$LNKIcon="$LNKExe,0" #Setting Icon Example
#$LNKIcon="C:\windows\system32\shell32.dll,99" #Setting Icon Example 2
$LNKDescription="A Sample Description" #LNK Description Field.
$LNKWinStart=1 #Window Start Size 1=Norm, 3=Maximized, 7=Minimized
#$LNKHotKey="CTRL SHIFT F" # Enter HotKey Value and Uncomment Next Line if Desired.
$LNKRunAsAdmin=0 # Set LNKRunAsAdmin to 1 to enable "Run As Administrator CheckBox"

 

#######################################
### Main Code Only Edit Lines Above ###
#######################################

$Shell = New-Object -ComObject ("WScript.Shell")
$ShortCut = $Shell.CreateShortcut("$LNKFile")
$ShortCut.TargetPath="$LNKExe"
$ShortCut.Arguments="$LNKParm"
$ShortCut.WorkingDirectory = "$LNKWorkDir";
$ShortCut.WindowStyle = $LNKwinStart;
$ShortCut.Hotkey = $LNKHotKey
$ShortCut.IconLocation = "$LNKIcon";
$ShortCut.Description = $LNKDescription;
$ShortCut.Save()

### Lines below are to set shortcut to "Run As Administrator"
### May never be used but here is the code just in case…

If ($LNKRunAsAdmin -ne 1)
{
exit
}
else
{$bytes = [System.IO.File]::ReadAllBytes("$LNKFile")
$bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
[System.IO.File]::WriteAllBytes("$LNKFile", $bytes)
}

--

To find other articles by Craig Wilson simply follow the link below:

https://community.microfocus.com/members/craigdwilson/bookmarks

If you find this article useful, please be sure to give it a like at the bottom of the page!

Labels:

Collateral
Configuration Management
How To-Best Practice
Comment List
Anonymous
Related Discussions
Recommended