Michael's profileThe Old Dogs Scripting B...BlogListsNetworkMore Tools Help

Blog


    August 12

    Powershell InStr() and Mid()

    First of all, PowerShell does not have an InStr() or Mid() function, at least version one does not.
     
    I was trying to extract the Group name from the Common Name in AD and I wanted
    everything between the first = sign and the first "," comma. (I know there are other ways)
     
    So I found that in Powershell you can get the first position of a character by using indexof() like so:
     
    $a=$path
    $b = $a.indexof("=")     # so the first = is at character 9
     
    I don't want the equal sign so I add 1
    $b = $b + 1

    Next I get the index of the first comma.
    $c = $a.indexof(",")
    Then I get the number of characters between $c and $b
    $d = $c - $b
     
    Finally I get the substring I wanted:
    substring starts at the first character, in this case $b and then includes
    the number of characters in your substring, in this case $d.
     
    $e = $a.substring($b,$d)
     
    And here is my MID function:
     
    Function MID($path) {
    $a=$path
    $b = $a.indexof("=")
    $b = $b + 1
    $c = $a.indexof(",")
    $d = $c - $b
    $e = $a.substring($b,$d)
    $ws.Cells.Item($row,4) = $e      #This puts my substring in an Excel Spreadsheet in column 4
    }
     
     
     

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://olddogsblog.spaces.live.com/blog/cns!C2DB05EEFA6C21A1!239.trak
    Weblogs that reference this entry
    • None