I have a script being run via scheduled task. Occasionally when the script runs it outputs a broken path. The path should be a folder, named with the current date, however sometimes this path is not a folder but instead a file, type '28 File'. I have attached an image of the broken path below.
The script will only build this path if it does not exist, per the Test-Path shown below. I can only replicate the error if a user deletes the $dailyLocal folder, when the next pass of the script tries to rebuild the path, we see the broken path.
Are there any additional parameters I can put in place to prevent this? Does anyone even understand the error or what '28 File' is?
EDIT: The 28 is from the date format, Powershell thinks I am asking to build a file with extension .28, I have already specified the new path should be a folder. Are there any other measures I can take to specify this as a folder?
#name variables
$bay = 'X1'
$hotFolder = 'C:\Hot_Folder'
$uploadArchive = 'C:\Hot_Folder\Archive'
$today = get-date -Format yyyy.MM.dd
$dailyServer = "\\server\$today $bay"
$dailyLocal = "$uploadArchive\$today $bay"
#build local archive and server archive path for date and bay (test if exists first)
if(!((Test-Path -Path $dailyServer -PathType Container) -or (Test-Path -Path $dailyLocal -PathType Container))){
New-Item -ItemType directory -Path $dailyServer, $dailyLocal
}
#copy to server archive, then move to local archive
$uploadImages = GCI $hotFolder -Filter *.jpg
foreach ($image in $uploadImages){
Write-Host "this is the new valid image" $image -ForegroundColor Green
Copy-Item $image.FullName -Destination $dailyServer -Force
Move-Item $image.FullName -Destination $dailyLocal -Force
}
Related
I am using following code to create a folder with current date name:
Get-ChildItem -Path $Path
$Path = 'C:\temp'
$folderName = (Get-Date).tostring("ddMMyyyy")
New-Item -itemType Directory -Path $Path -Name $FolderName
After this code, I want to add another code that copies files from certain directory and puts them into this newly created folder with current date name.
Thanks
Check this
cp C:\temp\filename.txt "$path\$foldername\"
I am working out a scenario using PowerShell.
As of now, I am trying to workout a scenario where I have a batch job which upon successful execution first creates a date folder for that particular day and then creates a .CSV file under. The folder structure than look like below.
\\Server\SourceA\Processed\20200120\TestA.CSV
when the job runs next day, it will create another folder and file like below.
\\Server\SourceA\Processed\20200121\TestB.CSV
This is how there have been so many folders already created in past.
My PS script is good to run daily after the batch job is completed. I read a date, append to path and it copies file from source to destination folder. But I want to enable my PS script to read all previous date folders created under
\\Server\SourceA\Processed\
Another tricky part is - under the date folder there is few other sub folders. I.e.
\\Server\SourceA\Processed\20191010\Log
\\Server\SourceA\Processed\20191010\Charlie
\\Server\SourceA\Processed\20191010\Alpha
\\Server\SourceA\Processed\20191010\Delta
among them, I only need to read files from Log folder only.
Hence, my actual source path becomes like
\\Server\SourceA\Processed\20191010\Log\TestA.CSV
Here is my script (which is static right now and unable to read past existing date folders).
$fullSourceFileName = "\\Server\SourceA\Processed\"
$date = Get-Date -format "yyyyMMdd"
$fullSourceFileName = "$($fullSourceFileName)$($date)\Log
$destination = "\\Server\DestA\Processed\"
$destination = "$($destination)$($date)\"
get-childitem -path $fullSourceFileName -recurse | copy-item -destination "$($destinationFolder)$($date)\"
Your help is highly appreciated.
I did not know I can use foreach loop in Powershell.
So, here is the answer to read all dynamic date folder under my given path.
I hope this helps the community.
$fullSourceFileName = "\\Server\SourceA\Processed\"
$DirToRead = "\Log\"
$dates = Get-ChildItem -Path $fullSourceFileName -Directory
$destination = "\\Server\DestA\Processed\"
foreach ($date in $dates){
$ActualPath = "$($fullSourceFileName)$($date)$($DirToRead)"
if(!(Test-Path $ActualPath))
{
Write-Output $($ActualPath)$(" source path does not exist")
}
else
{
get-childitem -path $ActualPath -recurse | copy-item -destination "$($destinationFolder)$($date)\"
}
$ActualPath = ""
}
I am trying to copy a folder full of exe and msi files from one server to all servers in the list in a text file.
So far I was lucky to copy or create a empty folder in the destination servers but no luck in copying the content of the folder on the source server. Can some one help me achieve this?
$comname = Get-Content -path 'H:\Installation Files\Patch Update files\server.txt'
Foreach ($value in $comname) {
$ss = "H:\Installation Files\Patch Update Files\Jun 2018"
If (Test-path $ss) {
$dest = "\\$value\H$\Install.*\Patch.*\Jun 2018"
If (Test-path $dest) {
Copy-Item -path $ss -destination $dest -force
}
Else {
Write-output "$dest does not exists"
}
}
Else {
Write-output "$ss does not exists"
}
}
Can someone help in copying all the contents/Files in the source server folder to destination server folder.
I am not getting any error. The script runs fine and copies or create an empty folder on the destination server. How to get the contents/Files in the source server folder to copied to the destination server?
Can’t use regex on Destination folder path.
And use of -Recurse solved the problem of copying all the files in the folder after creating a destination folder.
Else {
New-Item -path $dest -type directory -force
Copy-Item -path $source -destination $dest -Recurse
}
I need to get a TFS build process working where I dont control the agent, or any of the drop locations.
I have a post-build script which is shown Here...but I dont know how to edit it to so that it preserves the folder structure across multiple project directories.
I know I need to edit the file copy itself, but I am unsure of how to go about doing this in powershell...
foreach ($file in $files)
{
#Do something here to create the correct path on the drop server
Copy $file $Env:TF_BUILD_BINARIESDIRECTORY
}
The end goal is that instead of having all the build dlls in a single directory, I have them structured in folders per project.
Following is a simple script to achieve the feature you want, you can update it base on your folder structure and requirement.
Get-ChildItem -Path $Env:TF_BUILD_BUILDDIRECTORY -Filter *.*proj -Recurse | ForEach-Object {
$projectfolder = $_.BaseName
$currentpath = $_.Directory.ToString()
$copypath = $currentpath + '\obj'
$targetpath = $Env:TF_BUILD_BINARIESDIRECTORY + '\' + $projectfolder
Write-Host "Copying files from $copypath to $targetpath"
Copy-Item $copypath $targetpath -Recurse
}
I'm trying to move jpg snapshots from a webcam into daily archive folders via a scheduled powershell script.
The files have the following naming scheme Snapshot-yyyymmdd-hhmmss.jpg and all files of a single day should be moved into a yyyy-mm-dd directory.
So I created the following script that works as expected when called from the PS command prompt, but not from the windows task scheduler:
$day_yesterday = (get-date).AddDays(-1).ToString('yyyy-MM-dd')
$filename = (get-date).AddDays(-1).ToString('yyyyMMdd')
$source = 'E:\WebCam\jpg\Snapshot-'+ $filename + '-*.jpg'
$destination = 'E:\WebCam\jpg\'+$day_yesterday
if (!(Test-Path -path $destination)){
New-Item -Path E:\WebCam\jpg\ -Name $day_yesterday -ItemType directory
}
move-item $source -Destination $destination
When called from a scheduled task, the target directory is created, but the move-item commandlet silently fails, all files stay in the source directory.
Any ideas are appreciated...