locked
Format a Variable with 01, 001 RRS feed

  • Question

  • Hi all

    I have a SharePoint site which orchestrator pulls the data from. The user types in a computer name and computer number. However due to the mess AD is in and people not using the naming standards they often use 1, 01 or 001 for the number. I have a script that checks the name against AD and if it exists if adds 1 and checks AD again until it finds a server name and number that doesn't exist. My issue is I cant get the variable to display what they entered in the number field with the leading zeros. I know "{0:D2}" -f [int]$Number will display the number based on value of D2 to show the leading zeros, but my problem is I don't know how many leading zeros they entered without looking at the SharePoint site. Is a trick to get the variable to show exactly what was entered without using a field that allows text?

    Example

    Server Name = "Server"
    Number = "001"

    $Number = "1"

    I was going to use code to count the 0s and use If statement to send it down the correct route of {0:D2} or {0:D3} and create a range.

    Any help would be appreciated

    I know someone will just say "Get them to use the standards", but to keep the mess neater I need this to work.


    • Edited by ktitchard Tuesday, August 13, 2019 10:31 AM
    Tuesday, August 13, 2019 10:29 AM

Answers

  • This will count only the left side zeros:

    [regex]::Matches($Number,'0+')[0].Value.Length


    \_(ツ)_/


    • Edited by jrv Tuesday, August 13, 2019 12:58 PM
    • Marked as answer by ktitchard Tuesday, August 13, 2019 1:20 PM
    Tuesday, August 13, 2019 12:54 PM

All replies

  • This is simple to do with a regex:

     ('jsmit123456') -replace '\D+(\d+)','$1'
    
     123456

    \D+ = all the non-digits, \d+ = all the digits that follow.



    • Edited by asharma5 Tuesday, August 13, 2019 11:42 AM
    Tuesday, August 13, 2019 11:42 AM
  • Thanks, I've done this script, its a WIP

    [string]$Number = "003"
    [INT]$Integer = [INT]$Number
    
    $ZeroCount = ([regex]::Matches($Number, "0" )).count
    
    if ($ZeroCount -eq 0) {
    
        Write-host -ForegroundColor cyan "The Zero Count is None"
    
        $0 = "{0:D2}"
    
        }ElseIf ($ZeroCount -eq 1){
    
            Write-host -ForegroundColor cyan "The Zero Count is 1"
    
            $0 = "{0:D2}"
    
    
            }ElseIF ($ZeroCount -eq 2) {
    
                Write-host -ForegroundColor cyan "The Zero Count is 2"
    
                $0 = "{0:D3}"
    
        }
    
    $Integer ++
    
    $output = "$0" -f $Integer
    
    $output





    • Edited by ktitchard Tuesday, August 13, 2019 12:23 PM
    Tuesday, August 13, 2019 11:49 AM
  • Won't work for most numbers:

    [string]$Number = "003000"
    ([regex]::Matches($Number, "0" )).count  # now it is 5

    You need to limit the count to the left side only.


    \_(ツ)_/

    Tuesday, August 13, 2019 12:51 PM
  • This will count only the left side zeros:

    [regex]::Matches($Number,'0+')[0].Value.Length


    \_(ツ)_/


    • Edited by jrv Tuesday, August 13, 2019 12:58 PM
    • Marked as answer by ktitchard Tuesday, August 13, 2019 1:20 PM
    Tuesday, August 13, 2019 12:54 PM
  • This does it all in one line:

    $number = '0003001'
    "{0:D$($number.Length)}" -f [int]$number

    You can calculate the format elements as needed.


    \_(ツ)_/

    Tuesday, August 13, 2019 1:03 PM
  • Cheers Pal

    Currently reading up on this subject.

    works!

    [string]$Number = "19"
    [INT]$Integer = [INT]$Number
    
    $ZeroCount = [regex]::Matches($Number,'0+')[0].Value.Length
    
    if ($ZeroCount -eq 0) {
    
        Write-host -ForegroundColor cyan "The Zero Count is None"
    
        $0 = "{0:D1}"
    
        }ElseIf ($ZeroCount -eq 1){
    
            Write-host -ForegroundColor cyan "The Zero Count is 1"
    
            $0 = "{0:D3}"
    
    
            }ElseIF ($ZeroCount -eq 2) {
    
                Write-host -ForegroundColor cyan "The Zero Count is 2"
    
                $0 = "{0:D3}"
    
        }
    
    $Integer ++
    
    $output = "$0" -f $Integer
    
    $output


    • Edited by ktitchard Tuesday, August 13, 2019 1:30 PM
    Tuesday, August 13, 2019 1:21 PM