locked
If statement to run a portion of an existing script over again.... RRS feed

  • Question

  • Forgive me if this is such a noobish question...but I am pretty much a beginner at PS scripting.  I have the following script that  requests credentials and establishes a connection to my Exchange server then requests user input for a "job number" and creates a public folder with email enabled and also creates a set of transport rules for the folder.  It works wonderfully

    Function Establish-EXCConnection
    {
    
    $Usercredential = Get-Credential
    
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mail.server.com/Powershell/ -Authentication Kerberos -Credential $UserCredential
    Import-PSSession $Session
    }
    
    Function Add-JobFolder
    {
    
    $jobshort = $jobnumber -replace "[^0-9]",''
    
    new-PublicFolder -name "$jobshort" -path "\email job file"
    enable-mailpublicfolder "\email job file\$jobshort"
    set-mailpublicfolder "\email job file\$jobshort" -HiddenFromAddressListsEnable $true
    New-TransportRule -Name "$jobshort" -SubjectContains "$jobshort","$jobnumber" -copyto "$jobshort@lobar.com"
    
    }
    
    Establish-EXCConnection
    $jobnumber = read-host -Prompt "Enter Job Number with XX-XXX Format"
    Add-JobFolder $jobnumber

    My issue is when I have multiple job numbers or folders that I need to create.  I would have to exit the power shell session and re-establish a connection for each job number I have to create. 

    How do I create an if statement (I think that is what I need) that will prompt the user after finishing if they have more job numbers to enter with a Yes or No....then run the script again?

    Thanks in advance for any help anyone can give!


    • Edited by seanryan52 Friday, March 21, 2014 12:41 PM
    Friday, March 21, 2014 12:39 PM

Answers

  • Hi Sean,

    Welcome to Powershell Scripting.

    You can replace your final 2 lines with a simple while loop:

    $test = $true
    While ($test)
    {
    	$jobnumber = read-host -Prompt "Enter Job Number with XX-XXX Format, or exit to exit"
    	if ($Jobnumber -like "exit"){$test = $false}
    	else {Add-JobFolder $jobnumber}
    }

    This way, users will keep being prompted for JobNumbers until they enter exit.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Proposed as answer by Mike Laughlin Friday, March 21, 2014 1:09 PM
    • Marked as answer by seanryan52 Friday, March 21, 2014 1:23 PM
    Friday, March 21, 2014 12:56 PM

All replies

  • Hi Sean,

    Welcome to Powershell Scripting.

    You can replace your final 2 lines with a simple while loop:

    $test = $true
    While ($test)
    {
    	$jobnumber = read-host -Prompt "Enter Job Number with XX-XXX Format, or exit to exit"
    	if ($Jobnumber -like "exit"){$test = $false}
    	else {Add-JobFolder $jobnumber}
    }

    This way, users will keep being prompted for JobNumbers until they enter exit.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Proposed as answer by Mike Laughlin Friday, March 21, 2014 1:09 PM
    • Marked as answer by seanryan52 Friday, March 21, 2014 1:23 PM
    Friday, March 21, 2014 12:56 PM
  • Thank you!  I will edit my script accordingly.  Sorry for posting such an easy one.
    Friday, March 21, 2014 1:06 PM
  • Slightly more readable, IMO:


    while ( $true ) {
      $jobnumber = read-host -Prompt "Enter Job Number with XX-XXX Format, or exit to exit"
      if ( $jobnumber -eq "exit" ) {
        break
      }
      Add-JobFolder $jobnumber
    }
    


    -- Bill Stewart [Bill_Stewart]

    Friday, March 21, 2014 2:41 PM
  • True, and it does have the virtue of demonstrating the break command.

    Though I'd still advocate using the "-like" logical operator over "-eq", to make the query non-case-sensitive :)


    There's no place like 127.0.0.1

    Friday, March 21, 2014 2:47 PM
  • -eq is not case-sensitive for strings by default.


    PS C:\> "foo" -eq "FOO"
    True
    


    -- Bill Stewart [Bill_Stewart]

    Friday, March 21, 2014 2:52 PM
  • Though I'd still advocate using the "-like" logical operator over "-eq", to make the query non-case-sensitive :)

    -eq should still handle multiple cases:

    $in = Read-Host 'Exit vs exit vs EXIT'
    If ( $in -eq 'exit') { Write-Host 'EXIT!!!!' }
    Else { Write-Host 'Not gonna do it' }
    
    
    Example runs:
    PS C:\> $in = Read-Host 'Exit vs exit vs EXIT'
    If ( $in -eq 'exit') { Write-Host 'EXIT!!!!' }
    Else { Write-Host 'Not gonna do it' }
    Exit vs exit vs EXIT: ExIt
    EXIT!!!!
    
    PS C:\> $in = Read-Host 'Exit vs exit vs EXIT'
    If ( $in -eq 'exit') { Write-Host 'EXIT!!!!' }
    Else { Write-Host 'Not gonna do it' }
    Exit vs exit vs EXIT: EXIT
    EXIT!!!!
    
    PS C:\> $in = Read-Host 'Exit vs exit vs EXIT'
    If ( $in -eq 'exit') { Write-Host 'EXIT!!!!' }
    Else { Write-Host 'Not gonna do it' }
    Exit vs exit vs EXIT: exit
    EXIT!!!!
    
    PS C:\> $in = Read-Host 'Exit vs exit vs EXIT'
    If ( $in -eq 'exit') { Write-Host 'EXIT!!!!' }
    Else { Write-Host 'Not gonna do it' }
    Exit vs exit vs EXIT: aww
    Not gonna do it

    You can use -ceq if you want to be explicit about case though:

    PS C:\> $in = Read-Host 'Exit vs exit vs EXIT'
    If ( $in -ceq 'exit') { Write-Host 'EXIT!!!!' }
    Else { Write-Host 'Not gonna do it' }
    
    
    Exit vs exit vs EXIT: EXIT
    Not gonna do it

    EDIT: Never mind, Bill pointed this out already with much less text. =]


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Friday, March 21, 2014 2:53 PM
  • Well....the first example works....and I can read it just fine when I compare it to the rest of what I wrote.  Thanks anyway guys!
    Friday, March 21, 2014 2:55 PM
  • Hi Bill, Mike,

    alright, that made me feel slightly embarassed. How did I ever get the notion "-eq" was case sensitive?

    Thanks for ridding me of that assumption :)

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Friday, March 21, 2014 3:06 PM
  • Hi Bill, Mike,

    alright, that made me feel slightly embarassed. How did I ever get the notion "-eq" was case sensitive?

    Thanks for ridding me of that assumption :)

    =]

    Certainly not my intention to embarrass you. I thought the same thing for a long, long time.


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Friday, March 21, 2014 3:11 PM