📜 ⬆️ ⬇️

The script to create shortcuts remote control

Imagine that you have a table with the names and addresses of devices and services and you can easily get a lot of shortcuts from it to launch a browser, putty, remote desktop or telnet to manage these devices. The picture below shows schematically:

Here is a beautiful picture of how dry lines of a spreadsheet become miraculous labels.
Why is it even needed? For example, a new system has come to you for operation or inspection, or you have been given some test environment for use. In order not to drive addresses or copy from a file every time you connect to hosts, you can run the script once and create all the shortcuts at once.

The powershell script processes the CSV file, finds the columns “name”, “address”, “description”, “access”, creates shortcuts. The command for the shortcut is formed based on the value “access” (http, https, rdp, telnet), the arguments are from the value “address”. By the way, the “address” may not be an IP address, but a host name, for example. The field “description” is added to the label comment. You can also use shortname, address, method, and desc as field names. (I think it is clear what is what). The following parameters are accepted:

For example, like this:
PS C:\temp>.\create-shortcuts.ps1 -source MyNewFriends.csv -folder d:\job\new –noreplace –namePolicy shortname_addr 

By default, data is taken from the source.csv file in the current directory, shortcuts are also created in the current directory.
Paths to putty and Internet Explorer are specified at the beginning of the script. Of course, for all methods of connection, you can use any other program. The main thing is to consider the parameter transfer method. For example, in mstsc, the host name for the connection is transmitted not simply through a space, but as / v: <address> .
Also, by itself, you can add other methods of connection. Here you have to edit the createShct function, section #Shortcut command .
Yes, if you are running the powershell script for the first time, do not forget to run:
 PS C:\temp>set-executionpolicy -executionpolicy unrestricted -scope currentuser 

Here is the script itself:
 #################################################### # Remote access shortcuts creation script # v0.9 # # Defaults: # # * Create shortcuts in current directory # * Overwrite all shortcuts # * Shortcut name is "shortname" column value # * Shortcut comment is "desc" column value #################################################### #      #  0.9 # #  : # # *      # *    # *   –  "" # *   -  "" #################################################### # Arguments param ( [switch]$noreplace, # 'Do not overwrite shortcuts on creation' default is to overwrite $folder = '', # 'Target folder path' default is current dir $source = 'source.csv', # 'Source data file path' default $namePolicy = 'shortname' # 'Shortcut naming policy' default ) $csvPath = $source # Source data file path $shPath = $folder # Target folder path $shNoReplace = $noreplace # Do not overwrite shortcuts on creation $shHTTPcmd = '"C:\Program Files\Internet Explorer\iexplore.exe"' $shRDPcmd = 'mstsc.exe' $shSSHcmd = '"C:\Program Files\PuTTY\putty.exe"' $shTELNETcmd = 'telnet.exe' $shNamePolicy = $namepolicy # Shortcut naming policy function createShctFile($shText,$shCmd,$shArgs, $desc = '') { # creating shortcut file $shPathSh = "$shPath\$shText.lnk" if ( (test-path -path $shPathSh) -and $shNoReplace ) {return} $shct = $oshell.CreateShortcut($shPathSh) $shct.TargetPath = $shCmd $shct.Arguments = $shArgs $shct.Description = $desc $shct.Save() } function createShct($shortname,$desc='',$addr,$method) { # preparing shortcurt parameters # Shortcut name $shText = $shortname if (!$shortname) { write-host '(i) No shortcut name defined' return } switch ($shNamePolicy) { 'shortname' { $shText = $shortname } 'shortname_addr' { $shText = "$shortname $addr" } 'addr_shortname' { $shText = "$addr $shortname" } 'shortname_lastoct' { $octs = ($addr -split '\.') if ($octs[3]) {$shText += ' ' + $octs[3]} } 'shortname_last2octs' { $octs = ($addr -split '\.') if ($octs[3]) {$shText += ' ' + $octs[2]+ '.' + $octs[3]} } } #Shortcut command $shArgs = '' switch ($method) { 'http' { $shCmd = $shHTTPCmd $shArgs = "http://$addr" } 'https' { $shCmd = $shHTTPCmd $shArgs = "https://$addr" } 'rdp' { $shCmd = $shRDPCmd $shArgs = "/v:$addr" } 'ssh' { $shCmd = $shSSHcmd $shArgs = $addr } 'telnet' { $shCmd = $shTELNETcmd $shArgs = $addr } } createShctFile -shText $shText -shCmd $shCmd -shArgs $shArgs -desc $desc } ##### Main # Init $oshell = New-Object -comObject WScript.Shell $basePath = (get-location).path # Working dir [System.IO.Directory]::SetCurrentDirectory($basePath) # Set working dir to script working dir # Env check if (!(test-path -pathtype leaf -path $csvPath)) { # Cheking for source CSV path write-host "(!) Path to source CSV not found: $csvPath" exit } if (!($shPath)) {$shPath = $basePath } if (!(test-path -pathtype container -path $shPath)) { # Cheking for target folder path write-host "(!) Path for shortcuts not found: $shPath" exit } # Run $csv = get-content $csvPath | Convertfrom-CSV -UseCulture foreach ($str in $csv) { $shrt = $str.shortname if ($str.) {$shrt = $str.} $addr = $str.addr if ($str.) {$addr = $str.} $accs = $str.method if ($str.) {$accs = $str.} $desc = $str.desc if ($str.) {$desc = $str.} createShct -shortname $shrt -desc $desc -addr $addr -method $accs } 

')

Source: https://habr.com/ru/post/118719/


All Articles