Subscribe

SQLServer-DBA.com Links

Subscribe to newsletter

Dba_db2_button

Powered by TypePad
How to create a clickable link in Powershell

19 July,2013 by Jack Vamvas

Question : From the script below , I’m placing a file name in a variable. How can I doubleclick  the filename using Powershell , so it opens in a suitable client ?

 

srcDir = "C:\temp\STA\"
$File = Get-ChildItem $srcDir\myfile.* | % { ($_.basename)}
echo $File

 

Answer:  One method is to output  the link to a html file.  The user can  click on the link.

The example below can expanded in all sorts of ways. One way to extend is to place multiple files in an array and output into a html page 

Let’s break this down

Step 1: Source the file using Get-ChildItem

Step 2: Create a variable for the output file

Step 3: Create variables for the link and link title values

Step 4: Create a New-ObjectPSObject and Convert-To-html

Step 5: Use the HtmlDecode method  and the Set-Content cmdlet to write the data to the text file or in this case the html file

 

#Step1
$srcDir = "C:\projects\testing\laboratory"
$File = Get-ChildItem $srcDir\Servers.txt

#Step2 
$outputfile = 'C:\projects\testing\laboratory.html'


#Step 3: extract the base name to generate the link text
$PageTitle  = $File | % { ($_.basename)}
$PageLink = $File 

#Step 4
$link = New-Object PSObject -Property @{
    PageUrl = "$PageTitle"  
} | ConvertTo-Html

#Step 5
Add-Type -AssemblyName System.Web
[System.Web.HttpUtility]::HtmlDecode($link) | Set-Content  $outputfile

 Read More

Powershell Get-ChildItem with examples - SQL Server DBA

Powershell to HTML - SQL Server DBA


Author: Jack Vamvas (http://www.sqlserver-dba.com)

******** *******

Subscribe to SQLServer-DBA.com

Latest posts delivered to you daily

Delivered by FeedBurner

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer