Michael's profileThe Old Dogs Scripting B...BlogListsNetworkMore ![]() | Help |
|
|
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 } TrackbacksThe trackback URL for this entry is: http://olddogsblog.spaces.live.com/blog/cns!C2DB05EEFA6C21A1!239.trak Weblogs that reference this entry
|
|
|