PS script runs but doesn't copy over the specified folder - copy-item

My script run with no errors, but when I verify that it has copied one folder to the next that folder has nothing init, there are files in the source folder.
Clear-Host
Get and set varible names
$ComputerName1 = Read-Host "What is the name of your computer?"
$ComputerName2 = Read-Host "What is the name of the computer that has the errors?"
If folder exists, overwrite it
If(test-path "\$ComputerName2\c$\ProgramData\Micro Focus\"){Remove-item "\$ComputerName2\c$\ProgramData\Micro Focus\"}
Copy from one computer to the next
Copy-Item -Path "\$ComputerName1\c$\ProgramData\Micro Focus\" "\$ComputerName2\c$\ProgramData\Micro Focus\"
Delete folder from all users
Remove-Item -path "\$ComputerName2\c$\Users*\AppData\Roaming\Micro Focus\"
Read-Host -Prompt "Micro Focus Fix complete, press Enter to Exit"

Was browsing some questions and saw yours. In case you never figured this out, you need to add the -recurse parameter on your copy-item command. So:
Copy-Item -Path "\$ComputerName1\c$\ProgramData\Micro Focus\"
"\$ComputerName2\c$\ProgramData\Micro Focus\" -Recurse
The Copy-Item command will not automatically copy any sub directories associated with the source path. The -Recurse parameter tells the command to copy all sub directories, and any items contained in those directories.

Related

Copy and delete files from SFTP folder

I have to pick (remove) the files with file mask FileName_A_* and FileName_B_* from SFTP location and place them in an sharedrive.
I tried using WinSCP. I have created an HourlyFile.txt file with below code and placed it under C:\Program Files (x86)\WinSCP . Another batch file HourlyFile.bat to execute the script from HourlyFile.txt
HourlyFile.txt:
option batch abort
option confirm off
open sftp..........
get -filemask="FileName_A_*" /outbound/test/* \\sharedrive
get -filemask="FileName_B_*" /outbound/test/* \\sharedrive
del /outbound/test/FileName_A_*
del /outbound/test/FileName_B_*
exit
HourlyFile.bat:
winscp.com /script=HourlyFile.txt
pause
I tried with below options to delete the file but got the error message "Unknown command". Also the above code is copying subfolder from /outbound/test/ , which it should not.
Commands tried:
del /outbound/test/FileName_A_*
-del /outbound/test/FileName_A_*
delete /outbound/test/FileName_A_*
delete /outbound/test/FileName_A_20190604_090002
delete /outbound/test/FileName_A_20190604_090002.csv
If you want to download and delete the files, you better use -delete switch of the get command. This way, you can be sure that WinSCP deletes only those files that were really successfully downloaded.
get -delete /outbound/test/FileName_A_* \\sharedrive\
get -delete /outbound/test/FileName_B_* \\sharedrive\
See WinSCP article How do I create script that synchronizes files and deletes synchronized files from source afterward?
To answer your literal question: WinSCP has no del command. WinSCP has rm command:
rm /outbound/test/FileName_A_*
rm /outbound/test/FileName_B_*
On some Unix servers the wildcard command would be:
mrm /outbound/test/FileName_A_*
, in the event
rm /outbound/test/FileName_A_*
doesn't work, returning error:
rm: Access failed: File not found (/outbound/test/FileName_A_*)

Robocopy everything in subdirectory excluding the root files

How do I use robocopy so the root contents are not copied?
I already have root files stored elsewhere and I just would like to copy the sub directories and their contents while the source folder still containing root directory contents.
This is not possible with native robocopy switches as far as I can tell. You will need to use a script to enumerate the subdirectories and run robocopy against them.
Here is a sample PowerShell command that will accomplish what you want, copying everything from C:\temp\source\ to c:\temp\target\, excluding the files that are in c:\temp\source:
get-childitem c:\temp\source\* |?{$_.PsIsContainer} | %{robocopy $_.FullName c:\temp\target\$($_.Name) /S}
Credit to powershell ignore files in root but robocopy folders and their contents for the basics of this.
I don't have a reputation but the answer Mr. Hinkle gave solved 2 days of effort and searching. My challenge was moving the files that were > 1hour of age. This combination of powershell and robocopy appears to work. Below is my final code.
# Clear screen
cls
# Disconnect the drive if it exist - don't know where it is pointing to
If (Test-path p:) {
net use p: /delete
}
#Map the destination
net use p: \\server\dir1\dir2\dir3 password /USER:domain\user /P:no
get-childitem -path 'D:\dir1\dir2\' |
where-object {$_.LastWriteTime -lt (get-date).Addhours(-1)} |
?{$_.PsIsContainer} |
%{robocopy $_.FullName p:\$($_.Name) /S /MOVE /r:3 /W:1}
net use p: /delete

Force create existing folder with Meteor

When I use the command:
meteor create myfolder
It won't by default allow meteor to install itself if the folder is already existing. I can't find an option to force it. Is it really necessary that Meteor creates the directory by itself first or is this just because of 'good practice'?
I want to automate the creation of a folder first and run the meteor command afterwards, hence the question.
Maybe you want a shell script like the following:
$DIR = <some variable>
if [ ! -f "$DIR" ]; then
meteor create tempDir
mv tempDir/* "$DIR/"
rmdir tempDir
fi
which will copy the contents into your new directory, as long as it doesn't contain any Meteor files already.

Powershell script to create folder, website and deploy using MSDeploy.exe

I would like to deploy a web application on Windows 2008 R2. I know the separate PowerShell commands to do various tasks. But I would like to put this into a nice PowerShell script.
I just need the syntax, can you please help me to do the following actions:
Test if C:\Inetpub\MyWebsite folder exists, if not, create it.
Test in IIS7 if MyWebsite exists, if not create it (I know how to Import-Module WebAdministration and call New-WebSite)
Now the complicated part. I deploy a Web site from a package prepared by Visual Studio 2010. VS supplies a .cmd file where I just need to execute it from a DOS prompt. This means I have to leave the PS Console, open a DOS Console to run that cmd file. Is it possible to run a .cmd file from within a PowerShell console ?
To answer your questions:
Import-Module WebAdministration
# Check for physical path
$sitePath = "c:\inetpub\MyWebsite"
if (-not (Test-Path -path $sitePath))
{
New-Item -Path $sitePath -type directory
}
# Check for site
$siteName = "MyWebSite"
$site = Get-WebSite | where { $_.Name -eq $siteName }
if($site -eq $null)
{
Write-Host "Creating site: $siteName"
# Put your New-WebSite code here
}
# Execute your .cmd here
c:\PathToScript\MakeMySite.cmd
You can run .cmd scripts from within PowerShell just fine.
I have also changed a little bit. Using the same Test syntax to test if a website exists or not:
if (-not (Test-Path -path IIS:\Sites\$SiteName))
{
New-WebSite -Name $SiteName ...etc...
}
Also for executing the *.cmd file, I lift some code from the web and saw that people use & to execute external command. Hope that you are OK:
& c:\PathToScript\MakeMySite.cmd arg1 arg2
Thank you very much for your help.
If you need to run the .cmd file as administrator, you can use the following code:
Start-Process -FilePath C:\PathToScript\MakeMySite.cmd -Verb RunAs -ArgumentList "/y"

Run a shell script on all files within a directory and its sub-directories

Good evening fellow computational authors,
I'm trying to run the following script:
find . -name '*.php' -exec /search_replace.sh {} \;
so that it runs search_replace.sh on all the .php files in a folder and its sub-folders. I keep on getting the error:
find: /search_replace.sh: No such file or directory
Any assistance?
change
/search_replace
to
./search_replace
or whatever the full path to the script is...

Resources