Chapter1: Write a basic powershell script

The following items will be introduced in the following section:

  • 1.Parameters

1.Parameters

  • powershell用param來定義參數

    • 例如

        param (
            [string]$price = 100, 
            [string]$ComputerName = $env:computername,    
            [string]$username = $(throw "-username is required."),
            [string]$password = $( Read-Host -asSecureString "Input password" )
            [switch]$SaveData = $false
        )
        write-output "First argument is $price"
        write-output "Second argument is $ComputerName"
        write-output "The True/False switch argument is $SaveData"
    • 呼叫方法可為:

       .\demo.ps1 -ComputerName "\\server64" -SaveData
      • or setting -SaveData to $FALSE:

         .\demo.ps1 -ComputerName "\\server64" -SaveData:$false
  • Parameter Attributes

    • 例如

        [Parameter(Mandatory = $true)] 
      • Mandatory

        • 參數是否必備: $true-設為必備/ $false-設為option

Last updated

Was this helpful?