2014-02-28 72 views
2

我写了一个脚本,将文件复制到“所有用户”桌面或“公共桌面”PowerShell的确定远程计算机OS

但是我们有一个混合环境。有些人使用的是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 
    }  
} 
+0

[I如何确定一个远程计算机的OS?](http://superuser.com/q/323238/241386) –

回答

3

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

2

在你的foreach循环到$ COMPUTERLIST您可以抓取OS标题为每台计算机使用WMI

$OS = Get-WmiObject -Computer $computer -Class Win32_OperatingSystem 

蚂蚁则检查$ OS

if($OS.caption -like '*Windows 7*'){ 
    #Code here for Windows 7 
} 
#.... 
+0

感谢。你如何看待例外?复制项目:登录失败:未知的用户名或错误的密码。 在I:\ Path \ To \ Code \ powershell \ copy.ps1:194 char:22 + Copy-Item <<<< $ SOURCE“\\ $ computer \ $ DESTINATIONXP \”-Recurse -fo rce + CategoryInfo:NotSpecified:(:) [Copy-Item],IOException + FullyQualifiedErrorId:System.IO.IOException,Microsoft.PowerShell.Comman ds.CopyItemCommand –

+2

在Powershell 2.0中,您可以使用[Try/Catch](https: //blogs.technet.com/b/heyscriptingguy/archive/2010/03/11/hey-scripting-guy-march-11-2010.aspx) –

0

我有一个稍微不同的目标...但感谢您的基本知识。

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 
    } 
}