自定义函数,来自管道的值



这是一个在远程机器上更改本地用户密码的函数。我想让它从管道中获得价值。这项工作:

$x= @()
$x += Set-UserPassword -ComputerName smz0017d -User localadmin -NewPassword "1"
$x += Set-UserPassword -ComputerName smz0027d -User localadmin -NewPassword "2"
$x | Out-GridView

但有了管道的价值,就没有了。有什么建议吗?

$x = @()
$x += [pscustomobject]@{
ComputerName   = 'smzmi0027d'
User           = 'localadmin'
NewPassword    = 'djkufdjkuf1234'
}
$x += [pscustomobject]@{
ComputerName   = 'smzmi0027d'
User           = 'localadmin'
NewPassword    = '1'
}

foreach ($y in $x)
{
$y | Set-UserPassword

}

从管道接受值的函数。它调用远程机器上的命令并构建自定义对象,结果是:

function Set-UserPasswordLocaly
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true,
Position = 0)]
[string]$User,
[Parameter(Mandatory = $true,
Position = 1)]
[string]$NewPassword
)

#TODO: Place script here
try
{
Set-LocalUser -Name $User -Password (ConvertTo-SecureString $NewPassword -AsPlainText -Force) -ErrorAction Stop
$pwdSetResult = "$user password has been changed"
$isSuccess = $true
}
catch
{
$pwdSetResult = ($_.Exception).Message
$isSuccess = $false
}
return [PSCustomObject]@{
'ComputerName'     = $env:COMPUTERNAME
'isSuccess'        = $isSuccess
'Message'          = $pwdSetResult

}
}
function Set-UserPasswordRemotely
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true,
Position = 0)]
[string]$ComputerName,
[Parameter(Mandatory = $true,
Position = 1)]
[string]$User,
[Parameter(Mandatory = $true,
Position = 2)]
[string]$NewPassword
)

#TODO: Place script here
$param = @{
ComputerName     = $ComputerName
ScriptBlock      = ${function:Set-UserPasswordLocaly}
ArgumentList     = $User, $NewPassword
ErrorAction      = 'stop'
}
try
{
$invoke = Invoke-Command @param
$invokeResult = $invoke.Message
$isSuccess = $invoke.isSuccess

}
catch
{
$invokeResult = ($_.Exception).Message
$isSuccess = $false
}

return [PSCustomObject]@{
'ComputerName'      = $ComputerName
'isSuccess'         = $isSuccess
'Message'           = $invokeResult

}
}
function Set-UserPassword
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true,
Position = 0)]
[string]$ComputerName,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true,
Position = 1)]
[string]$User,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true,
Position = 2)]
[string]$NewPassword,
[Parameter(Position = 3,ValueFromPipelineByPropertyName = $true)]
[string]$PasswordVersion
)

#TODO: Place script here
PROCESS
{
if ($env:COMPUTERNAME -eq $ComputerName)
{
Set-UserPasswordLocaly $User $NewPassword
}
else
{
Set-UserPasswordRemotely $ComputerName $User $NewPassword
}
}
}

Set-UserPassword中的ComputerName参数未配置为接受管道输入。改变它就会奏效:

function Set-UserPassword
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true,
Position = 0)]
[string]$ComputerName,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true,
Position = 1)]
[string]$User,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true,
Position = 2)]
[string]$NewPassword,
[Parameter(Position = 3,ValueFromPipelineByPropertyName = $true)]
[string]$PasswordVersion
)
# I'm using Write-Host to demonstrate binding behavior, replace with your actual code
process { Write-Host $PSBoundParameters }
}
PS ~> [pscustomobject]@{
>>   ComputerName = 'smzmi0027d'
>>   User         = 'localadmin'
>>   NewPassword  = '1'
>> } |Set-UserPassword
>>
[NewPassword, 1] [User, localadmin] [ComputerName, smzmi0027d]

最新更新