Copy Folder To A Range Of Computers On LAN - powershell

I want to copy a folder to a range of computers on my LAN. Here is what I have so far:
$Computers = "Get-Content C:\Scripts\computers.txt"
$Source = "C:\Install\9_10_00_08HotFix_201504140001"
$Destination = "\\192.168.6.$\c$\Install"
ForEach-Object {
Copy-Item -Path $Source -Recurse -Destination $Destination -Verbose -Force -ErrorAction SilentlyContinue
}

You didn't say what your actual problem is, but it's probably that you misplaced the first double quote in the Get-Content statement, that you can't define a variable destination like you do, and that you don't do anything with what you (try to) read from computers.txt.
Change this:
$Computers = "Get-Content C:\Scripts\computers.txt"
$Source = "C:\Install\9_10_00_08HotFix_201504140001"
$Destination = "\\192.168.6.$\c$\Install"
ForEach-Object {
Copy-Item -Path $Source -Recurse -Destination $Destination -Verbose -Force -ErrorAction SilentlyContinue
}
into this:
$Computers = Get-Content "C:\Scripts\computers.txt"
$Source = "C:\Install\9_10_00_08HotFix_201504140001"
$Computers | ForEach-Object {
Copy-Item -Path $Source -Recurse -Destination "\\192.168.6.$_\c$\Install" -Verbose -Force -ErrorAction SilentlyContinue
}
and your code should work.

Related

New-PSsession to create a loop and wait to finish foreach line in text file

I am trying to get files from servers in a list using the below
$server = Get-Content server.txt
$server| ForEach-Object {
$session=new-pssession -computername $server -credential (Import-Clixml "mycredentials.xml")
Invoke-Command -Session $session -ScriptBlock ${function:getfiles}
Copy-Item -path "C:\some\folder\*" -Destination "C:\localfolder" -recurse -FromSession $session
}
If I supply explicitly a name in -computername, works like a charm.
When there are several names in the list, the execution stops after the first one. I suspect that the session closes after the first execution.
Is there a way to make it like this:
get-content -> for each line execute the copy-item -> close session -> open new session to new server -> .....etc, meaning that $session will be only for the current server.
$function:getfiles
function getfiles {
New-Item -Force -Path C:\path\trace.txt
$remoteserver=$env:computername
$trace='C:\path\trace.txt'
$Include = #('*.keystore', '*.cer', '*.crt', '*.pfx', '*.jks', '*.ks')
$exclude = '^C:\\(Windows|Program Files|Documents and Settings|Users|ProgramData)|\bBackup\b|\breleases?\b|\bRECYCLE.BIN\b|\bPerfLogs\b|\bold\b|\bBackups\b|\brelease?\b|'
Get-ChildItem -Path 'C:\','D:\' -file -Include $include -Recurse -EA 0|
Where-Object { $_.DirectoryName -notmatch $exclude } |
Select-Object -ExpandProperty FullName |
Set-Content -Path $trace
$des = "C:\some\folder\$remoteserver"
$safe = Get-Content $trace
$safe | ForEach-Object{
#find drive-delimeter
$first=$_.IndexOf(":\");
if($first -eq 1){
#stripe it
$newdes=Join-Path -Path $des -ChildPath #($_.Substring(0,1)+$_.Substring(2))[0]
}
else{
$newdes=Join-Path -Path $des -ChildPath $_
}
$folder=Split-Path -Path $newdes -Parent
$err=0
#check if folder exists"
$void=Get-Item $folder -ErrorVariable err -ErrorAction SilentlyContinue
if($err.Count -ne 0){
#create when it doesn't
$void=New-Item -Path $folder -ItemType Directory -Force -Verbose
}
$void=Copy-Item -Path $_ -destination $newdes -Recurse -Container -Verbose
}
}
UPDATE
So I have found out that the file where the lines should be be redirected from the script is not populated, which explains why the next step for copy-item fails. I have tried redirecting in different ways, still cant get it populated. The file is created without issues.
Made a workaround - placed the function in a script which is copied to the remote server / execute it \ clean afterwards.

Batch Replace-Name & Move-item

I got a script:
$CopySource = "C:\Source\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$CopyDestination = "C:\Dest\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$files = Get-ChildItem -File "*.qvd" $CopySource -Force
foreach ($file in $files) {
Copy-Item -path $CopySource\$_$file -Destination $CopyDestination -Force
}
$CopySource = "C:\Source\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$CopyDestination = "C:\Dest\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$items = Get-ChildItem -File "*.qvd" $CopyDestination
foreach($I in $Items) {
$newfilename= rename-item -path $I.Name -newname ("Agresso_" + $I.Name)
The problem I have is that I need to copy files from source to destination and once they are over they need to have agresso_ added. Then they day after a batch of files will be copied again and also they need to be renamed to agresso_ and overwrite the old ones, preferably with move-item. I got a problem already, as I am not used with prefixes,
I tried enless of versions with where-object and simular, could not figure out a way to use test-path either.
I did exactly this but with renaming the files, like this for maximo:
$files = Get-ChildItem -File "*.qvd" $CopySource -Force
foreach ($file in $files) {
Copy-Item -path $CopySource\$_$file -Destination $CopyDestination -Force -Verbose 4>&1 |
Out-File -Append $logpath
}
"Klar med Kopiering av .qvd filer $global:currenttime" | Out-File $logpath -Append
"Påbörjar omdöpning av .qvd filer $global:currenttime" | Out-File $logpath -Append
$items = Get-ChildItem -File "*.qvd" $CopyDestination
foreach($I in $Items) {
$newfilename=$I.Name.Replace("QVDLager1","Maximo")
Move-Item -Path $I.FullName -Destination $CopyDestination\$newfilename -Force -Verbose 4>&1 |
Out-File -Append $logpath
If anyone can help me in the right direction it would be highly appriciated.
You can copy (or move) and rename the destination at the same time:
$CopySource = "C:\Source\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$CopyDestination = "C:\Dest\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
Get-ChildItem -Path $CopySource -File "*.qvd" -Force | ForEach-Object {
# create the full path for the target file with "Agresso_" prefixed
$target = Join-Path -Path $CopyDestination -ChildPath ('Agresso_{0}' -f $_.Name)
$_ | Copy-Item -Destination $target -WhatIf
}
If satisfied with the results of the code shown in the console, you can remove the -WhatIf switch to really start copying (or moving) the files.

Powershell find and move file

I have a long list of find and move operations, I want to slim down the script by substituting the following 2 long commands with 2 short variables.
Short variables:
$f = 'Get-ChildItem -Recurse -Filter'
$m = 'Move-Item -Force -Verbose -Destination V:\MSL\_pdf\'
Long commands:
Get-ChildItem -Recurse -Filter GAS*.pdf | Move-Item -Force -Verbose -Destination V:\MSL\_pdf\GAS
Get-ChildItem -Recurse -Filter GCA_00*.pdf | Move-Item -Force -Verbose -Destination V:\MSL\_pdf\GCA\GCA_00
Get-ChildItem -Recurse -Filter GCA_01*.pdf | Move-Item -Force -Verbose -Destination V:\MSL\_pdf\GCA\GCA_01
And this doesn't work:
$f GAS*.pdf | $m`GAS
$f GCA_00*.pdf | $m`GCA\GCA_00
$f GCA_01*.pdf | $m`GCA\GCA_01
As #Lee said in the comment, you should use function:
function GetAndMove($files, $dest)
{
$dest = "V:\MSL\_pdf\$($dest)"
Get-ChildItem -Recurse -Filter $files | Move-Item -Force -Verbose -Destination $dest
}
# Now call the function
GetAndMove "GAS*.pdf" "GAS"
GetAndMove "GCA_00*" "GCA\GCA_00"
GetAndMove "GCA_01*" "GCA\GCA_01"
...

Powershell Copy-Item not copying

I have to following directory structure:
fold1
- file1
- file2
fold2
- file1
I am trying to test to see if the folders are identical, and if they arent, copy fold1 to fold2 and overwrite any different files. This is what I have tried:
$fold1 = Get-ChildItem -Recurse -path C:\fold1
$fold2 = Get-ChildItem -Recurse -path C:\fold2
$isDif = Compare-Object $fold1 $fold2
if ($isDif -eq $null) {
Write-Host "Folders Are Identical"
}
else {
Write-Host "Folders Are Different"
Copy-Item -Path $fold1.FullName -Destination $fold2.FullName -Recurse -Force
}
But when I run it, it says the folders are different, but it doesn't copy anything over. No errors or anything, it just doesn't work.
I ended up using robocopy instead:
robocopy c:\fold1 c:\fold2 /s
i, just do it
$path1 = "C:\temp2\*"
$path2 = "C:\temp3"
Copy-Item -Path $path1 -Destination $path2 -Recurse -Force -ErrorAction Continue
This is how I would recommend doing the task in powerhsell. It makes it much easier if you create path variables, that way you are not trying to insert records into a directory object.
$path1 = "C:\fold1"
$path2 = "C:\fold2"
$fold1 = Get-ChildItem -Recurse -path $path1
$fold2 = Get-ChildItem -Recurse -path $path2
$isDif = Compare-Object $fold1 $fold2
if ($isDif -eq $null) {
Write-Host "Folders Are Identical"
}
else
{
Write-Host "Folders Are Different"
ForEach($file in $fold1)
{
if($fold2 -notcontains $file)
{
Copy-Item -Path $file.FullName -Destination $path2 -Recurse -Force
}
}
}

Powershell: Copy-Item wildcard destination path

Trying to copy an updated shortcut to a wildcard path. The code works when I run it in a test scenario on the local machine:
$Source1 = "C:\Temp\Updated Shortcut\MyShortcut.lnk"
$destination1 = "C:\Temp\Users\*\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\MyShortcut.lnk"
Get-ChildItem -Path $destination1 | ForEach-Object { Copy-Item -Path $Source1 -Destination $_.DirectoryName }
But running it against the production target path does not work:
$Source1 = "C:\Temp\Updated Shortcut\MyShortcut.lnk"
$destination1 = "U:\Users\*\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\MyShortcut.lnk"
Get-ChildItem -Path $destination1 | ForEach-Object { Copy-Item -Path $Source1 -Destination $_.DirectoryName }
It will also work if I remove the wildcard and use an actual path:
$Source1 = "C:\Temp\Updated Shortcut\MyShortcut.lnk"
$destination1 = "U:\Users\JohnSmith\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\MyShortcut.lnk"
Get-ChildItem -Path $destination1 | ForEach-Object { Copy-Item -Path $Source1 -Destination $_.DirectoryName }
The U:\Users folder does contain 1181 folders in there (one for each of the 1181 users), so not sure if that may be an issue too??
Just because code can be written on one line or using a pipeline, does not mean that code is easy to debug or maintain.
Break your code down, debug it, add some logging, etc.
$Source1 = "C:\Temp\Updated Shortcut\MyShortcut.lnk"
$destination1 = "U:\Users\*\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\MyShortcut.lnk"
$items = Get-ChildItem -Path $destination1
Write-Verbose "Number of items: $($items.Count)" -Verbose
foreach ($item in $items)
{
Write-Verbose "Item: $item" -Verbose
# use -Force here? Does $_ have a DirectoryName property?
#
#Copy-Item -Path $Source1 -Destination $_.DirectoryName
Copy-Item -Path $Source1 -Destination $item -Force -WhatIf
}
My guess is that once you start looking into the diagnostics, you will isolate the issues quickly.