Triming

This topic contains 10 replies, has 5 voices, and was last updated by Profile photo of Mark Prior Mark Prior 5 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #38234
    Profile photo of Mark Prior
    Mark Prior
    Participant

    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

    #38235
    Profile photo of Anthony Stringer
    Anthony Stringer
    Participant

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

    #38236
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

    #38237
    Profile photo of Mark Prior
    Mark Prior
    Participant

    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

    #38239
    Profile photo of random commandline
    random commandline
    Participant

    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
    }}
    
    #38240
    Profile photo of Mark Prior
    Mark Prior
    Participant

    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

    #38241
    Profile photo of random commandline
    random commandline
    Participant

    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.

    #38244
    Profile photo of Mark Prior
    Mark Prior
    Participant

    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

    #38246
    Profile photo of random commandline
    random commandline
    Participant

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

    #38257
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

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

    #38282
    Profile photo of Mark Prior
    Mark Prior
    Participant

    Thanks all have this working how i need now

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.