Mark Minasi's Tech Forum
Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
JeffHicks

Avatar / Picture

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 35
Reply with quote  #1 
We hijacked another thread about strings and the -F operator so I thought I'd start something new. I just posted an article on my blog that includes a demonstration of using the -F operator in a very narrow use case.

http://bit.ly/1KhkJEm 

__________________
Jeff Hicks Author ~ Trainer ~ Guru
Cloud and Datacenter Management MVP



0
Mark

Hacked Mark's Facebook Account
Registered:
Posts: 273
Reply with quote  #2 
Hey, I never said it wasn't PRACTICAL, Jeff... I totally get what it does.  It just looks like Martian and I am FINE with that... it just means that I won't get into it until I've got a couple of hours to wrap my head around it.  [smile]  Thanks!
__________________
May I ask that everyone please populate the first name and last name in your user account profile.  Thanks!
0
JeffHicks

Avatar / Picture

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 35
Reply with quote  #3 
Well as I always tell people, you can't learn Martian unless you use it every day.[comp]
__________________
Jeff Hicks Author ~ Trainer ~ Guru
Cloud and Datacenter Management MVP



0
Mark

Hacked Mark's Facebook Account
Registered:
Posts: 273
Reply with quote  #4 
Klaatu barata niktu, my friend. [smile]


__________________
May I ask that everyone please populate the first name and last name in your user account profile.  Thanks!
0
Infradeploy

Avatar / Picture

Senior Member
Registered:
Posts: 179
Reply with quote  #5 
For me i use the -F when i want a sequential number as a string at a length i want. Especially for naming conventions.



$Otap= "T"
$VMS = Get-Azurevm |? name -like "SRV$($otap)*"|select -expand name
foreach ($vm in $vms)
{
    [int]$number = $vm.substring(4)
    [array]$arr += $number
}
$new = ($arr |sort -Descending)[0] + 1
$Servicename = "SRV$($otap)$("{0:D4}" -f $new)"



Edit: better post that in here

__________________
Have SpaceSuit, Will Travel

0
Mark

Hacked Mark's Facebook Account
Registered:
Posts: 273
Reply with quote  #6 
And that does what, Ton -- take the integer and right-justify it in a four-character space, with spaces filled from the left?  Or does it left-fill with zeros?  

And could you pick this line apart please?  

$Servicename ="SRV$($otap)$("{0:D4}" -f $new)"

I'm just not following what it does or how it's doing it.

Thanks!

__________________
May I ask that everyone please populate the first name and last name in your user account profile.  Thanks!
0
JeffHicks

Avatar / Picture

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 35
Reply with quote  #7 
One thing to be aware of the for qualifiers like 'D' when using the -f operator is that they are case sensitive. In this particular case he is generating an integer like 5 for $new. He is then formatting it as a with a length of 4 characters padded with leading zeros.  Here's a variation that breaks it down a bit further.


$Otap= "T"
"Otap = $oTap "
$new = 5
"New = $new" 
$myNumber = "{0:D4}" -f $new
"MyNumber = $MyNumber"
$myNumber
$Servicename = "SRV$($otap)$MyNumber"
"Servicename = $Servicename"


When run you get this:

Otap = T
New = 5
MyNumber = 0005
0005
Servicename = SRVT0005



He is using the -F operator to fulfill a special use case, which I think is perfectly fine.  Change the value of $new to see how this behaves.


__________________
Jeff Hicks Author ~ Trainer ~ Guru
Cloud and Datacenter Management MVP



0
JeffHicks

Avatar / Picture

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 35
Reply with quote  #8 
I should point out that in this case D or d doesn't make a difference. But "{0:g}" -f (get-date) is different than "{0:G}" -f (get-date). He could also have gotten the same result using the PadLeft() method on the string object:

$new.tostring().padleft(4,"0")

__________________
Jeff Hicks Author ~ Trainer ~ Guru
Cloud and Datacenter Management MVP



0
Infradeploy

Avatar / Picture

Senior Member
Registered:
Posts: 179
Reply with quote  #9 
What Jeff said. Basically with the -f I create a string from a number and I make sure it's length is 4 chars, leading with zero's..
The first zero in 0[biggrin]4 is the index, D stands for decimal and 4 is the number of chars

__________________
Have SpaceSuit, Will Travel

0
Mark

Hacked Mark's Facebook Account
Registered:
Posts: 273
Reply with quote  #10 
Thanks!  That'll be a great example to play with.  
__________________
May I ask that everyone please populate the first name and last name in your user account profile.  Thanks!
0
JeffHicks

Avatar / Picture

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 35
Reply with quote  #11 
I have a module on GitHub with commands that uses the -F operator if you want to see other examples. https://github.com/jdhitsolutions/FormatFunctions
__________________
Jeff Hicks Author ~ Trainer ~ Guru
Cloud and Datacenter Management MVP



0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.