I was asked once again, "How can I use PowerShell to create a shortcut?". For those who may have been scripting for a bit, we used to have a nifty VBScript to complete this task. It was something similar to this:
Set objShell = WScript.CreateObject("WScript.Shell")
Set lnk = objShell.CreateShortcut("C:\MyShortcut.LNK")
lnk.TargetPath = "C:\Program Files\MyApp\MyProgram.EXE"
lnk.Arguments = ""
lnk.Description = "MyProgram"
lnk.HotKey = "ALT+CTRL+F"
lnk.IconLocation = "C:\Program Files\MyApp\MyProgram.EXE, 2"
lnk.WindowStyle = "1"
lnk.WorkingDirectory = "C:\Program Files\MyApp"
lnk.Save
PowerShell is a newer method to complete these tasks. As you may know, it has the ability to handle calls to the Windows Scripting Host and generally ease execution as you don't need to call the scripting host for processing. Translation, no batch file to execute the VBScript using the desired scripting engine. Below is an example:
$Shell = New-Object -ComObject ("WScript.Shell") #Set the shell used
$ShortCut = $Shell.CreateShortcut($env:USERPROFILE + "\Desktop\Whatever.lnk") #Where to create the shortcut
$ShortCut.TargetPath="executable.exe" #Set the path and file name for the target
$ShortCut.Arguments="-arguementsifrequired" #Only if necessary
$ShortCut.WorkingDirectory = "path to file"; #Remember, apps need to know where they started from
$ShortCut.WindowStyle = 1; # 1 - Activates and displays a window.3 - Activates the window and displays it as a maximized window.7 - Minimizes the window and activates the next top-level window.
$ShortCut.Hotkey = "CTRL+SHIFT+F";
$ShortCut.IconLocation = "yourexecutable.exe, 0"; #Picture for shortcut and index number from file
$ShortCut.Description = "Your Custom Shortcut Description"; #What is it
$ShortCut.Save() #Create the shortcut
You can also create a shortcut to a URL. Not much different, but here is an example:
$Shell = New-Object -ComObject ("WScript.Shell")
$URL= $Shell.CreateShortcut($env:USERPROFILE + "\Desktop\Whatever.url")
$URL.TargetPath = "http://www.url.com";
$URL.Save()
You still call the Windows Script host to complete the work. However, the file name is '.url' rather than '.lnk', which invokes the browser as it is a known file type association. This is a quick post, for more detail on these examples be sure to check out the following:
https://ss64.com/vb/shortcut.html
http://powershellpainrelief.blogspot.com/2013/03/creating-desktop-shortcuts-using.html
Now you are able to incorporate this in your deployment scripts via your chosen tools or by simply executing your script!
-Happy Scripting!
Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts
Thursday, September 12, 2019
Tuesday, June 25, 2019
Package management... In Windows?
You read that title right! There are package management capabilities for Windows! Within the operating system, Microsoft integrated what was once known as OneGet back in PowerShell 5.0. But wait, there's more! There is a third party tool and package repo that includes many applications that would normally be sourced from multiple websites called Chocolatey. For those who may be familiar, the open source community has had several utilities for some time, such as apt-get or yum. Package management allows for the distribution of applications and configuration settings in a programmatic manner. I have been known to say, "If you will have to do it more than twice, automate it!".
A bit of personal history. I have spent a lot of time during my career in EUC and endpoint management. Through this, I have attended InstallShield (Macrovision) training, used tools such as InstallShield Admin Studio, Wise Package Studio, WinZip Self-Extractor, Novell ZENworks Suite, Microsoft Group Policy, Microsoft SCCM and various script techniques to implement application and workstation lifecycle solutions for several large enterprises and adapted these methods to the individual and small business. So when I learned about this tool, of course, I was enticed to know more.
With package management, you can...
- Manage a list of software repositories in which packages can be searched, acquired, and installed
- Search and filter your repositories to find the packages you need
- Seamlessly install and uninstall packages from one or more repositories with a single PowerShell command
Per GitHub -
"PackageManagement is supported in Windows, Linux and MacOS now. We periodically make binary drops to PowerShellCore, meaning PackageManagement is a part of PowerShell Core releases. Also PackageManagement and PowershellGet Modules are regularly updated in PowerShellGallery.com."
"OneGet is shipped in Win10 and Windows Server 2016! For downlevel OS, you can install the WMF 5.1 and then start using the OneGet."
What is PackageManagement (OneGet)?
OneGet is a Windows package manager, renamed as PackageManagement. It is a unified interface to package management systems and aims to make Software Discovery, Installation, and Inventory (SDII) work via a common set of cmdlets (and eventually a set of APIs). Regardless of the installation technology underneath, users can use these common cmdlets to install/uninstall packages, add/remove/query package repositories, and query a system for the software installed.
As shown in this image, there are many providers and sources currently available. The PowerShell gallery is a growing collection of modules and code while another, Chocolatey, has quickly become a mainstay for application acquisition.
Per Chocolatey -
Think of Chocolatey as a framework that you can build on top of. Chef, Puppet, Boxstarter, PowerShell DSC, Ansible, Saltstack, etc all have ways for using Chocolatey to ensure the state of a computer and packages installed. Even Microsoft has decided to use Chocolatey's framework with the PowerShell PackageManagement / OneGet package manager aggregator! See Jeffrey Snover's post for more information.
Chocolatey is a package manager for Windows (like apt-get or yum but for Windows). It was designed to be a decentralized framework for quickly installing applications and tools that you need. It is built on the NuGet infrastructure currently using PowerShell as its focus for delivering packages from the distros to your door, err computer.
Chocolatey is a single, unified interface designed to easily work with all aspects of managing Windows software (installers, zip archives, runtime binaries, internal and 3rd party software) using a packaging framework that understands both versioning and dependency requirements. Chocolatey packages encapsulate everything required to manage a particular piece of software into one deployment artifact by wrapping installers, executables, zips, and scripts into a compiled package file. Chocolatey packages can be used independently, but also integrate with configuration managers like SCCM, Puppet, and Chef. Chocolatey is trusted by businesses all over the world to manage their software deployments on Windows. You’ve never had so much fun managing software!
I highly recommend familiarizing yourself with this capability as it will save you a lot of time building and deploying in your environment! I also thank the PowerShell and Chocolatey teams for their efforts for these capabilities.
Think of Chocolatey as a framework that you can build on top of. Chef, Puppet, Boxstarter, PowerShell DSC, Ansible, Saltstack, etc all have ways for using Chocolatey to ensure the state of a computer and packages installed. Even Microsoft has decided to use Chocolatey's framework with the PowerShell PackageManagement / OneGet package manager aggregator! See Jeffrey Snover's post for more information.
Chocolatey is a package manager for Windows (like apt-get or yum but for Windows). It was designed to be a decentralized framework for quickly installing applications and tools that you need. It is built on the NuGet infrastructure currently using PowerShell as its focus for delivering packages from the distros to your door, err computer.
Chocolatey is a single, unified interface designed to easily work with all aspects of managing Windows software (installers, zip archives, runtime binaries, internal and 3rd party software) using a packaging framework that understands both versioning and dependency requirements. Chocolatey packages encapsulate everything required to manage a particular piece of software into one deployment artifact by wrapping installers, executables, zips, and scripts into a compiled package file. Chocolatey packages can be used independently, but also integrate with configuration managers like SCCM, Puppet, and Chef. Chocolatey is trusted by businesses all over the world to manage their software deployments on Windows. You’ve never had so much fun managing software!
I highly recommend familiarizing yourself with this capability as it will save you a lot of time building and deploying in your environment! I also thank the PowerShell and Chocolatey teams for their efforts for these capabilities.
Wednesday, May 29, 2019
Playing music with PowerShell
While continuing my journey to continue learning Microsoft PowerShell, I thought back to the early days of making music with computers. I have worked on many projects throughout the years, using several languages, and making simple music has been a repeated challenge that I have used to help learn timing, loops, functions and debugging. While doing so in PowerShell, I located a site that had already published frequency information at https://sl.advdat.com/NotesAndFrequencies, the related .NET information at https://sl.advdat.com/2WghUEV and finally the PowerShell call '[System.Console]::Beep(frequency,length in milliseconds)'. After compiling this information, I took the examples I found and produced the following code:
##########Begin Script##############
With this information, you too can create simple music on the console. I have been working through translating some old simplified sheet music, just to see what I can do. It's a bit more enjoyable than the old Hello World scripts we have usually started with. However, I have noted several examples on GitHub as well:
Tetris - https://gist.github.com/HelgeSverre/33361e8a283624dfbbd6
SuperMario - https://gist.github.com/davewilson/5612674
Happy scripting!
##########Begin Script##############
# Notes
$Dsharp1 = 38.891
$E1 = 41.203
$F1 = 43.654
$Fsharp1 = 46.249
$G1 = 48.999
$Gsharp1 = 51.913
$A1 = 55
$Asharp1 = 58.27
$B1 = 61.735
$C2 = 65.406
$Csharp2 = 69.296
$D2 = 73.416
$Dsharp2 = 77.782
$E2 = 82.407
$F2 = 87.307
$Fsharp2 = 92.499
$G2 = 97.999
$Gsharp2 = 103.826
$A2 = 110
$Asharp2 = 116.541
$B2 = 123.471
$C3 = 130.813
$Csharp3 = 138.591
$D3 = 146.832
$Dsharp3 = 155.563
$E3 = 164.814
$F3 = 174.614
$Fsharp3 = 184.997
$G3 = 195.998
$Gsharp3 = 207.652
$A3 = 220
$Asharp3 = 233.082
$B3 = 246.942
$C4 = 261.626
#Middle C
$Csharp4 = 277.183
$D4 = 293.665
$Dsharp4 = 311.127
$E4 = 329.628
$F4 = 349.228
$Fsharp4 = 369.994
$G4 = 391.995
$Gsharp4 = 415.305
$A4 = 440
$Asharp4 = 466.164
$B4 = 493.883
$C5 = 523.251
$Csharp5 = 554.365
$D5 = 587.33
$Dsharp5 = 622.254
$E5 = 659.255
$F5 = 698.456
$Fsharp5 = 739.989
$G5 = 783.991
$Gsharp5 = 830.609
$A5 = 880
$Asharp5 = 932.328
$B5 = 987.767
$C6 = 1046.502
$Csharp6 = 1108.731
$D6 = 1174.659
$Dsharp6 = 1244.508
$E6 = 1318.51
$F6 = 1396.913
$Fsharp6 = 1479.978
$G6 = 1567.982
$Gsharp6 = 1661.219
$A6 = 1760
$Asharp6 = 1864.655
$B6 = 1975.533
$C7 = 2093.005
$Csharp7 = 2217.461
$D7 = 2349.318
$Dsharp7 = 2489.016
$E7 = 2637.021
$F7 = 2793.826
$Fsharp7 = 2959.955
$G7 = 3135.964
# Note Types 60bpm
Whole=4000ms
$w=4000 #whole note
$h=$w/2 #half note
$q=$h/2 #quarter
note
$e=$q/2 #eighth note
$s=$e/2 #sixteenth
note
#Play Middle C
[System.Console]::Beep($C4,$W) # Middle C
Function CMScale{
[System.Console]::Beep($c4,$q)
[System.Console]::Beep($d4,$q)
[System.Console]::Beep($e4,$q)
[System.Console]::Beep($F4,$q)
[System.Console]::Beep($g4,$q)
[System.Console]::Beep($a4,$q)
[System.Console]::Beep($b4,$q)
[System.Console]::Beep($c5,$q)
}
#Call Function
CMScale
##########End Script##############
With this information, you too can create simple music on the console. I have been working through translating some old simplified sheet music, just to see what I can do. It's a bit more enjoyable than the old Hello World scripts we have usually started with. However, I have noted several examples on GitHub as well:
Tetris - https://gist.github.com/HelgeSverre/33361e8a283624dfbbd6
SuperMario - https://gist.github.com/davewilson/5612674
Happy scripting!
Subscribe to:
Posts (Atom)