Kal*_*lly 1 powershell function powershell-3.0
我有以下PowerShell脚本
Function Publish
{
Param(
[parameter(Mandatory=$true)]
[String]
$RELEASEDIR,
[parameter(Mandatory=$true)]
[String]
$SERVICENAME,
[parameter(Mandatory=$true)]
[String]
$SERVER
)
Get-ChildItem "$RELEASEDIR\*"
$service = Get-Service -Name $SERVICENAME -Computername $SERVER -ErrorAction SilentlyContinue
$service.Status
}
Publish
Run Code Online (Sandbox Code Playgroud)
我是如何执行这个:
PS C:\Release\RPCPS> .\RPCPublish.ps1 -RELEASEDIR "C:\Location" -SERVICENAME "value" -SERVER "server"
cmdlet Publish at command pipeline position 1
Supply values for the following parameters:
RELEASEDIR:
Run Code Online (Sandbox Code Playgroud)
即使在执行时传递参数,脚本也会再次期待它.我在这做错了什么?
如果要通过调用示例中的.ps1来执行脚本,则无需使用函数.您的脚本应该如下所示:
Param(
[parameter(Mandatory=$true)]
[String]
$RELEASEDIR,
[parameter(Mandatory=$true)]
[String]
$SERVICENAME,
[parameter(Mandatory=$true)]
[String]
$SERVER
)
Get-ChildItem "$RELEASEDIR\*"
$service = Get-Service -Name $SERVICENAME -Computername $SERVER -ErrorAction SilentlyContinue
$service.Status
Run Code Online (Sandbox Code Playgroud)
参数直接传递给脚本,可以在那里使用.
另一方面,如果要建立(可重用)函数,只需从脚本中删除最后一行,该行调用不带参数的函数(这就是为什么每次都要求强制参数).
如果删除最后一行,则可以不带参数调用脚本一次.之后,您Publish在当前会话中有一个新功能,然后您可以调用它
Publish -RELEASEDIR "C:\Release\Batchfile" -SERVICENAME "AmazonSSMAgent" -SERVER "10.0.1.91"
Run Code Online (Sandbox Code Playgroud)
独立于脚本文件.