Home > Articles > Data > SQL Server

Like this article? We recommend

Parameter Checking within the Script

Now that we have established the header information, let's take a look at the next logical section of the script: parameter checking. Because I am a little paranoid (only a little, mind you), I always explicitly check to see that the user(s) of my scripts supply the parameters that I expect them to. This ensures that if the script fails, the user will at least understand why!

The general format (pseudo-code) for parameter checking is "if variable is not empty check next parameter (use goto), otherwise we either assign a value to the parameter or report to the user that the parameter is missing". For example:

1:   if not "%MyParameter%"=="" goto :MyParameterOK
2:       (echo ERROR: Missing input parameter MyParameter.)
3:       (echo ERROR: Missing input parameter MyParameter.) >> %Logfile%
4:       set RETVAL=10
5:       goto :end
6:   :LogFileOK

In the above code snippet, you can see on line 1 that we check for the existence of our parameter (MyParameter); if it is not empty, we jump to line 6, so that we can check the next parameter.

Otherwise (if the parameter is empty), we go to line 2 and send a message out to the console window, alerting the user to the fact that the parameter did not contain a value. Line 3 writes the error out to our log file, and line 4 sets a return value, so when we are looking through our logs, it is easy to identify where the script errored-out. Finally, line 5 forces us to exit the batch program because it is an error we cannot recover from.

In Listing 2, you can see the parameter-checking in action.

Listing 2—Checking for the Parameters within the Script.

:Main
    REM Check for the input values.
    if not "%LogFile%"=="" goto :LogFileOK
        (echo WARNING: Missing input parameter LogFile. Setting to default "%TEMP%\SQLInstall.log")
        set LogFile="%TEMP%\SQLInstall.log"
    :LogFileOK

    if not "%AppPath%"=="" goto :AppPathOK
        (echo ERROR: Missing input parameter AppPath.)
        (echo ERROR: Missing input parameter AppPath.) >> %Logfile%
        set RETVAL=10
        goto :end
    :AppPathOK

    if not "%SQLPath%"=="" goto :SQLPathOK
        (echo ERROR: Missing input parameter SQLPath.)
        (echo ERROR: Missing input parameter SQLPath.) >> %Logfile%
        set RETVAL=15
        goto :end
    :SQLPathOK

    if not "%SvcActName%"=="" goto :SvcActNameOK
        (echo WARNING: Missing input parameter SvcActName. Setting to default "LocalSystem")
        (echo WARNING: Missing input parameter SvcActName. Setting to default "LocalSystem") >> 
 %Logfile%
        set SvcActName=LocalSystem
    :SvcActNameOK

    REM If the service account is set to "LocalSystem" then we don't check for the existence of the other 
    REM of the domain and password parameters (not required if running under localsystem)
    if "%SvcActName%"=="LocalSystem" goto :SkipCheck
        if not "%SvcActDom%"=="" goto :SvcActDomOK
           (echo ERROR: Missing input parameter SvcActDom.)
           (echo ERROR: Missing input parameter SvcActDom.) >> %Logfile%
           set RETVAL=20
           goto :end
        :SvcActDomOK

        if not "%SvcActPwd%"=="" goto :SvcActPwdOK
           (echo ERROR: Missing input parameter SvcActPwd.)
           (echo ERROR: Missing input parameter SvcActPwd.) >> %Logfile%
           set RETVAL=25
           goto :end
        :SvcActPwdOK
    :SkipCheck

    if not "%MixedMode%"=="" goto :MixedModeOK
        (echo WARNING: Missing input parameter MixedMode. Setting to default "No")
        (echo WARNING: Missing input parameter MixedMode. Setting to default "No") >> %Logfile%
        set MixedMode=No
    :MixedModeOK

    REM Checks to see if we are using Mixed Mode or Windows only authentication. If Mixed Mode is used
    REM a password MUST be supplied for the "sa" account.
    if "%MixedMode:~0,1%"=="N" goto :NotMixedMode
        if not "%SQLsaPwd%"=="" goto :SQLsaPwdOK
           (echo ERROR: Missing input parameter SQLsaPwd.)
           (echo ERROR: Missing input parameter SQLsaPwd.) >> %Logfile%
           set RETVAL=30
           goto :end
        :SQLsaPwdOK
    :NotMixedMode

    if not "%CompanyName%"=="" goto :CompanyNameOK
        (echo ERROR: Missing input parameter CompanyName.)
        (echo ERROR: Missing input parameter CompanyName.) >> %Logfile%
        set RETVAL=35
        goto :end
    :CompanyNameOK

    if not "%ISSDir%"=="" goto :ISSDirOK
        (echo WARNING: Missing input parameter ISSDir. Setting to default "%TEMP%")
        (echo WARNING: Missing input parameter ISSDir. Setting to default "%TEMP%") >> %Logfile%
        set ISSDir=%TEMP%
    :ISSDirOK

    if not "%SQLDataPath%"=="" goto :SQLDataPathOK
        (echo WARNING: Missing input parameter SQLDataPath. Setting to default "%SQLPath%")
        (echo WARNING: Missing input parameter SQLDataPath. Setting to default "%SQLPath%") >>    
 %Logfile%
        set SQLDataPath=%SQLPath%
    :SQLDataPathOK

    if not "%InstanceName%"=="" goto :InstanceNameOK
        (echo WARNING: Missing input parameter InstanceName. Setting to default "MSSQLSERVER")
        (echo WARNING: Missing input parameter InstanceName. Setting to default "MSSQLSERVER") >>    
 %Logfile%
        set InstanceName=MSSQLSERVER
    :InstanceNameOK

    if not "%SQLCollation%"=="" goto :SQLCollationOK
        (echo WARNING: Missing input parameter SQLCollation. Setting to default
 "SQL_Latin1_General_CP1_CI_AS")
        (echo WARNING: Missing input parameter SQLCollation. Setting to default
 "SQL_Latin1_General_CP1_CI_AS") >> %Logfile%
        set SQLCollation=SQL_Latin1_General_CP1_CI_AS
    :SQLCollationOK

    if not "%TCPPort%"=="" goto :TCPPortOK
        (echo WARNING: Missing input parameter TCPPort. Setting to default "1433")
        (echo WARNING: Missing input parameter TCPPort. Setting to default "1433") >> %Logfile%
        set TCPPort=1433
    :TCPPortOK

    if NOT "%LicenseMode%"=="" goto :LicenseModeOK
        (echo WARNING: Missing input parameter LicenseMode. Setting to default "PERDEVICE")
        (echo WARNING: Missing input parameter LicenseMode. Setting to default "PERDEVICE") >>
 %Logfile%
        set LicenseMode=PERDEVICE
    :LicenseModeOK

    if NOT "%LicenseLimit%"=="" goto :LicenseLimitOK
        (echo WARNING: Missing input parameter LicenseLimit. Setting to default "1")
        (echo WARNING: Missing input parameter LicenseLimit. Setting to default "1") >> %Logfile%
        set LicenseLimit=1
    :LicenseLimitOK

    if "%InstanceName%"=="MSSQLSERVER" goto :DefaultInstance
        (echo Setting InstanceName to "MSSQLSERVER$%InstanceName%")
        (echo Setting InstanceName to "MSSQLSERVER$%InstanceName%") >> %Logfile%
        set InstanceName=MSSQLSERVER$%InstanceName%
    :DefaultInstance

Wow, what a lot of code! Yes, it does look like a lot, but all the code does is check for the existence of values for our variables; that is, it ensures that a value has been supplied for those parameters that require it.

You may have also noticed that with some of the variables, default values have been supplied. This means that if the user does not supply them (overlooked them), we give them a value instead. For example, the first check looks for the existence of the LogFile variable. If this variable does not contain a value, a default value of %Temp%\SQLInstall.log is assigned.

The next interesting part of this code is the check for the value that the SvcActName variable contains. If this contains a value of LocalSystem, we know that SQL Server will be installed to run (as a service) as LocalSystem; thus, we do not check for the existence of the SvcActDom (domain) or SvcActPwd (password) variables. However, if the SvcActName does not equal LocalSystem, the user of the script must provide the domain and password values for the variables.

NOTE

Even if SQL Server uses a local user account, the domain name must be supplied. If using a local user account, it is the machine name.

By building in this type of conditional logic, we can install SQL Server in a number of slightly different configurations, simply by passing in different values for the variables.

The last cunning trick is the instance name (look at the last IF statement in the code). This allows us to install multiple instances of SQL Server on the same machine (a feature available only in SQL Server 2000) by checking to see if the user has supplied the special MSSQLSERVER value (default instance). Using this logic, we can decide whether this installation is for the default instance or for a named instance (not the default instance). If the name is not MSSQLSERVER, we append the value MSSQLSERVER$ to the name. For instance, if we want an installation of SQL Server to be called JIMMY, the .iss file (when written out) will contain MSSQLSERVER$JIMMY in the [DlgInstanceName-0] section.

NOTE

In this script, I am forcing the user to supply a value for the sa password because my script requires that SQL Server be installed in Mixed Mode. You may not actually require a password in your installation, so you can remove this check if you want or modify the code to supply a default value if the user of the script does not supply a value. Or simply remove the capability for the installation to use SQL authentication altogether, thus forcing the server to use only Windows Authentication.

Finally, you may have also noticed the return status (RETVAL) incrementing (by 5) as we check for each variable (see within each IF statement). By using a different return value at each checkpoint, it allows us to pinpoint the problem areas within the script. For example, if the script has a return status of 55, the user has not supplied a value for the parameter CompanyName.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020