header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Splitting A String
Last Post 02 Sep 2010 06:47 AM by James. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
JamesUser is Offline
Basic Member
Basic Member
Posts:374
Avatar

--
02 Sep 2010 02:52 AM

    Hello,

    I am trying to get the first letter of the string and hold that in a variable for use later on in my code.

    I have tried split which will split into an array for me which is fine however I require just the first letter

    Does anyone know how I can do this? I have tried using regular expressions however they are not working and I am not too good with them wither.

    Can anyone assist?

    My code before the split is:



    Get-QADUser -Title "teacher" , "Staff" , "Parent" |

    ForEach-Object {

    $UserName = $_.givenName + "." + $_.sn

    $Count = $UserName.Length

    if($Count -le "20") {

    Set-QADUser -Identity $UserName `
                -UserPrincipalName $UserName + "@" + $School + ".domain.com" `
                -SamAccountName $UserName
    else {
    $FLetter = $UserName.Split("^")
    $NewName = $FLetter + "." + $_.GivenName

    Set-QADUser -Identity $NewName ` 
                            -UserPrincipalName $NewName + "@" + $School + ".domain.com" ` 
                            -SamAccountName $NewName

    }

    }
    }



    Many Thanks

    James

    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1641
    Avatar

    --
    02 Sep 2010 05:16 AM
    All you need is the first character?

    How about:
    PS>$first="test".substring(0,1)
    PS>$first
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    02 Sep 2010 05:26 AM
    PS > $first = "string"[0]
    PS > $first
    s

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    02 Sep 2010 06:25 AM
    Hello,

    Works like a dream!

    Thank you so much!

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    02 Sep 2010 06:47 AM
    Hello,

    Just one more question on this...

    With my new code of (used for testing) :

    Get-QADUser -Identity longforenametest.longsurnametest | ForEach-Object { $Global:User = $_.Name $Global:UserName = $_.givenName + "." + $_.sn $Global:Count = $UserName.Length $DN = $_.DN $dn1 = $DN -replace "CN=.*,OU=Users,OU=","" $School = $dn1 -replace ",OU=foo1,DC=foo,DC=domain,DC=com","" if($Count -lt "21") { $UPN = $UserName + "@" + $School + ".domain.com" Set-QADUser -Identity $User ` -UserPrincipalName $UPN ` -SamAccountName $UserName } if($Count -gt "20"){ $FLetter = $UserName[0] $NewName = $FLetter + "." + $_.sn $NewUPN = $NewName + "@" + $School + ".domain.com" Set-QADUser -Identity $User ` -UserPrincipalName $NewUPN ` -SamAccountName $NewName } }

    When I run it I get the following error:


    Name Type DN
    ---- ---- --
    longforena.longsurna user CN=longforena.longsurna,OU=Us...
    Set-QADUser : The object already exists. (Exception from HRESULT: 0x80071392)
    At line:19 char:12
    + Set-QADUser <<<< -Identity $User `
    + CategoryInfo : NotSpecified: (:) [Set-QADUser], DirectoryServic
    esCOMException
    + FullyQualifiedErrorId : System.DirectoryServices.DirectoryServicesCOMExc
    eption,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.SetUserCmd
    let

    Does anyone know why?

    Many Thanks

    James
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer