Powershell script: Create file on TFS server using New-Item - powershell

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.

Related

Create a powershell script to place a custom word template in the templates folder

I want to place a word template, template.dotm into the Word custom templates folder.
Using Office 365, latest version of Word. Windows 10. Apologies if my terminology is incorrect, still a powershell/programming novice.
This folder doesn't exist by default, and the directory Word looks for default templates in doesn't exist by default either. If a user has created a template, then it will create an expanding string named PersonalTemplates at the following registry key: HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Word\Options, with the value being the directory they've elected as their default custom templates directory.
I want to make a script which:
Checks for presence of PersonalTemplates. If present, and value is not null, store as $regvalue.
If not present, or value is null, create expanding string with the following value $newreg at HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Word\Options.
Then copy template.dotm into the $regvalue or $newreg. Powershell will be run from the same directory as the template.dotm is stored in.
I've got a bunch of snippets which do some of the principle operations, though I can't work out how to tie them together, and am missing some bits which I just can't work out:
Copy the template to the destination
ForEach ($user in (Get-ChildItem "C:\Users" -Exclude Public)) {
New-Item -ItemType Directory -Force -Path "C:\Users$($user.Name)\Documents\Custom Office Templates"
Copy-Item template.dotm -Destination "C:\Users$($user.Name)\Documents\Custom Office Templates"
Create registry key with value
Set-Location -Path
'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options'
New-ItemProperty -Path
'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' -Name
'PersonalTemplates' -Value "C:\Users$($user.Name)\Documents\Custom
Office Templates" -PropertyType ExpandString -Force }
Get regvalue
$regvalue = (Get-ItemPropertyValue
'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options'
'PersonalTemplates')
I have put together your code snippets in order, also corrected the logic for checking if the Registry key is present or not.
ForEach ($user in (Get-ChildItem "C:\Users" -Exclude Public))
{
$location = "C:\Users\$($user.Name)\Documents\Custom Office Templates"
$IsPresent = Get-ItemProperty 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' | ForEach-Object {If($_ -like '*PersonalTemplates*'){ Return 'True' }}
if(-Not($IsPresent -eq 'True'))
{
New-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' -Name 'PersonalTemplates' -Value $location -PropertyType ExpandString -Force \\Not tested
New-Item -ItemType Directory -Force -Path $location
}
$existingValue= Get-ItemPropertyValue -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' -Name 'PersonalTemplates'
if([string]::IsNullOrWhiteSpace($existingValue)){
Set-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' -Name 'PersonalTemplates' -Value $location
}
else{
$location=$existingValue
if(!(test-path $existingValue))
{
New-Item -ItemType Directory -Force -Path $existingValue
}
}
Copy-Item template.dotm -Destination $location
}
I have not tested the creation of registry key as I am on my work laptop, so assuming that line of code works.
Also, question for you: With this approach wouldn't the registry entry have the single value that of the first user folder, you may have to look into the logic? I feel you may have to run this script for each user after they login using $env:Username instead of looping through the user folder. But I could be wrong, there may be other who could suggest better.

New-Item Creating Extension-less File Instead of Directory

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.

How to create symbolic link for non-existing folder?

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

Create directory and file in same command using PowerShell

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

Piping out to "New-Items" in PowerShell

I'm trying to output to a file that is created on the fly, but I can't seem to get either to work. Here's that portion of my code-
New-Item -Path $LogPath -Name $InfoLog -Type File -Force
New-Item -Path $LogPath -Name $ErrorLog -Type File -Force
"Script started at: $DateStamp_${TimeStamp}" | $InfoLog
I've also tried just ">>" instead of the pipe. The script runs fine, it just doesn't pipe the output into the file. Instead it pipes it out to a a files called "0" in the directory the script ran from.
Three things.
First, New-Item outputs the item it creates. So unless you do something with the output objects, New-Item will output the new file items to the pipeline. So I think you might want to say:
New-Item -Path $LogPath -Name $InfoLog -Type File -Force | Out-Null
Second, since you're not specifying the path to the file you want to write to, PowerShell will assume the current location.
Finally, if you want to write output to a log file, you probably want to use Out-File. Perhaps something like this:
$infoLogPath = Join-Path $LogPath $InfoLog
"Script started at: $DateStamp_${TimeStamp}" | Out-File $infoLogPath
Join-Path combines the directory and filename into a fully-qualified path name.