【问题标题】:function parameter not getting assigned [duplicate]未分配函数参数[重复]
【发布时间】:2019-06-15 10:00:41
【问题描述】:

调用封装停止 IIS 应用程序池的函数。为什么 Stop-WebAppPool 的“名称”参数会有问题?我唯一能看到的是因为它在一个脚本块内?

错误:

无法验证参数“名称”上的参数。参数为空。 为参数提供一个有效值,然后尝试运行 再次命令。

function StopAppPool() {
    param( 
        [string] $siteName = "",
        [string] $serverName = ""
    );


    $session = New-PSSession -ComputerName $serverName
    Invoke-Command -ComputerName $serverName -ScriptBlock { Stop-WebAppPool -Name $siteName }   

}

# -- entry here
StopAppPool -siteName "my.test-server.web" -serverName "DEV-MYTEST1"

【问题讨论】:

    标签: powershell powershell-remoting


    【解决方案1】:

    Name 为空,因为您不能直接在脚本块内引用变量。使用Invoke-Command,您必须将$siteName 作为参数传入脚本块,并在脚本块内将其作为参数接收。像这样的:

    ...
    
    Invoke-Command -ComputerName $serverName -ArgumentList $siteName -ScriptBlock {
        Param($siteName)
        Stop-WebAppPool -Name $siteName
        }   
    
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      相关资源
      最近更新 更多