如何在 powershell 脚本中使用关键字参数?
How to use keywords parameters in powershell scripts?
我有一个简单的脚本:
function getops {
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()]
[string]$server,
[string]$name,
[string]$user,
[string]$password,
[string]$url,
[string]$acl
)
echo $server
echo $name
echo $user
echo $password
echo $url
echo $acl
}
getops
但是当我试图用参数调用这个脚本时。
.\Untitled2.ps1 -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000
我看到空结果。
当我在脚本中添加函数时需要参数
function getops {
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()]
[string]$server,
[string]$name,
[string]$user,
[string]$password,
[string]$url,
[string]$acl
)
echo $server
echo $name
echo $user
echo $password
echo $url
echo $acl
}
getops -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000
我看到了我需要的结果:
my\sqlexpress
my
my_user
my_password
192.168.0.1
192.168.0.1:5000
问题,如何通过使用关键字参数调用脚本在 powershell 中接收相同的结果,如下所示:
.\Untitled2.ps1 -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000
主要任务是将这些参数接收到变量中,并将这些变量放到不同的函数中。
在函数外定义另一个参数块,以便将参数传递给您的脚本。在您的情况下,您只需调用不带参数的函数。
# Untitled2.ps1
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()]
[string]$server,
[string]$name,
[string]$user,
[string]$password,
[string]$url,
[string]$acl
)
function getops {
param
(
[ValidateNotNullOrEmpty()]
[string]$server,
[string]$name,
[string]$user,
[string]$password,
[string]$url,
[string]$acl
)
echo $server
echo $name
echo $user
echo $password
echo $url
echo $acl
}
getops -server $server -name $name -user $user -password $password -url $url -acl $acl
然后你可以通过
调用你的脚本
.\Untitled2.ps1 -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000
您可以像在函数中一样在脚本中使用参数
使用参数块编写脚本
param
(
[ValidateNotNullOrEmpty()]
[string]$server,
[string]$name,
[string]$user,
[string]$password,
[string]$url,
[string]$acl
)
echo $server
echo $name
echo $user
echo $password
echo $url
echo $acl
然后 运行 脚本
.\Untitled2.ps1 -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000
我有一个简单的脚本:
function getops {
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()]
[string]$server,
[string]$name,
[string]$user,
[string]$password,
[string]$url,
[string]$acl
)
echo $server
echo $name
echo $user
echo $password
echo $url
echo $acl
}
getops
但是当我试图用参数调用这个脚本时。
.\Untitled2.ps1 -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000
我看到空结果。
当我在脚本中添加函数时需要参数
function getops {
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()]
[string]$server,
[string]$name,
[string]$user,
[string]$password,
[string]$url,
[string]$acl
)
echo $server
echo $name
echo $user
echo $password
echo $url
echo $acl
}
getops -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000
我看到了我需要的结果:
my\sqlexpress
my
my_user
my_password
192.168.0.1
192.168.0.1:5000
问题,如何通过使用关键字参数调用脚本在 powershell 中接收相同的结果,如下所示:
.\Untitled2.ps1 -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000
主要任务是将这些参数接收到变量中,并将这些变量放到不同的函数中。
在函数外定义另一个参数块,以便将参数传递给您的脚本。在您的情况下,您只需调用不带参数的函数。
# Untitled2.ps1
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()]
[string]$server,
[string]$name,
[string]$user,
[string]$password,
[string]$url,
[string]$acl
)
function getops {
param
(
[ValidateNotNullOrEmpty()]
[string]$server,
[string]$name,
[string]$user,
[string]$password,
[string]$url,
[string]$acl
)
echo $server
echo $name
echo $user
echo $password
echo $url
echo $acl
}
getops -server $server -name $name -user $user -password $password -url $url -acl $acl
然后你可以通过
调用你的脚本.\Untitled2.ps1 -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000
您可以像在函数中一样在脚本中使用参数
使用参数块编写脚本
param
(
[ValidateNotNullOrEmpty()]
[string]$server,
[string]$name,
[string]$user,
[string]$password,
[string]$url,
[string]$acl
)
echo $server
echo $name
echo $user
echo $password
echo $url
echo $acl
然后 运行 脚本
.\Untitled2.ps1 -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000