我正在写一个包含许多脚本的模块化脚本。 因此主脚本将调用另一个下标,而这些下标有时也会调用其他脚本。其他脚本的调用将在同一会话中完成。我的问题是使用ValidateScript进行参数验证。
因此,主脚本将调用另一个名为NSX-Gather-AT-Main.ps1的子脚本,并且此NSX主脚本具有16个参数,我需要以某种方式对其进行验证,如果验证失败,则抛出异常。
在下面的脚本(这是从主脚本调用的NSX-Gather-AT-Main-ps.1)中,虽然没有进行所有验证,但是我只包括对无效的文件路径的验证使它起作用,然后我将添加其他验证。
每当我从主脚本调用脚本时,都会出现以下错误:
E:\CGS-NSX-Gather-AT\NSX-Gather-AT-Main.ps1 : Cannot validate argument on parameter 'NsxOutputFilePath'. The "If ( -Not ($_ | Test-Path)) {Throw "Provided File path for NSX Outputs ($_) is not valid."}" validation script for the argument
with value "E:\Testing-Scripts\Test-01\" did not return a result of True. Determine why the validation script failed, and then try the command again.
At line:1 char:554
+ ... word NotUsed -NsxOutputFilePath "E:\Testing-Scripts\Test-01\" -NsxScr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [NSX-Gather-AT-Main.ps1], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,NSX-Gather-AT-Main.ps1
我在主脚本中使用的Call命令如下:
. E:\CGS-NSX-Gather-AT\CGS-NSX-Gather-AT-Main.ps1 -NsxManagerFqdn CGSM-NSX.nfv.gss -NsxManagerPassword VMware1! -vCenterFqdn CGSM-VCSA.nfv.gss -vCenterUser administrator@vsphere.local -vCenterPassword VMware1! -NsxControllerPassword VMware1!VMware1! -HostingvCenterFQDN CGSM-VCSA.nfv.gss -HostingvCenterUser administrator@vsphere.local -HostingvCenterPassword VMware1! -PrimaryNsxManagerFqdn "NotUsed" -PrimaryNsxManagerPassword NotUsed -PrimaryNsxvCenterFqdn NotUsed -PrimaryNsxvCenterUser NotUsed -PrimaryNsxvCenterPassword NotUsed -NsxOutputFilePath E:\Testing-Scripts\Test-01\ -NsxScriptFilePath "E:\Testing-Scripts\Test-02"
NSX-Gather-AT-Main.ps1脚本如下:
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
# *********************************** #
# NSX-AT-Gather-Main #
# CGS NSX Gather Assessment Tool #
# This tool will automate the assessment #
# process for any onboarding #
# VMware CGS customers. #
# *********************************** #
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
################################################################################################################
#PS: The code will be divided into section using the region option to better maintain/Organize of the tool code.
################################################################################################################
#===================================================================#
########### Start of Parameters of this Script ###########
#===================================================================#
#region 1st Section - Section-Name: ScriptParameters
#===================================================
#======================================#
### Start of 1st Section ###
#======================================#
#Section Description: This Section will hold the Parameters of this Script.
# PS: Parameters of this script will not be validated and the full validation will be done at a later stage of this Script through calling the NsxValidateInfo Subscript.
#----------------------------------------------------------------------------
<#
.SYNOPSIS
NSX Manager Assessment Tool Gather Main (CGS-NSX-Gather-AT-Main) Script.
.DESCRIPTION
CGS-NSX-Gather-AT-Main is the main script to be called by the parent script to perform the assessment of the NSX Environment.
.PARAMETER
.PARAMETER NsxManagerFqdn
Mandatory. FQDN of the NSX Manager to be assessed. Example CGSM-NSX.nfv.gss
.PARAMETER NsxManagerPassword
Mandatory. Admin Password of the NSX Manager to be assessed. Example VMware1!
.PARAMETER vCenterFqdn
Mandatory. FQDN of the vCenter server registered with the NSX Manager. Example CGSM-VCSA.nfv.gss
.PARAMETER vCenterUser
Mandatory. SSO Admin User of the vCenter server registered with the NSX Manager. Example administrator@vsphere.local
.PARAMETER vCenterPassword
Mandatory. SSO Admin Password of the vCenter server registered with the NSX Manager. Example VMware1!
.PARAMETER NsxControllerPassword
Mandatory. Admin Password of the NSX Controllers deployed. PS: This filed could also have a value of NotUsed. Example VMware1! / NotUsed
.PARAMETER HostingvCenterFQDN
Mandatory. FQDN of the vCenter Server Hosting the NSX manager to be assessed. Example CGSM-VCSA.nfv.gss
.PARAMETER HostingvCenterUser
Mandatory. SSO Admin User of the vCenter server Hosting the NSX manager to be assessed. Example administrator@vsphere.local
.PARAMETER HostingvCenterPassword
Mandatory. SSO Admin Password of the vCenter server Hosting the NSX manager to be assessed. Example VMware1!
.PARAMETER PrimaryNsxManagerFqdn
Mandatory. FQDN of the Primary NSX Manager if the NSX Manager to be assessed is a Secondary. PS: This filed could also have a value of NotUsed. Example CGSR-NSX.nfv.gss / NotUsed
.PARAMETER PrimaryNsxManagerPassword
Mandatory. Admin Password of the Primary NSX Manager if the NSX Manager to be assessed is a Secondary. PS: This filed could also have a value of NotUsed. Example VMware1! / NotUsed
.PARAMETER PrimaryNsxvCenterFqdn
Mandatory. FQDN of the vCenter server registered to the Primary NSX Manager if the NSX Manager to be assessed is a Secondary. PS: This filed could also have a value of NotUsed. Example CGSR-VCSA.nfv.gss / NotUsed
.PARAMETER PrimaryNsxvCenterUser
Mandatory. SSO Admin User of the vCenter server registered to the Primary NSX Manager if the NSX Manager to be assessed is a Secondary. PS: This filed could also have a value of NotUsed. Example administrator@vsphere.local / NotUsed
.PARAMETER PrimaryNsxvCenterPassword
Mandatory. SSO Admin Password of the vCenter server registered to the Primary NSX Manager if the NSX Manager to be assessed is a Secondary. PS: This filed could also have a value of NotUsed. Example VMware1! / NotUsed
.PARAMETER NsxOutputFilePath
Mandatory. File path for the script to dump all gathered data. Example C:\Some-File-Name.
.PARAMETER NsxScriptFilePath
Mandatory. File path for the script to dump all gathered data. Example C:\Some-File-Name.
.INPUTS
Parameters above
.OUTPUTS
Dumping all gathered data to the NsxOutputFilePath.
.NOTES
Version: 1.0
Author: Taher Shaker
Creation Date: 01/11/2018
.EXAMPLE
.$CGS-NSX-AT-Main.ps1 -NsxManagerFqdn CGSM-NSX.nfv.gss -NsxManagerPassword VMware1! -vCenterFqdn CGSM-VCSA.nfv.gss -vCenterUser administrator@vsphere.local -vCenterPassword VMware1! -NsxControllerPassword VMware1! -HostingvCenterFQDN CGSM-VCSA.nfv.gss -HostingvCenterUser administrator@vsphere.local -HostingvCenterPassword VMware1! -PrimaryNsxManagerFqdn CGSR-NSX.nfv.gss -PrimaryNsxManagerPassword VMware1! -PrimaryNsxvCenterFqdn CGSR-VCSA.nfv.gss -PrimaryNsxvCenterUser administrator@vsphere.local -PrimaryNsxvCenterPassword VMware1! -NsxOutputFilePath "C:\Test-Script\" -NsxScriptFilePath "C:\Test-Script\Nsx-Subscripts"
.$CGS-NSX-AT-Main.ps1 -NsxManagerFqdn CGSM-NSX.nfv.gss -NsxManagerPassword VMware1! -vCenterFqdn CGSM-VCSA.nfv.gss -vCenterUser administrator@vsphere.local -vCenterPassword VMware1! -NsxControllerPassword NotUsed -HostingvCenterFQDN CGSM-VCSA.nfv.gss -HostingvCenterUser administrator@vsphere.local -HostingvCenterPassword VMware1! -PrimaryNsxManagerFqdn NotUsed -PrimaryNsxManagerPassword NotUsed -PrimaryNsxvCenterFqdn NotUsed -PrimaryNsxvCenterUser NotUsed -PrimaryNsxvCenterPassword NotUsed -NsxOutputFilePath "C:\Test-Script\" -NsxScriptFilePath "C:\Test-Script\Nsx-Subscripts"
#>
#-------------------------------------------------------------------
Param(
# 1st Parameter will be the NSX Manager FQDN for the NSX Manager to be assessed.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$NsxManagerFqdn,
#-----------------------------------
# 2nd Parameter will be the Admin Password for the NSX Manager to be assessed.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$NsxManagerPassword,
#-----------------------------------
# 3rd Parameter will be the vCenter Server FQDN for the vCenter registered with the NSX Manager to be assessed.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$vCenterFqdn,
#-----------------------------------
# 4th Parameter will be the Username for the vCenter Server registered with the NSX Manager to be assessed.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$vCenterUser,
#-----------------------------------
# 5th Parameter will be the Password for the vCenter Server registered with the NSX Manager to be assessed.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$vCenterPassword,
#-----------------------------------
# 6th Parameter will be the Password of the NSX Controllers. This Parameter could be set to NotUsed if NSX Controllers dose not exists.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$NsxControllerPassword,
#-----------------------------------
# 7th Parameter will be the vCenter Server FQDN for the vCenter Hosting the NSX Manager to be assessed.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$HostingvCenterFQDN,
#-----------------------------------
# 8th Parameter will be the Username for the vCenter Server Hosting the NSX Manager to be assessed.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$HostingvCenterUser,
#-----------------------------------
# 9th Parameter will be the Password for the vCenter Server Hosting the NSX Manager to be assessed.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$HostingvCenterPassword,
#-----------------------------------
# 10th Parameter will be the FQDN of the Primary NSX Manager. This Parameter could be set to NotUsed if the NSX Manager to be assessed is not a Secondary NSX manager.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$PrimaryNsxManagerFqdn,
#-----------------------------------
# 11th Parameter will be the Admin Password of the Primary NSX Manager. This Parameter could be set to NotUsed if the NSX Manager to be assessed is not a Secondary NSX manager.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$PrimaryNsxManagerPassword,
#-----------------------------------
# 12th Parameter will be the FQDN of the vCenter Server registered to the Primary NSX Manager. This Parameter could be set to NotUsed if the NSX Manager to be assessed is not a Secondary NSX manager.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$PrimaryNsxvCenterFqdn,
#-----------------------------------
# 13th Parameter will be the SSO Admin User of the vCenter Server registered to the Primary NSX Manager. This Parameter could be set to NotUsed if the NSX Manager to be assessed is not a Secondary NSX manager.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$PrimaryNsxvCenterUser,
#-----------------------------------
# 14th Parameter will be the SSO Admin Password of the vCenter Server registered to the Primary NSX Manager. This Parameter could be set to NotUsed if the NSX Manager to be assessed is not a Secondary NSX manager.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$PrimaryNsxvCenterPassword,
#-----------------------------------
# 15th Parameter will be the Path to the file where the outputs of this script will be dumped to.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[ValidateScript({If ( -Not ($_ | Test-Path)) {Throw "Provided File path for NSX Outputs ($_) is not valid."}})]
[System.IO.FileInfo]$NsxOutputFilePath,
#-----------------------------------
# 16th Parameter will be the Path to the file where the NSX Subscripts and modules located.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[ValidateScript({
If ( -Not ($_ | Test-Path)) {
Throw "Provided File path for NSX Script ($_) is not valid."
}
})]
[System.IO.FileInfo]$NsxScriptFilePath
)
#-------------------------------------------------------------------
#endregion - 1st Section - END - Section-Name: ScriptParameters
#====================================================================
############## End of Parameters of this Script #############
#====================================================================
#----------------------------------------------
#=============================================#
###### Start of The Script ######
#=============================================#
答案 0 :(得分:0)
感谢@Mathias R. Jessen,您需要添加其他{$ True}。据我了解,在验证脚本中使用IF条件时,您需要确保在某一点上该值为true,因此需要添加IF和Else以确保验证将转到$ Flase或$ True。
因此,要更改上述脚本中的最后一个参数,只需添加其他{$ True}即可,如下所示:
# 15th Parameter will be the Path to the file where the outputs of this script will be dumped to.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[ValidateScript({If ( -Not ($_ | Test-Path)) {Throw "Provided File path for NSX Outputs ($_) is not valid."} Else {$True}})]
[String]$NsxOutputFilePath,
#-----------------------------------
# 16th Parameter will be the Path to the file where the NSX Subscripts and modules located.
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[ValidateScript({If ( -Not ($_ | Test-Path)) {Throw "Provided File path for NSX Script ($_) is not valid."} Else {$True}})]
[String]$NsxScriptFilePath