Author Posts

January 1, 2012 at 12:00 am

by Uelidetumm at 2012-08-22 02:46:49

Hello everyone

I have some medium-difficult question...

I have to compare username with a list and i am not sure how i can make it work most simply.

First i get the Username:
xx0blabla

Now i have to make the folder
xendxen/xx0blabla

To find out, where i have to make the folder, i have to compare the first two letters from the Username (xx) with a list.
The list looks like this:
xx=xendxen
yy=yayon
zz=zananja
..

Thats my solution idea:
$homedir = "\\filecluster\users$\"
$username = [Environment]::UserName
$profiledir = "\\filecluster\profiles$\"
$users_profile = join-paths @($profiledir, $mainfolder, $username)

What i need now is how to get the mainfolder.
Any Ideas?
Need more information?

Ueli

by poshoholic at 2012-08-22 08:16:25

Hi Ueli,

It sounds like you need a look-up hashtable. Based on the information you provided, I would define it like this:

[script=powershell]$mainFolderTable = @{
xx = 'xendxen'
yy = 'yayon'
zz = 'zananja'
}[/script]

With this defined, all you need to do is find the appropriate hash table entry by looking at the first two characters. You can do that like this:

[script=powershell]$prefix = -join $username[0..1][/script]

With the prefix in hand, lookup in a hashtable is easy.

[script=powershell]$mainFolder = $mainFolderTable[$prefix][/script]

You probably want to make sure that you raise an error if the table doesn't find a match though. Something like this:

[script=powershell]if (-not $mainFolderTable.ContainsKey($prefix)) {
throw "Prefix $prefix was not found in the lookup table."
}[/script]

That should give you what you were looking for.

by Uelidetumm at 2012-08-23 04:07:58

Thanks for this very usefull answer!
Based on your detailed description, i even could follow your instructions. I understand now what i do 🙂

If i want to take the first three letters from the username, do i have to do this?
$prefix = -join $username[0..2]

by DexterPOSH at 2012-08-23 07:02:31

Another very cool tip.
This solved one of the problems i faced 🙂

Thanks Kirk.

by poshoholic at 2012-08-23 07:23:21

Yes Ueli, to increase the number of characters you take for the prefix to 3, just increase the second index in the range to 2 like you did in your reply.

Ranges in PowerShell are quite useful, allowing for all sorts of things like this:

PS C:\> $str = 'Hello'
# Show the string in reverse
PS C:\> -join $str[$str.Length..0]
oellH
# Show the string in reverse using negative indices instead
PS C:\> -join $str[-1..-$str.Length]
oellH
# And mix things up a little bit
PS C:\> -join $str[-2..2]
loHel

As shown, negative numbers are allowed, as are variables or object properties, giving a lot of flexibility when identifying prefixes/suffixes or when trying to manipulate strings.

Dexter, thanks, glad I'm helping. 🙂

by Uelidetumm at 2012-08-27 06:32:41

Hello, its me again...

I have now a little Problem. I want to add the path with a join-path command, but it won't work.

I try this:
$homedir = "\\filecluster\users$\"
$username = [Environment]::UserName
$usernameNew = [Environment]::UserName
$usernameNew += ".v2"
# get the name
$mainFolderTable = @{
xx = 'xendxen'
yy = 'yayon'
zz = 'zananja'
}
$prefix = -join $username[0..1]
$mainFolder = $mainFolderTable[$prefix]
$profiledir = "\\filecluster\profiles$\"
$users_profileOld = join-path @($profiledir, $mainFolder, $username)
$users_profileNew = join-path @($profiledir, $mainFolder, $usernameNew)

echo msgbox "Parameter initialised."
# Copy useless Data
$FolderOld = (join-path @($users_profileNew, "Application Data", "PI"))
$FolderNew = (join-path @($users_profileNew, "AppData", "Roaming"))
$okay = new-item -type directory $FolderNew
$okay = copy-item (join-path @($profiledir, $mainFolder, $FolderOld)) ($profiledir, $mainFolder, $FolderNew)

The Errormessage sounds like:

Join-Path : "System.Object[]" can not be converted to "System.String", wich is needed for "Path". The detected method is not supported.

by poshoholic at 2012-08-27 13:07:26

Join-Path only joins two path segments together at a time, not more. Therefore what you should do is something like this:

[script=powershell]# Note, only joining two pieces together at a time.
$mainFolderPath = Join-Path -Path $profileDir -ChildPath $mainFolder
$users_profileOld = Join-Path -Path $mainFolderPath -ChildPath $username
$users_profileNew = Join-Path -Path $mainFolderPath -ChildPath $usernameNew
# And so on...[/script]
You can also combine multiple paths with Join-Path all at once, like this:

[script=powershell]# Note, I'm combining the string paths together first then joining them to the path
$FolderOld = Join-Path -Path $users_profileNew -ChildPath 'Application Data\PI'
$FolderNew = Join-Path -Path $users_profileNew -ChildPath "AppData\Roaming"[/script]
Last, when you copy your data, be careful, you have some unusual plans in your script as is. I think you just want to copy from $FolderOld to $FolderNew since you already have full paths at that point (no need to use Join-Path there).

by Uelidetumm at 2012-08-29 00:38:46

Ah nice, now it works without problems. im so happy 🙂
Thank you so much for your help.

Lg Ueli

by poshoholic at 2012-08-29 06:06:16

I'm glad that worked out your remaining issues. 🙂