Author Posts

April 26, 2016 at 5:30 am

Hi All , having a day where my head just cant look at code with sense

i have a csv file that contains a bunch of names, i want to capture the first 20 characters only

$Users = Import-Csv "h:\usersCAL.csv" |Out-String

$string1 = foreach ($user in $users) {$user.Substring(0,10)}

have a feeling im way off

April 26, 2016 at 5:50 am

i think you may just need to remove |Out-String

April 26, 2016 at 5:57 am

Remove Out-String, because it's turning your structured data into a flat string. And, keep in mind that the way you've written this, $string1 is going to contain a collection of strings. Make sure that's what you want.

April 26, 2016 at 6:05 am

thanks both removing out-string give the below error
Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named 'Substring'.
At line:3 char:39
+ $string1 = foreach ($user in $users) {$user.Substring(0,10)}
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Substring:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

what i am after is , passing names to an exchange command full context below (looks at samaacount name so hence 20 letters restriction

$Users = Import-Csv "h:\usersCAL.csv"
foreach ($User in $Users)
{
$user.Substring(0,20)
    
    add-MailboxFolderPermission -Identity Climate-ChangeEvents:\Calendar -user $User.users -AccessRights "editor"
}

Csv data looks like

Users
Gemma.Andres
Bethan.Lawson
Michael.Baker
Lokeshwar.Vohra
Marialina.Klokidi
Ewan.Frost-Pennington
Daniel.Adegbie
Yke.Wynia
Laura.Hill
Zach.Wilcox
Aude.Lucien
Charlotte.Graves
Annie.Gibbons
Lewis.Fox-James
Paul.Garbett

April 26, 2016 at 6:12 am

It looks like you are using a column header (Users). That is why you are getting the error. You need to take one more step and work with type: string not pscustomobject.

foreach ($user in $users) {
    If ($user.users.length -ge 20){
    $username = $user.users.Substring(0,20)
    Add-MailboxFolder... -User $username} Else {
    Add-MailboxFolder... -User $user.users
}}

April 26, 2016 at 6:26 am

Hi Random

i get this when adding that extra part

$Users = Import-Csv "h:\usersCAL.csv"
foreach ($User in $Users)
{
$user.users.Substring(0,20)
    
   add-MailboxFolderPermission -Identity Climate-ChangeEvents:\Calendar -user $User.users -AccessRights "editor"
}

Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string.
Parameter name: length"
At H:\EAA\Powers Shell Commands\For Each Example (CSV)Multiple Calendar Perms.ps1:4 char:1
+ $user.users.Substring(0,20)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentOutOfRangeException

April 26, 2016 at 6:37 am

I modified my previous comment. One of your users' length is longer than 20 characters (Ewan.Frost-Pennington : 21). It will generate an error because of that. You can add an If statement to check user length.

April 26, 2016 at 6:47 am

yeah the idea is to ignore everything past the 20 character mark changeing:

Ewan.Frost-Pennington
to
Ewan.Frost-Penningto

hmm even just running this part i get errors,

$Users = Import-Csv "h:\usersCAL.csv"
foreach ($user in $users) {
    If ($user.users.length -le 20){
    $user.users.Substring(0,20)}}

Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string.
Parameter name: length"
At line:4 char:5
+ $user.users.Substring(0,20)}}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentOutOfRangeException

April 26, 2016 at 7:16 am

Sorry about that, I was confused. Try my previous comment again.

April 26, 2016 at 9:06 am

You only want to trim the string if it is -gt 20, not -le.

April 27, 2016 at 12:53 am

Thanks all have this working how i need now