2

I need to extract client_TargetText and AnotherTargetText_110953_140521 from the results I get when running the whoami /groups command:

Powershell Results:

Group Name                                 Type             SID                                           Attributes
========================================== ================ ============================================= ==================================================
Everyone                                   Well-known group S-1-1-0                                       Mandatory group, Enabled by default, Enabled group
BUILTIN\Users                              Alias            S-1-5-32-545                                  Mandatory group, Enabled by default, Enabled group
NT AUTHORITY\INTERACTIVE                   Well-known group S-1-5-4                                       Mandatory group, Enabled by default, Enabled group
CONSOLE LOGON                              Well-known group S-1-2-1                                       Mandatory group, Enabled by default, Enabled group
NT AUTHORITY\Authenticated Users           Well-known group S-1-5-11                                      Mandatory group, Enabled by default, Enabled group
NT AUTHORITY\This Organization             Well-known group S-1-5-15                                      Mandatory group, Enabled by default, Enabled group
LOCAL                                      Well-known group S-1-2-0                                       Mandatory group, Enabled by default, Enabled group
MYDOMAIN\GGM-FIRE-PC                       Group            S-1-5-21-457414007-2867176591-488352320-6061  Mandatory group, Enabled by default, Enabled group
MYDOMAIN\myles_gp                          Group            S-1-5-21-457414007-2867176591-488352320-12531 Mandatory group, Enabled by default, Enabled group
MYDOMAIN\GGM-RDP                           Group            S-1-5-21-457414007-2867176591-488352320-13873 Mandatory group, Enabled by default, Enabled group
MYDOMAIN\client_TargetText                 Group            S-1-5-21-457414007-2867176591-488352320-7924  Mandatory group, Enabled by default, Enabled group
MYDOMAIN\AnotherTargetText_110953_140521   Group            S-1-5-21-457414007-2867176591-488352320-13947 Mandatory group, Enabled by default, Enabled group
Authentication authority asserted identity Well-known group S-1-18-1                                      Mandatory group, Enabled by default, Enabled group
Mandatory Label\Medium Mandatory Level     Label            S-1-16-8192
PS C:\Users\test.dev>

How would I go about grabbing client_TargetText and removing the client_ so it becomes just TargetText and then make this $Param1 in the script below? (client_ is always present in the text I need to grab for $Param1)

Then how would I go about grabbing AnotherTargetText_110953_140521 so it can be made into $Param2 in the script below? (The desired text for $Param2 is always suffixed by 6digits_6digits in these AD groups)

My goal is to somehow send this text over into a script I use for creating server shortcuts on user desktops.

The results will be different for each user when whoami runs so I think the only wildcards that can be used are client_ and 6digits_6digits. Any helping me towards finding this answer very much appreciated as I am trying to learn how this would work but can't figure it out.

The script below is the desired outcome after successfully parsing those two pieces of text over and turning them into $Param1 and $Param2:

function set-shortcut {
param ( [string]$SourceLnk, [string]$DestinationPath )
    $WshShell = New-Object -comObject WScript.Shell
    $Shortcut = $WshShell.CreateShortcut($SourceLnk)
    $Shortcut.TargetPath = $DestinationPath
    $Shortcut.Save()
    }

    try{

$Param1 = TargetText
$Param2 = AnotherTargetText_110953_140521
$SourcePath = \\server\data\designs\$Param1\$Param2\data_store"

set-shortcut "%USERPROFILE%\Desktop\data_store.lnk" "$SourcePath"

     "This worked"
     
pause
}

catch
   {
    
    "This didn't work"
}
pause
3
  • 1
    take a look at the Get-LocalUser cmdlet. it lists the groups the account is a member of. then you can filter for your pattern in the .Name property. once you have the group name as text, just do a -replace OR a .Split() as you prefer.
    – Lee_Dailey
    Commented Aug 3, 2021 at 22:34
  • Thank you Lee I completely forgot about Get-LocalUser I will see into that
    – Mastaxx
    Commented Aug 4, 2021 at 9:19
  • you are most welcome! glad to have helped a little bit ... [grin]
    – Lee_Dailey
    Commented Aug 4, 2021 at 14:48

1 Answer 1

2

If this is being ran with PowerShell:

  1. Let's use the proper opening, and closing quotes ("").
  2. Let's also use the proper environment variables: $env:USERPROFILE.

I really couldn't make too much sense in what you were trying to accomplish, so I did what I think you meant. As far as your parameters for your function, that i'm still lost on.

function Set-ShortCut {
    Param ( 
        [string]$SourceLnk, 
        [string]$DestinationPath 
    )
        
        $WshShell = New-Object -comObject WScript.Shell
        $Shortcut = $WshShell.CreateShortcut($SourceLnk)
        $Shortcut.TargetPath = $DestinationPath
        $Shortcut.Save()

}


$Groups = Whoami /Groups /FO CSV | ConvertFrom-Csv

$Param1, $Param2 = $Groups.'Group Name'.Where{
    
        $_ -match 'client_' -or `
        $_ -match '(_\d{6}){2}'

    } -replace 'client_|MYDOMAIN\\'



$SourcePath = "\\server\data\designs\$Param1\$Param2\data_store"

Set-ShortCut -SourceLnk "$env:USERPROFILE\Desktop\data_store.lnk" -DestinationPath $SourcePath

Luckily, whoami has a /FO CSV option which we can use to convert it to a proper PowerShell object using ConvertFrom-Csv. After the object conversion, we can use some Regex matching to get what you're after assuming that's exactly how the names are displayed. Please note the double assignment of $Param1, and $Param2 as it splits the results into those two variables. Also, in trying to avoid the pipeline, I used the .Where()\{} operator (yes, it's considered an operator) for faster parsing.

I would suggest in looking into the the cmdlet mentioned by @Lee_Daily, to include the method he provide in getting the results you're after.

EDIT: You can try separating the filtering into two variables for explicit filtering. Although not the best solution, it should work:

function Set-ShortCut {
    Param ( 
        [string]$SourceLnk, 
        [string]$DestinationPath 
    )
        
        $WshShell = New-Object -comObject WScript.Shell
        $Shortcut = $WshShell.CreateShortcut($SourceLnk)
        $Shortcut.TargetPath = $DestinationPath
        $Shortcut.Save()

}

$Groups = WhoAmI /Groups /FO CSV | ConvertFrom-Csv

$Param1 = $Groups.'Group Name'.Where{ $_ -match 'client_' } -replace 'client_|MYDOMAIN\\'
$Param2 = $Groups.'Group Name'.Where{ $_ -match '(_\d{6}){2}' } -replace 'MYDOMAIN\\'

$SourcePath = "\\server\data\designs\$Param1\$Param2\data_store"

Set-ShortCut -SourceLnk "$env:USERPROFILE\Desktop\data_store.lnk" -DestinationPath $SourcePath
8
  • 1
    Thank you Abraham for the lesson there. The script now works as intended. Thanks.
    – Mastaxx
    Commented Aug 4, 2021 at 9:20
  • Awesome man, glad it worked first try. Commented Aug 4, 2021 at 11:27
  • 1
    @Mastaxx, see my edit. Commented Aug 4, 2021 at 12:00
  • 1
    That's done it. My version of attempting that was way off! Thanks alot for that!
    – Mastaxx
    Commented Aug 4, 2021 at 12:16
  • 2
    @AbrahamZinala - you can leave off the backtick after the -or since PoSh knows the operator is followed by "more stuff". [grin]
    – Lee_Dailey
    Commented Aug 4, 2021 at 14:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .