PowerShell - 将变量传递到ForEach循环的问题

时间:2016-03-14 22:17:10

标签: foreach copy unc

我正在尝试从Active Directory枚举服务器列表,然后将服务器名称作为复制命令的一部分插入到UNC路径中。

当我执行脚本时,我得到下面的结果。我想也许我必须转换我的变量,但我不知道将它转换成什么。

VERBOSE:执行操作"复制文件" on target" Item:C:\ davidtemp \ Logo.png目的地:\ @ {name = NCIDITSTWEB07} \ c $ \ program files ...

$webdev = Get-ADOrganizationalUnit -filter {name -like "*dev*"} | where {$_.DistinguishedName -like "*relativity*"}
$ServerList = Get-ADComputer -SearchBase $webdev | where {$_.name -like "*web*"} | select name | sort name


Foreach($server in $ServerList)
{
$scriptBlockwork = { copy C:\davidtemp\Logo.png "\\$server\c$\program files\web\images" -Force -Verbose}
Invoke-Command -ScriptBlock $scriptBlockwork -verbose
}

1 个答案:

答案 0 :(得分:1)

我联系了一位能够提供帮助的朋友。我没有正确定义变量。

我需要使用-expandProperty将结果转换为适用于管道的格式

$ ServerList = Get-ADComputer -SearchBase $ webdev |其中{$ _。name-like" web "} | select -expandProperty name

希望这有助于其他可能遇到类似问题的人。

相关问题