Powershell:在param语句中以数组形式传入多行txt文件值

Powershell:在param语句中以数组形式传入多行txt文件值,powershell,Powershell,我有一个包含服务器列表的文本文件,每行一个,如: SERVER1 SERVER2 SERVER3 当我对文件执行Get Content操作时,我确实看到了如下输出: SERVER1 SERVER2 SERVER3 现在我正在编写一个函数,我希望将多个服务器作为一个数组,并在函数中迭代数组。该函数当前如下所示: function Get-LocalAdministrators { param( [Parameter(Mandatory=$True,ValueFromP

我有一个包含服务器列表的文本文件,每行一个,如:

SERVER1
SERVER2
SERVER3
当我对文件执行
Get Content
操作时,我确实看到了如下输出:

SERVER1
SERVER2
SERVER3
现在我正在编写一个函数,我希望将多个服务器作为一个数组,并在函数中迭代数组。该函数当前如下所示:

function Get-LocalAdministrators 
{
    param(
        [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string[]]$computers
    )

    foreach ($computername in $computers)
    {
        $ADMINS = get-wmiobject -computername $computername -query "select * from win32_groupuser where GroupComponent=""Win32_Group.Domain='$computername',Name='administrators'""" | % {$_.partcomponent}

        foreach ($ADMIN in $ADMINS) 
        {
            $admin = $admin.replace("\\$computername\root\cimv2:Win32_UserAccount.Domain=","") # trims the results for a user
            $admin = $admin.replace("\\$computername\root\cimv2:Win32_Group.Domain=","") # trims the results for a group
            $admin = $admin.replace('",Name="',"\")
            $admin = $admin.REPLACE("""","") # strips the last "

            $objOutput = New-Object PSObject -Property @{
                Machinename = $($computername)
                Fullname = ($admin)
                #DomainName  =$admin.split("\")[0]
                #UserName = $admin.split("\")[1]
            }

        $objReport+=@($objOutput)
        }

        return $objReport
    }
}
然后我计划调用函数,如下所示:

Get-Content “C:\temp\computers.txt” | Get-LocalAdministrators
但是,当我运行该函数时,我可以看到$computers的值是
{SERVER3}
(即,仅文件中的最后一行)。我试图通过Google找到这个问题的答案,尽管有很多数组引用/示例,但我找不到一个可以从文件中读取值的组合,放入
param
语句中的数组中。请原谅我的无知,并提供线索,我需要。。。谢谢


更新:链接到PowerGUI脚本编辑器中运行的脚本的屏幕截图,显示运行期间$computers的值:

当您通过管道将对象传递到函数中时,一次只将一个对象传递到函数中,而不是整个数组。因此,您不需要使用
foreach
循环,也不需要在函数中设置
$computers
数组

此外,当您具有管道功能时,应该使用
开始
过程
结束
关键字。每个表示一个脚本块-
begin
是一个执行一次的脚本块(当管道“设置”时),
process
是要为通过管道传递的每个对象执行的脚本块,
end
begin
类似,只是它在最后一项通过后运行

因此,最低限度,您的功能应该是:

function Get-LocalAdministrators
{
    param(
    [Parameter(Mandatory=$True,Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
    [string]$computer
    )
process{
$ADMINS = get-wmiobject -computername $computername -query "select * from win32_groupuser where GroupComponent=""Win32_Group.Domain='$computername',Name='administrators'""" | % {$_.partcomponent}
# Do other stuff here
}
}
说明(这是在
获取有关函数的帮助中-还有更多内容):

将对象管道化到函数
任何函数都可以从管道中获取输入。你可以控制 函数使用Begin、Process和End处理来自管道的输入 关键词。以下示例语法显示了三个关键字:

      function <name> { 
          begin {<statement list>}
          process {<statement list>}
          end {<statement list>}
      }

  The Begin statement list runs one time only, at the beginning of 
  the function.  

  The Process statement list runs one time for each object in the pipeline.
  While the Process block is running, each pipeline object is assigned to 
  the $_ automatic variable, one pipeline object at a time. 

  After the function receives all the objects in the pipeline, the End 
  statement list runs one time. If no Begin, Process, or End keywords are 
  used, all the statements are treated like an End statement list.

当通过管道将对象传递到函数中时,一次只将一个对象传递到函数中,而不是整个数组。因此,您不需要使用
foreach
循环,也不需要在函数中设置
$computers
数组

此外,当您具有管道功能时,应该使用
开始
过程
结束
关键字。每个表示一个脚本块-
begin
是一个执行一次的脚本块(当管道“设置”时),
process
是要为通过管道传递的每个对象执行的脚本块,
end
begin
类似,只是它在最后一项通过后运行

因此,最低限度,您的功能应该是:

function Get-LocalAdministrators
{
    param(
    [Parameter(Mandatory=$True,Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
    [string]$computer
    )
process{
$ADMINS = get-wmiobject -computername $computername -query "select * from win32_groupuser where GroupComponent=""Win32_Group.Domain='$computername',Name='administrators'""" | % {$_.partcomponent}
# Do other stuff here
}
}
说明(这是在
获取有关函数的帮助中-还有更多内容):

将对象管道化到函数
任何函数都可以从管道中获取输入。你可以控制 函数使用Begin、Process和End处理来自管道的输入 关键词。以下示例语法显示了三个关键字:

      function <name> { 
          begin {<statement list>}
          process {<statement list>}
          end {<statement list>}
      }

  The Begin statement list runs one time only, at the beginning of 
  the function.  

  The Process statement list runs one time for each object in the pipeline.
  While the Process block is running, each pipeline object is assigned to 
  the $_ automatic variable, one pipeline object at a time. 

  After the function receives all the objects in the pipeline, the End 
  statement list runs one time. If no Begin, Process, or End keywords are 
  used, all the statements are treated like an End statement list.

感谢您提供的信息,但这仍然无法解决以下问题:
$computers
获取内容的管道中只有一个值(最后一行)。。。该函数确实运行,但只为文件中最后一个命名的服务器提供输出。。。我这样假设是因为$computers中只有一个值。如何从文件中传入多个值(行),以便
$computers
中包含多个值?Get Content将所有行读取到一个数组中,其中每个元素都是一行$计算机的值应该与文件中的行数相同。我的猜测是,您正在使用一个输出文件,并且每次都会覆盖它,因此最终您只能得到最终迭代的结果。如果你发布整个函数,这会有所帮助。你还没有展示你是如何输出的
$computers
中包含所有的值(我自己运行了代码),如果将
$admins
行替换为
$computer
,您将看到所有3个名称的输出。这让我得出结论,在输出中有一些东西是您没有向我们展示的,这导致了您的问题-
(…)
section.edited原创帖子至:1)添加完整功能代码2)添加链接至屏幕截图,显示在PowerGUI脚本编辑器中运行的脚本,在左下角显示
$computers
的值与
获取内容的输出pane@WillDennis请,请重新阅读我的文章全文。您需要删除
foreach
,并将参数设置为单个
字符串,而不是数组。您的OP也有缺陷,因为您在
foreach
循环中调用
return
——因此它只运行一次迭代!感谢您提供的信息,但这仍然无法解决以下问题:
$computers
获取内容的管道中只有一个值(最后一行)。。。该函数确实运行,但只为文件中最后一个命名的服务器提供输出。。。我这样假设是因为$computers中只有一个值。如何从文件中传入多个值(行),以便
$computers
中包含多个值?Get Content将所有行读取到一个数组中,其中每个元素都是一行$计算机的值应该与文件中的行数相同。我的猜测是,您正在使用一个输出文件,并且每次都会覆盖它,因此最终您只能得到最终迭代的结果。如果你发布整个函数,这会有所帮助。你还没有展示你是如何输出的
$computers
中包含所有值(我自己运行了代码),如果替换
$admins
lin