7

I wrote a script to copy files to the "All Users" desktop or "Public Desktop"

However we have a mixed environment. Some people are using Windows XP and other people are using 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 } } 
1

3 Answers 3

9

DESTINATION is empty. Expanding on Keith's suggestion:

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

This could avoid the error you're getting also. empty $DESTINATION.

Sign up to request clarification or add additional context in comments.

Comments

3

In your foreach loop through $computerlist you can grab the OS Caption for each computer by using WMI:

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

Ant then check the $OS

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

2 Comments

Thanks. How do you catch the exceptions? Copy-Item : Logon failure: unknown user name or bad password. At 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
In Powershell 2.0 you can just use Try/Catch
0

I had a slightly different goal...But thanks for the basics.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.