powershell - powershell变量不会在脚本的第二部分中使用

标签 powershell

我正在学习Power Shell,并且这段代码有问题。当我解析它时,所有的东西都可以,但是不能一起使用。可能是什么问题呢?谢谢你的答案。

$hotfix = read-host "Enter hotfixID"

Start-Process firefox.exe (get-hotfix | 
Where-Object -filter {$_.hotfixID -eq $hotfix} | 
Select-Object -ExpandProperty Caption)

最佳答案

您的脚本在这里可以正常工作。请注意,我没有安装Firefox,但在iexplore上运行良好。您遇到什么问题?

另外,正如@ Colyn1337所述,您不需要使用Where-Object;您可以按以下方式简化此脚本:

$Hotfix = Read-Host "Enter Hotfix ID"

Start-Process firefox.exe
(
    Get-HotFix -Id "$Hotfix" | 
        Select-Object -ExpandProperty Caption
)

编辑:如以下注释中所述,问题是通过powershell.exe -command scriptname调用时参数不起作用。解决方案是通过ArgumentList隐式传递参数:
$Hotfix = Read-Host "Enter Hotfix ID"

Start-Process firefox.exe -ArgumentList `
(
    Get-HotFix -Id "$Hotfix" | 
        Select-Object -ExpandProperty Caption
)

关于powershell - powershell变量不会在脚本的第二部分中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18253517/

相关文章:

excel - PowerShell 可以生成包含多个工作表的纯 Excel 文件吗?

powershell - 如何以编程方式写入 Powershell 中的当前提示行

Azure Powershell 函数使用推送输出绑定(bind)附加到 Blob

powershell - 如何通过powershell添加Azure应用服务扩展?

Powershell Get-Service 命令输出服务代码而不是文本状态?

c# - 如何在Azure云服务上使用第3方DLL

powershell - invoke-expression 不会在 powershell 中抛出错误

powershell - 使用输出覆盖 Azure Function Table Store 行

windows - 获取文件 "last saved by"属性而不更改它

powershell - 在Powershell中运行远程脚本