【问题标题】:PowerShell function doesn't produce outputPowerShell 函数不产生输出
【发布时间】:2017-04-20 04:55:57
【问题描述】:

对不起,如果这是一个基本问题,我是一名 PS 初学者......为什么当我将以下 Powershell 函数添加到 $Profile 时,它​​运行良好并将输出写入控制台,但在以下情况下不产生任何输出我将它作为 .ps1 运行? (当我作为脚本运行时,它似乎在运行,但没有返回任何输出或错误。

我的执行策略 = 不受限制,我以管理员身份运行。想法?

    function Get-NetStats
    { 
        $properties = 'Protocol','LocalAddress','LocalPort' 
        $properties += 'RemoteAddress','RemotePort','State','ProcessName','PID'

        netstat -ano | Select-String -Pattern '\s+(TCP|UDP)' | ForEach-Object {

            $item = $_.line.split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)

            if($item[1] -notmatch '^\[::') 
            {            
                if (($la = $item[1] -as [ipaddress]).AddressFamily -eq 'InterNetworkV6') 
                { 
                   $localAddress = $la.IPAddressToString 
                   $localPort = $item[1].split('\]:')[-1] 
                } 
                else 
                { 
                    $localAddress = $item[1].split(':')[0] 
                    $localPort = $item[1].split(':')[-1] 
                } 

                if (($ra = $item[2] -as [ipaddress]).AddressFamily -eq 'InterNetworkV6') 
                { 
                   $remoteAddress = $ra.IPAddressToString 
                   $remotePort = $item[2].split('\]:')[-1] 
                } 
                else 
                { 
                   $remoteAddress = $item[2].split(':')[0] 
                   $remotePort = $item[2].split(':')[-1] 
                } 

                New-Object PSObject -Property @{ 
                    PID = $item[-1] 
                    ProcessName = (Get-Process -Id $item[-1] -ErrorAction SilentlyContinue).Name 
                    Protocol = $item[0] 
                    LocalAddress = $localAddress 
                    LocalPort = $localPort 
                    RemoteAddress =$remoteAddress 
                    RemotePort = $remotePort 
                    State = if($item[0] -eq 'tcp') {$item[3]} else {$null} 
                } | Select-Object -Property $properties 
            } 
        } 
    }

【问题讨论】:

  • "我从不调用我的函数,所以它永远不会运行。为什么它不输出任何东西?"
  • 是的;确切地。仔细阅读 TessellatingHeckler 的评论。

标签: powershell


【解决方案1】:

TessellatingHeckler 说得对,虽然可能不如我说的那么清楚,而且可能比我说的更尖刻:

简单地说,您的 .ps1 定义函数 Get-NetStats 但它不调用该函数。如果没有调用这个函数,你将不会得到任何输出。

一旦您运行 .ps1 - 或者,如果函数定义在您的配置文件中 - 该函数将被定义,并且可以在该 PowerShell 会话中随时调用,只需键入 Get-NetStats。

【讨论】:

    【解决方案2】:

    我找到了一个帮助我解决这个问题的程序。它位于此处:

    In PowerShell, how do I define a function in a file and call it from the PowerShell commandline?

    现在,我需要弄清楚如何将我的新功能部署到远程主机,可能是通过 GPO 或其他 PowerShell 脚本...

    干杯!

    【讨论】:

      【解决方案3】:

      为了在运行.ps1 文件时查看位于.ps1 文件中的函数的输出,您需要在脚本中添加函数调用。

      例如,假设我创建了以下文件:Do-AlmostNothing.ps1

      # Do-AlmostNothing.ps1
      # define the function doNotSoMuch
      function doNotSoMuch {
         Write-Host "Look how little I've accomplished!"
      }
      # execute the function defined above
      doNotSoMuch
      

      如果不调用函数,什么都不会发生。

      执行脚本:

      PS> ./Do-AlmostNothing.ps1
      Look how little I've accomplished!
      PS>
      

      【讨论】:

        【解决方案4】:

        您确定不需要在脚本中输出返回结果吗?当您在 powershell 窗口中从 $Profile 运行函数时,结果将返回到该窗口。在脚本中并非如此。

        在脚本中尝试:

        $result = Get-NetStats
        $result
        

        【讨论】:

        • 我试图通过管道将该函数传递给 Write-Host,但无济于事,我认为这应该会产生任何输出到控制台。
        【解决方案5】:

        你可以试试这样的

        $myStaff = Get-NetStats

        Write-output "$myStaff"

        【讨论】:

          猜你喜欢
          • 2011-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-29
          • 2012-01-07
          • 2017-10-05
          • 1970-01-01
          相关资源
          最近更新 更多