【问题标题】:unable to write powershell command output to variable无法将 powershell 命令输出写入变量
【发布时间】:2015-01-01 08:15:13
【问题描述】:

我正在尝试查找 Firefox 是否在系统中被阻止。我已经从 remy 的网站 http://code.remyservices.net/powershell-firefoxpref/wiki/Home 导入了模块。

感谢雷米的宝贵帮助。我能够在命令中获得输出。但我无法将输出分配给变量。请帮忙。下面是脚本的代码和输出...

代码:

Import-Module 'C:\firefox module\FirefoxPref.psd1'

Import-Module 'C:\firefox module\FirefoxPref.psm1'

Get-Firefoxpref -scope user -file mozilla.cfg -setting network.proxy.type

powershell 中的输出:

Setting found:
    lockPref("network.proxy.type", 2);

我想将输出分配给一个变量。

$var = powershell .\firefox block proxy.ps1

也不行..

【问题讨论】:

  • 你为什么用powershell打开另一个powershell会话?您在文件proxy.ps1 中提供的代码是吗?您是否尝试过只使用$var = proxy.ps1?抱歉,我对那个 firefox 模块一无所知。还可以尝试在 SO 中搜索“分配给变量 [powershell]”之类的内容
  • 其实我的要求是写一个powershell脚本来检查mozilla.cfg是否有一个特定的值lockpref(Network.proxy.type,2)如果是这样,它证明我firefox代理设置被阻止了.导入 firefox 模块后,我运行命令检查是否存在上述值。然后我试图将它分配给一个变量。但这并没有发生。所以我想将 .ps1 的输出分配给一个变量.. 到目前为止没有运气....

标签: variables firefox powershell scripting


【解决方案1】:

当然只是使用:

$Var = Get-Firefoxpref -scope user -file mozilla.cfg -setting network.proxy.type

我以前没有使用过这个模块,但是不存储它的输出应该没有太大的不同。

【讨论】:

  • developer.mozilla.org/en-US/docs/Archive/Mozilla/… 显然,这仅在机器全部设置为使用 CFG 文件时才有效。对我来说,该声明没有返回任何内容,因为我们这里只有 IE 政策,因为我们不支持 Mozilla/Chrome 等。
  • 你是对的。我有一个配置为禁用 Firefox 代理设置的 CFG 文件。我想阅读内容并决定是否禁用...
  • $Var = Get-Firefoxpref -scope user -file mozilla.cfg -setting network.proxy.type 直接给我输出... $Var 中没有保存任何内容...
【解决方案2】:

开发者在脚本中使用了 write-host。以下是截图:

if ($SettingFound -eq 0) {Write-Host "Setting found:"}
Write-Host "   "$_

Write-Host 替换为 Write-Output 应该可以正常工作

【讨论】:

    【解决方案3】:

    最后在修改 Firefox 模块后,我的脚本检查 mozill.cfg 是否存在,如果存在代理是否被阻止...

    $strFileName="C:\Program Files\Mozilla Firefox\mozilla.cfg"

    功能代理-检查{

    Import-Module '\\firefox module\FirefoxPref.psd1'
    
    Import-Module '\\firefox module\FirefoxPref.psm1'
    
    $Var = Get-Firefoxpref -scope user -file mozilla.cfg -setting network.proxy.type
    
    if ($var) {
    
    $str = "Compliant"
    
    } else {
    
    $str = "Non-compliant"
    
    }
    
    Return, $str
    
    }
    

    如果(测试路径 $strFileName){

    proxy-Check
    
    }else{
    
    Write-host "Firefox Not installed"
    
    }
    

    非常感谢 user4221907 :) :) :) :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 2021-10-04
      • 2018-07-16
      • 1970-01-01
      相关资源
      最近更新 更多