Creating a shortcut Powershell

jasieltego

Member
Joined
Oct 5, 2021
Posts
15
Hi All,

I'm facing an issue here.
I know how to replace and export information from Attribute editor in AD.
However I'm tasked with whatever the value is, create a shortcut in the desktop that resides in the attribute for the particular user.
Allow me to expand.


In this random case I chose to use the AD Attribute destination indicator. (I know I can create a custom one)
In there I use a unc path like \\share\folder (This far, we know the Destination Indicator Attribute has a value of \\share\folder) This path does exist in testing
with Powershell I want to create a script that will create a shortcut of whatever is in the attribute field destination indicator.

I know how to create a shortcut with powershell, but not how to create one based on whats on a certain attribute. Can someone help me or point me in the right direction.
Thanks
 
Nevermind, I understand now. I'd never heard of Destination indicator attribute. So just warning you I'm a student and I've only done a bit of powershell but it's mostly been AD and Exchange server management stuff so I know a bit. I'm always looking for ways to improve my skills though. Dont worry if you've already figured out a solution, just let me know what it was? I'm running this on one of my DC VMs for a class.

So im not sure where you will be running this script (login script on the user?) so the way you get the path will have to vary. In the simplest case I'm working on the DC. I use $path = Get-ADUser -Identity whatever -Properties * | Select-Object destinationIndicator to get the desktination indicator. Now here's where it gets stupid. I'm sure theres a better way to do this but the shortcut will only take a string (and other stuff probably but we'll do string so we have to change the output type to string. $pathString = Out-String -InputObject $path which gives me this output.

1637462804011.png

Ew. Nasty. So now it's time to trim the string. This is when it gets ugly. I wanna get rid of everything but the path so i do
Code:
$pathString = $pathString.Substring(57)
$pathString = $pathString.Trim()
$pathString = $pathString.TrimEnd("}")
May need to change some values but the substring part cuts out everything in front of the actual path, but while you can make it cut characters away from the end as well I couldn't get it to work because paths can all be different sizes. Other string manipulation methods could work. Then it trims all the whitespace and gets rid of the final bracket. Now you have a normal path in string form.

I'm assuming the shortcut will be on the desktop, so then all you need to do is somehow get the path to desktop. I'm going by a lot of assumptions here though, so I'm not sure how helpful I'm being. Where will you be running this script? How automated do you want it? I'm assuming logon script, so the first bit of the script would have to grab the user's account name which you can use to grab the attribute path and put the shortcut on the desktop. For querying the AD though you'll need something in your script that can do that which I can try and help with as well, if that's the way you're going about it.
 
One thing I will inject here is that since PowerShell includes the ability to use Regular Expressions (often called regex) you can do pattern matching and stripping parts you want in a much more compact manner once you master the syntax necessary. That three line "ugly" can be replaced with this single line:

Code:
$pathString = $pathString -replace '\{(.+)\}','$1'

The part that you want is captured in the part of the regex (.+) and the $1 is what gives you only that part back.

Regular expressions are something that take a while to wrap your head around, and a lot of practice and testing even once you understand the syntax to make sure you're getting what you want, and only what you want, as it's easy to screw them up if you're not careful. But the power that is built into regular expressions for pattern matching and parsing is, no pun intended, unmatched.
 
Quick note, the preceding assumes that the original pathString variable only contains the curly brace enclosed path. I tested as follows:

Code:
$pathString='{\\DAMIEN\UserDocs\Black\}'
$pathString = $pathString -replace '\{(.+)\}','$1'
echo $pathString

There are ways to manipulate the regular expression if there is something else contained in front of that first left curly brace that would need to be matched, but tossed away. Right now it's only the left curly brace that's being matched and tossed, as is the right one, for that matter.
 
Quick note, the preceding assumes that the original pathString variable only contains the curly brace enclosed path. I tested as follows:

Code:
$pathString='{\\DAMIEN\UserDocs\Black\}'
$pathString = $pathString -replace '\{(.+)\}','$1'
echo $pathString

There are ways to manipulate the regular expression if there is something else contained in front of that first left curly brace that would need to be matched, but tossed away. Right now it's only the left curly brace that's being matched and tossed, as is the right one, for that matter.
Thanks! I learn something new every day. I'm gonna have to start playing around with regular expressions now. I've always struggled with things like that (and arrays, nearly failed programming fundamentals because I couldn't understand arrays in C#) but it's always a good goal to make the code as efficient as possible.
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top