Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
修改函数中的PowerShell对象_Powershell - Fatal编程技术网

修改函数中的PowerShell对象

修改函数中的PowerShell对象,powershell,Powershell,有没有一种方法可以将对象传递到函数中而不必命名对象本身 换句话说,我可以使用对象的名称调用函数并对该对象进行更改,而不是对每个$username重复相同的最后三行代码(有10个username文本框),如果输入对象都是引用类型,则可以在$input枚举器上迭代: function Itil { $_.Font = 'Microsoft Sans Serif, 8.25pt, style=Italic' $_.Text = 'Double-click to add username

有没有一种方法可以将对象传递到函数中而不必命名对象本身


换句话说,我可以使用对象的名称调用函数并对该对象进行更改,而不是对每个
$username
重复相同的最后三行代码(有10个username文本框),如果输入对象都是引用类型,则可以在
$input
枚举器上迭代:

function Itil
{
    $_.Font = 'Microsoft Sans Serif, 8.25pt, style=Italic'
    $_.Text = 'Double-click to add username'
    $_.ForeColor = 'ScrollBar'
}

$Username10.Font = 'Microsoft Sans Serif, 8.25pt, style=Italic'
$Username10.Text = 'Double-click to add username'
$Username10.ForeColor = 'ScrollBar'
并使用类似于:

filter Set-UsernameFieldStyle {
    $input |ForEach-Object {
        $_.Font = 'Microsoft Sans Serif, 8.25pt, style=Italic'
        $_.Text = 'Double-click to add username'
        $_.ForeColor = 'ScrollBar'
    }
}

Mathias提供了一种使用PowerShell过滤器的好方法。但是,如果要使用简单的函数,这应该可以:

# Input all the elements that need to be styled this way via the pipeline:
$Username1,$Username4,$Username10 |Set-UsernameFieldStyle

是否可以执行$username$n(其中$n是整数)?ForEach($n在1..10中){UseSystemPasswordChar$Password$n}
function Set-BasicProperties
{
    [CmdletBinding()]
    Param
    (
        $UiObject
    )

    $UiObject.Font = 'Microsoft Sans Serif, 8.25pt, style=Italic'
    $UiObject.Text = 'Double-click to add username'
    $UiObject.ForeColor = 'ScrollBar'
}


Set-BasicProperties $Username10