未分配函数参数

function parameter not getting assigned

调用包装停止 IIS 应用程序池的函数。为什么 Stop-WebAppPool 的 'Name' 参数会有问题?我唯一能看到的是因为它在脚本块中?

错误:

Cannot validate argument on parameter 'Name'. The argument is null. Provide a valid value for the argument, and then try running the command again.

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"

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

...

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

...