需要在PowerShell中编写if语句的帮助吗

需要在PowerShell中编写if语句的帮助吗,powershell,Powershell,因此,我有一个脚本,它根据位置在我们的dr服务器上创建快照镜像。下面只是脚本的一小部分。我需要写一个if语句,这样if location='uk'就不能运行下面的函数,否则如果location='us'就创建快照镜像 function Create-SnapMirror { [CmdletBinding(PositionalBinding=$false, HelpUri='http://www.microsoft.com/',

因此,我有一个脚本,它根据位置在我们的dr服务器上创建快照镜像。下面只是脚本的一小部分。我需要写一个if语句,这样if location='uk'就不能运行下面的函数,否则如果location='us'就创建快照镜像

function Create-SnapMirror {
    [CmdletBinding(PositionalBinding=$false,
                   HelpUri='http://www.microsoft.com/',
                   ConfirmImpact='Medium')]
    [OutputType([Boolean])]
    Param(
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   ValueFromRemainingArguments=$false,
                   Position=0)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [String]$SourcePath,

        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   ValueFromRemainingArguments=$false,
                   Position=1)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [String]$DestinationPath,

        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   ValueFromRemainingArguments=$false,
                   Position=2)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [String]$LogName
    )

    $success = $null
    $error.Clear()
}

假设该逻辑是函数外部所必需的,则只需将函数调用封装在if语句中即可实现,如下所示:

if($Location -eq 'us') { Create-SnapMirror -SourcePath $MySourcePath -DestinationPath $MyDestinationPath -LogName $MyLogName }
但是,如果要检查函数中的位置,首先需要从输入参数或其他方法接收位置。假设您在名为$location的变量中有位置,则只需在任何其他操作之前在函数中添加以下内容:

if($Location -ne 'us') { return }

这将退出该功能;您可以添加其他操作,例如在括号内进行日志记录。

位置从何而来?这是一个由运行脚本的用户输入的参数,目前只有两个位置存在于美国或英国