I am trying to automate log copying between multiple remote computers and a shared folder on said remote computer's network. The script is working fine for the most part, but occasionally (I cannot figure out why) an extension-less file is created instead of a file folder, and the logs' contents are copied into it. When I edit it with notepad++, I can see the contents of the log file I was trying to copy into the folder.
I have this script on multiple remote computers, which all transfer the logs to the same shared network folder (but to different sub-directories). On each machine, I have the script executing at the same time (3:00 AM) via a Windows Task.
$sharedFolderPath = "\\xXxXxXx\xXxXxX\xXxXx\"
$yesterdaysDate = (get-date).AddDays(-1).ToString("yyyy-MM-dd")
#Create Daily Directories
if (-Not(Test-Path "$sharedFolderPath$yesterdaysDate")) {
new-item -Path "$sharedFolderPath" -Name $yesterdaysDate -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "BMS" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "BPS" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "DDS" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "POS" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "SCAPPS" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "TRCS" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "Optitrack" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterDaysDate\DDS" -Name "Layer Files" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterDaysDate\TRCS" -Name "Catch-All" -ItemType directory
}
Working correctly, I'd expect a folder called "2019-06-30" (or whatever yesterday's date is) to be made, with folders BMS, BPS, DDS, Optitrack, POS, SCAPPS, and TRCS inside, that have their respective logs stored in them.
After the code I provided, I've added these lines:
#fail-safes
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "DDS" -ItemType directory -Force
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "BMS" -ItemType directory -Force
Which has seemed to fix the problem. However, for the sake of being robust, I would like to know what's causing this so that I can fix it properly.
Related
When destination folder exists the right way of copying files is defined here
Copy-Item 'C:\Source\*' 'C:\Destination' -Recurse -Force
If destination folder doesn't exist, some files in the source subfolders are copied straight into destination, without keeping original folder structure.
Is there a way to have a single Copy-Item command to address both cases and persist folder structure? Or is it too much to ask from Powershell?
You may want to use a if statement with test-path
this is the script i used to fix this problem
$ValidPath = Test-Path -Path c:\temp
If ($ValidPath -eq $False){
New-Item -Path "c:\temp" -ItemType directory
Copy-Item -Path "c:\temp" -Destination "c:\temp2" -force
}
Else {
Copy-Item -Path "c:\temp" -Destination "c:\temp2" -force
}
Able to create file on local server with this command.
$msbuild = "C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild.exe"
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
New-Item -Path $ScriptDir\NITI_20180402.223.txt -ItemType "file" -Value "This is a text string."
But file is not created when build this script on TFS server.
New-Item -Path $ScriptDir\NITI_20180402.223.txt -ItemType "file" -Value "This is a text string."
I tested the script on my side, everything works as expected.
Whatever you can try this (Add -Force):
New-Item -Path $ScriptDir\NITI_20180402.223.txt -ItemType "file" -Value "This is a text string." -Force
If that still not work, please share your build definition settings (capture screenshot is better) and the logs for further troubleshooting.
I have the following script:
param([Parameter(Mandatory=$true)][string]$dest)
New-Item -force -path "$dest\1\" -itemtype directory
New-Item -force -path "$dest\2\" -itemtype directory
New-Item -force -path "$dest\3\" -itemtype directory
Copy-Item -path "C:\Development\1\bin\Debug\*" -destination "$dest\1\" -container -recurse -force
Copy-Item -path "C:\Development\2\bin\Debug\*" -destination "$dest\2\" -container -recurse -force
Copy-Item -path "C:\Development\3\bin\Debug\*" -destination "$dest\3\" -container -recurse -force
The script takes a string and copies all files and folders from the static origin path to the given root string, amending some folders for structure clarity.
It works fine but prints out the results from the "New-Item" commands and I would like to hide that. I've looked at the net and other questions on SE but no definitive answers to my problem were found.
In case someone is wondering - I am using "New-item" at the beginning in order to circumvent a flaw in PS' -recurse parameter not copying all subfolders correctly if the destination folder does not exist. (I.e. they are mandatory)
Option 1: Pipe it to Out-Null
New-Item -Path c:\temp\foo -ItemType Directory | Out-Null
Test-Path c:\temp\foo
Option 2: assign to $null (faster than option 1)
$null = New-Item -Path c:\temp\foo -ItemType Directory
Test-Path c:\temp\foo
Option 3: cast to [void] (also faster than option 1)
[void](New-Item -Path c:\temp\foo -ItemType Directory)
Test-Path c:\temp\foo
See also: What's the better (cleaner) way to ignore output in PowerShell?
I'd like to create link to a folder on desktop of remote computer. I do not have permissions to execute scripts on that computer, but I can copy files to that computer.
My idea was to create link to folder on local computer and then copy the link to remote computer.
But, I am getting error New-Item : Cannot find path 'C:\SomeFolder' because it
does not exist.
Here is my command:
New-Item -Path "c:\Users\pocherka\Desktop\link" -ItemType SymbolicLink -Value "c:\SomeFolder" -Force
Any ideas for workaround?
Try adding the -force parameter:
New-Item -Path "c:\Users\pocherka\Desktop\link" -ItemType SymbolicLink -Value "c:\SomeFolder" -force
You can do using mklink also . Make sure that the destination folder is available . You can use the Test-Path to check that :
$destination = "c:\SomeFolder"
if(Test-Path $destination)
{
cmd /c mklink "c:\Users\pocherka\Desktop\link" $destination
# OR you can use the new-item also. Just commented in the below line
# New-Item -Path "c:\Users\pocherka\Desktop\link" -ItemType SymbolicLink -Value $destination
}
else
{
New-Item $destination -ItemType Directory -Force
cmd /c mklink "c:\Users\pocherka\Desktop\link" $destination
}
Hope it helps
I try to create a folder and a file in one PowerShell command:
I tried:
New-Item hello\test\ (New-Item hello\test\file.txt -type file) -type directory -Force
and
New-Item file.txt (New-Item hello\test\ -type direcotry) -type file -Force
But both don't seem to work as expected.
What I want to achieve is one command to create hello\test\file.txt
Just provide the filepath you want, including the directory, and it will work:
New-Item -Path '.\hello\test\file.txt' -ItemType File -Force
If you want to put the c.txt file in the b folder of the a folder, do so. ni a/b/c.text -Force
If you want to put two files y.txt and z.hml in x folder ni x/y.txt, x/z.html -Force
I created the directory and the file with the following code.
New-Item hello\test -type Directory ; Start-Sleep -Milliseconds 500 ; New-Item hello\test\file.txt
It's not case-sensitive in powershell command, so the most convenient and fastest way to create file would be - > new-item -path . -name 'newfile.txt' -itemtype 'file'
And for new directory, it's the same as in Linux-terminal -> mkdir newdirectory