Here is another one of the student projects from Decembers PowerShell class. In this case, the student needed to examine the hard drive of a computer and discover certain image files. He then needed to copy those files to another computer and hold them in a folder who’s name contained the source clients name and a date stamp. This code is still rough as we identified a lot of conflict scenarios that could pop up, but it still works.
Function Copy-ImageFile
{
param (
[switch]$ListFiles,
$Destination = "C:\Users\Jason\Documents\Roman\Roman2"
)
$Images = Get-ChildItem -Recurse | where{$_.Extension -match "gif|jpg|jpeg|png"}
If ($ListFiles-eq $True)
{
$Images
}
Else
{
# Get the computer name.
$ComputerName = gc env:ComputerName
# Create the name of the folder to copy data to.
[string]$FolderName = $ComputerName + ((Get-Date).Year).ToString() + `
((Get-Date).month).ToString() + ((Get-Date).Day).ToString()
# Create new folder in Destingation.
New-Item -type Directory -Path $Destination -Name $FolderName
# Copy Image to the destination
$Images | Copy-Item -Destination "$Destination\$FolderName" -Force
}
} # End: Function Copy-ImageFiles
Copy-ImageFile
Comments