Mass file copy to domain computers - powershell

I am trying to take a screen saver file I've made and copy it to all of our desktops and laptops \system32 folder. I created a computers text file and found this script, but I keep getting this error. Any help would be appreciated.
Running this in Powershell 3.0 on a 2012 Server logged in as an admin.
$computers = gc "\\server\share\scripts\computers.txt"
$source = "\\share\scripts\MySlideshow.scr"
$dest = "C:\Windows\System32"
foreach ($computer in $computers) {
if (test-Connection -Cn $computer -quiet) {
Copy-Item $source -Destination \\$computer\$dest -Recurse
} else {
"$computer is not online"
}
}
Error:
Copy-Item : The given path's format is not supported.
At C:\users\tech\desktop\scripts\screen.ps1:6 char:9
+ Copy-Item $source -Destination \\$computer\$dest -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand

The resulting UNC format for you destination is invalid. You're passing
"\\computer\c:\windows\system32"
when you should be passing
"\\computer\c$\windows\system32"
Try quoting the -destination parameter like this too:
Copy-Item $source -Destination "\\$computer\$dest" -Recurse
You'll also need to use single-quotes when assigning to $dest to prevent powershell from trying to expand the dollar sign as a variable sigil.
$dest = 'c$\windows\system32'
Debug your script by using copy-item -whatif ... to ensure that you're passing the correct parameters.

Related

Powershell - Error message for trying to add a date to the end of a copy of a folder

I am new to Powershell and trying to see if I can copy a folder from a Test folder then put it on a Backup folder and rename the folder to the date it was done.
$sourceFile = "C:\Test1\"
$destination = "C:\Backup"
copy-item $sourceFile -destination $destination .\server-backup-$(Get-Date -format "yyyy_MM_dd_hh_mm_ss") -Recurse
However, I keep getting an error saying cannot be found that accepts arguments.
Copy-Item : A positional parameter cannot be found that accepts argument '.\server-backup-2022_01_20_09_32_27'.
At line:5 char:2
+ copy-item $sourceFile -destination $destination .\server-backup-$(Ge ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Is there a better way of going about this or can this error be easily fixed?
You need to concatenate the destination string:
$sourceFile = "C:\Test1\"
$destination = "C:\Backup"
copy-item $sourceFile -destination ($destination + "\server-backup-" + (Get-Date).ToString("yyyy_MM_dd_hh_mm_ss")) -Recurse
I have slightly adjusted your get-date as I am not sure on the output of -format on get-date.
If you have any spaces in the path then this is considered as a different parameter.
To avoid this you can use quotes to encapsulate the string, however you cannot execute functions, parenthesis like I have used and concatenating the string is another approach.

I have an issue with using Copy-Item and a Veriable for destination

This Might be an easy one. But I can figure out what is going wrong with my simple copy script.
I have a shared directory that I am copying items from. I am printing out the destination path to console so I know it is correct But I am receiving a powershell error I do not understand.
Here is my script
#Files to copy
#Get Installers from folder
$APPS = Get-ChildItem \\Server1\shared\APPS -Name
#ForEach loop to identify and move files
ForEach($APP in $APPS) {
$dest = "\\Server1\Shared\APPS\$APP"
#Write-host to see destination path on console
write-host $dest
#copy item from destination path to local directory
Copy-Item $dest -Destination "c:\apps\"
}
This seems straight forward. But I don't understand why I am receiving the following error
\\Server1\Shared\APPS\LTCDesktopSetup.exe
Copy-Item : The filename, directory name, or volume label syntax is incorrect.
At C:\Users\computer1\documents\PowerShell\Moving Installer to local drive.ps1:13 char:2
+ Copy-Item $dest -Destination "c:\apps\"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
Augusto,
I'd suggest this syntax:
$APPS = (Get-ChildItem "\\mybooklive\CMShared\NAS-Downloads" -filter *.exe).FullName
ForEach ($App in $Apps) {
copy-item "$APP" -Destination "G:\Test\Copy-Test" -Force
}
or the more compact:
$APPS = (Get-ChildItem "\\mybooklive\CMShared\NAS-Downloads" -filter *.exe).FullName |
copy-item -Destination "G:\Test\Copy-Test" -Force
Getting FullName vs Name so you don't have to add the source path back in.
Using -Filter so you only get .exe files (this is an assumption from the variable name $Apps).
The force will take care of some IO problems like the file already existing.
HTH
Naturally, you'll have to substitute your paths for my test ones.

$Env variable not recognized inside an if block

I am trying to execute this code, which just basically copies a file from over the network to the local logged on user's desktop if it is older than the one on the network. The first line of the following code works well, but it throws an error for the part where $env:userprofile is used inside an if block. No idea what's going on here.
Copy-Item -Path "\\path1\subpath1\subpath2\Patch\help\*" -Filter *.chm -Destination "$Env:UserProfile\Desktop" -force -Recurse
$chmfileNetwork = Get-ItemPropertyvalue -Path 'path1\subpath1\subpath2\Patch\help\*' -Filter *.chm -Name 'LastWriteTime'
$chmfileLocal = Get-ItemPropertyValue -Path '$Env:UserProfile\Desktop\*' -Filter *.chm -Name 'LastWriteTime'
if ($chmfileLocal -lt $chmfileNetwork) {
Copy-Item -Path "path1\subpath1\subpath2\Patch\help\*" -Destination "$Env:UserProfile\Desktop" -force -Recurse
} else {
echo "Saul good, man"
}
That throws the error
Get-ItemPropertyValue : Cannot find drive. A drive with the name '$Env'
does not exist.
At C:\Users\user1\Downloads\PS2EXE-GUI\psfile.ps1:28 char:17
+ ... fileLocal = Get-ItemPropertyValue -Path '$Env:UserProfile\Desktop\*' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ($Env:String) [Get-ItemPropertyValue], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetItemPropertyValueCommand
You can use $home\Desktop to get a user's desktop folder, but Ansgar Wiechers really pointed out the problem with your script as to why it was throwing the error. You used single quotes on the chmfileLocal = line. Variables are not expanded when you use single quotes, only when you use double quotes. The original script could have been fixed with the change of:
$chmfileLocal = Get-ItemPropertyValue -Path "$Env:UserProfile\Desktop\*" -Filter *.chm -Name 'LastWriteTime'

Compress-Archive command returns "Exception: Stream was too long."

I have a simple script here to archive logs that begin with the name "Archive" then delete those files leaving only the archive.
cd L:\
$Source = Get-ChildItem L:\ | Where{$_.Name -match "^Archive.*\.evtx$"} |Get-ChildItem -name
$CurrentDate = get-date -Format M.d.yyyy
$Destination = "$CurrentDate.zip"
Compress-Archive -Path $Source -destinationpath $Destination
rm L:\$Source
However, I receive the below error when the script runs:
Exception calling "Write" with "3" argument(s): "Stream was too long." At
C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:809
char:29
+ ... $destStream.Write($buffer, 0, $numberOfBytesRead)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IOException
Any suggestions?
Compress-Archive relies upon the Microsoft .NET Framework API System.IO.Compression.ZipArchive to compress files, the maximum file size that you can compress by using Compress-Archive is currently 2 GB. This is a limitation of the underlying API.
Please see more at : here
i would recommend using 7z.exe portable. here is my example script
$TempFolderName = "I:\TempLogs2\"
$source = $TempFolderName+'\Archive'
$destination = $TempFolderName+'\Archive'
$azcopylogs = Get-ChildItem $source -Include *.log -Recurse
foreach ($s in $azcopylogs) {
$azcopylogPath = $s.FullName
$azcopylogName = $s.Name
C:\Tools\7Z\7z.exe a -t7z $azcopylogPath'.7z' -r $azcopylogPath
;if (Test-Path $azcopylogPath'.7z') { Remove-Item $azcopylogPath -force }
}

Get data from CSV to delete users folders from multiple servers

I am very new to powershell and situation is that I have some unneeded users folder that must be deleted on different servers. Path on all servers is f.e. "\server1\hiddenshare$\username" My CSV looks like:
ServerName,FolderName
SERVER1,AAAA
SERVER2,AAA1
SERVER3,AAA2
And I am trying to run this code:
$path = "C:\temp\servers_folders.csv"
$ServerName = Import-Csv -Path $path
$FolderName = Import-CSV -Path $path
Import-Csv -Path $path | % {
Remove-Item -Path \\$ServerName\hiddenshare$\$FolderName -Recurse
}
After all I get this error:
Remove-Item : Cannot find path '\\ \$\ ' because it does not exist.
At C:\temp\servers_folders.csv.ps1:5 char:3
+ Remove-Item -Path \\$ServerName\hiddenshare$\$FolderName -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\\ \usr$\ :String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
you dont need to import your csv more than once.
Inside the foreach block you have to reference the current item by $_
try this, remove -whatif if it's ok
$servers=import-csv .\servers.csv
$servers | %{
remove-item -path \\$($_.servername)\users$\$($_.FolderName) -whatif
}