there's got to be something blatantly obvious on this one that I'm doing wrong, but I need another set of eyes to help me out because I'm not getting any errors - it's just not doing what I want... I need to push a software installer file to "X:\folder\Software Install Files" and backup the existing files to "X:\folder\Software Backups" on a bunch of different servers. In order to do that, I need to make sure these folders exist first and create them if they don't. The script below gathers the $computer variable from a servers.txt file, then for each computer I look to the registry to find out what drive the software is currently installed on, then create the appropriate folders on the drive if they don't exist:
# This file contains the list of Servers
$computers = gc "C:\Folder\Subfolder\Servers.txt"
clear-host
# The Command below pulls all the variables above and performs the file copy
foreach ($computer in $computers) {
#Gather Version and Program Directory Information from Registry
$machinename = $computer
$icakey = "SOFTWARE\\Wow6432Node\\Program\\Program"
$ICAVers = "Version"
$ICADrive = "InstallDir"
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',
$machinename)
$icaregkey = $reg.opensubkey($icakey)
$ICAV = $icaregkey.getvalue($IcaVers)
$ICAD = $icaregkey.getvalue($IcaDrive)
#Declare Directory Drive Letter As Variable
$DriveLetter,$Folder = $ICAD.split(":")
#Declare path for folders
$Softwarepath = "\\$computer\$driveletter$\Folder\Software Install Files"
$BackupPath = "\\$computer\$driveletter$\Folder\Software Backups"
#Create Software Folder
If(test-Path -path $SoftwarePath)
{Write-host "$SoftwarePath already exists on $computer" -ForegroundColor Red}
ELSE
{{New-Item -Path $SoftwarePath -ItemType Directory}
{write-host "$SoftwarePath created on $computer"}}
#Create WebAccess Backups Folder
If(test-Path -path $BackupPath)
{Write-host "$BackupPath already exists on $computer" -ForegroundColor Red}
ELSE
{{New-Item -Path $BackupPath -ItemType Directory}
{write-host "$BackupPath created on $computer"}}
The results I'm getting show this for every server in the servers.txt:
New-Item -Path $SoftwarePath -ItemType Directory
write-host "$SoftwarePath created on $computer"
New-Item -Path $BackupPath -ItemType Directory
write-host "$BackupPath created on $computer"
It's not actually creating the folder, and in previous times I've done something like this, the variables in the result displayed the value they were representing, and the "clear-host" at the top made sure the only result was the stuff that should have been in the write-host line.
Again, I feel like this is something blatantly obvious, but I'm tearing my hair out to figure it out.
To avoid a long discussion in comments, I think I may have found what was missing in your code. Indeed it had to do with the paths you set up for
$Softwarepath
and$BackupPath
where you forgot the$
sign beforeFolder
. The directories were probably created, but not where you expected them. They will have ended up in\\computername\X$\Folder\Software Install Files
.Anyway, here's a revised version of your code:
Hope that helps