如何将PowerShell cmdlet的输出传递给脚本?

如何将PowerShell cmdlet的输出传递给脚本?,powershell,Powershell,我正在尝试运行PowerShell脚本,输入是另一个PowerShell cmdlet的结果。以下是跨林Exchange 2013 PowerShell命令,通过指定-Identity参数,我可以为一个用户成功运行该命令: .\Prepare-MoveRequest.ps1 -Identity "user@domain.com" -RemoteForestDomainController "dc.remotedomain.com" $Remote -UseLocalObject -Overwri

我正在尝试运行PowerShell脚本,输入是另一个PowerShell cmdlet的结果。以下是跨林Exchange 2013 PowerShell命令,通过指定-Identity参数,我可以为一个用户成功运行该命令:

.\Prepare-MoveRequest.ps1 -Identity "user@domain.com" -RemoteForestDomainController "dc.remotedomain.com" $Remote -UseLocalObject -OverwriteLocalObject -Verbose
我想为所有邮件用户运行此命令。因此,我想运行的是:

Get-MailUser | select windowsemailaddress | .\Prepare-MoveRequest.ps1 -RemoteForestDomainController "dc.remotedomain.com" $Remote -LocalForestDomainController "dc.localdomain.com" -UseLocalObject -OverwriteLocalObject -Verbose
请注意,我删除了
-Identity
参数,因为我是从每个
Get-MailUser
WindowsEmailAddress
属性值中输入它的。但是,这会返回一个管道输入错误

我还尝试将
WindowsEmailAddress
属性值导出到CSV,然后根据以下站点读取,但我也遇到了管道问题:

将每个邮件用户的windowsemailaddress字段提供给我的Prepare-MoveRequest.ps1脚本的最佳方式是什么

编辑:除了上面的
Import Csv
选项之外,我可能刚刚通过以下
foreach
解决了这个问题。我现在正在测试它:

Import-Csv mailusers.csv | foreach { Prepare-MoveRequest.ps1 -Identity $_.windowsemailaddress -RemoteForestDomainController DC.remotedomain.com -RemoteForestCredential $Remote }

您应该声明名为
Prepare MoveRequest
的自定义函数,而不是简单地将其作为脚本。然后,点源声明函数的脚本,然后调用函数。要接受函数中的管道输入,需要声明一个或多个使用适当参数属性的参数,例如
valuefrompippeline
valuefrompiplinebypropertyname
。下面是属性的参数

例如,假设我正在开发一个定制的
Stop-Process
cmdlet。我想根据Windows进程的
ProcessID
(或
PID
)停止进程。下面是命令的外观:

function Stop-CustomProcess {
    # Specify the CmdletBinding() attribute for our
    # custom advanced function.
    [CmdletBinding()]
    # Specify the PARAM block, and declare the parameter
    # that accepts pipeline input
    param (
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [int] $Id
    )

    # You must specify the PROCESS block, because we want this
    # code to execute FOR EACH process that is piped into the
    # cmdlet. If we do not specify the PROCESS block, then the
    # END block is used by default, which only would run once.
    process {
        Write-Verbose -Message ('Stopping process with PID: {0}' -f $ID);
        # Stop the process here
    }
}

# 1. Launch three (3) instances of notepad
1..3 | % { notepad; };

# 2. Call the Stop-CustomProcess cmdlet, using pipeline input
Get-Process notepad | Stop-CustomProcess -Verbose;

# 3. Do an actual clean-up
Get-Process notepad | Stop-Process;
现在我们已经看了一个构建自定义函数的示例。。。在脚本文件中定义自定义函数后,请在“主”脚本中点源代码


编辑:我想指出,您对帖子的编辑没有正确处理PowerShell中的参数绑定。它可能会达到预期的结果,但并没有教给您在PowerShell中绑定参数的正确方法。您不必使用ForEach对象来实现所需的结果。通读我的文章,我相信你会加深对参数绑定的理解。

我的foreach循环成功了

Import-Csv mailusers.csv | foreach { Prepare-MoveRequest.ps1 -Identity $_.windowsemailaddress -RemoteForestDomainController DC.remotedomain.com -RemoteForestCredential $Remote }
可能重复的
# Import the custom function into the current session
. $PSScriptRoot\Prepare-MoveRequest.ps1
# Call the function
Get-MailUser | Prepare-MoveRequest -RemoteForestDomainController dc.remotedomain.com $Remote -LocalForestDomainController dc.localdomain.com -UseLocalObject -OverwriteLocalObject -Verbose;
# Note: Since you've defined a parameter named `-WindowsEmailAddress` that uses the `ValueFromPipelineByPropertyName` attribute, the value of each object will be bound to the parameter, as it passes through the `PROCESS` block.
Import-Csv mailusers.csv | foreach { Prepare-MoveRequest.ps1 -Identity $_.windowsemailaddress -RemoteForestDomainController DC.remotedomain.com -RemoteForestCredential $Remote }