【发布时间】:2020-03-16 16:11:15
【问题描述】:
我有一个简单的脚本:
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
主要任务是将这些参数接收到变量中,并将这些变量放入不同的函数中。
【问题讨论】:
标签: powershell arguments parameter-passing command-line-arguments keyword