我写了一个脚本将文件复制到“所有用户”桌面或“公共桌面”
但是我们有一个混合的环境。 有些人正在使用Windows XP,而其他人正在使用Windows 7。
$SOURCE = "I:\Path\To\Folder\*" $DESTINATION7 = "c$\Users\Public\Desktop" $DESTINATIONXP = "c$\Documents and Settings\All Users\Desktop" $computerlist = Get-Content I:\Path\To\File\computer-list.csv $results = @() $filenotthere = @() $filesremoved = @() foreach ($computer in $computerlist) { if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet)) { Write-Host "\\$computer\$DESTINATION\" Copy-Item $SOURCE "\\$computer\$DESTINATION\" -Recurse -force } else { $details = @{ Date = get-date ComputerName = $Computer Destination = $Destination } $results += New-Object PSObject -Property $details $results | export-csv -Path I:\Path\To\logs\offline.txt -NoTypeInformation -Append } }
DESTINATION是空的。 基思的建议扩大:
foreach ($computer in $computerlist) { if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet)) { $OS = Get-WmiObject -Computer $computer -Class Win32_OperatingSystem if($OS.caption -like '*Windows 7*'){ $DESTINATION = $DESTINATION7 } if($OS.caption -like '*Windows XP*'){ $DESTINATION = $DESTINATIONXP } } }
这可以避免你正在得到的错误。 empty $DESTINATION
。
在通过$ computerlist的foreach循环中,您可以使用WMI获取每台计算机的OS标题:
$OS = Get-WmiObject -Computer $computer -Class Win32_OperatingSystem
Ant然后检查$ OS
if($OS.caption -like '*Windows 7*'){ #Code here for Windows 7 } #....
我有一个稍微不同的目标…但感谢您的基本知识。
del C:\scripts\OS.csv $computerlist = Get-Content c:\scripts\computerlist.csv foreach ($computer in $computerlist) { if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet)) { Get-WMIObject Win32_OperatingSystem -ComputerName $computer | select-object CSName, Caption, CSDVersion, OSType, LastBootUpTime, ProductType| export-csv -Path C:\Scripts\OS.csv -NoTypeInformation -Append } }