Help me improve my script

by Dec 18, 2014


I'm learning PowerShell.  In order to lean certain thing I create script (which I use for tasks) in a basic way
to understand what I want to learn.  In this scrip I want to learn testing a Path for existence of a file and folder

My script does work (in a way) it does copy, however, I want it to test if a folder exist and initially it test correctly if the folder is not
present and it gets create.

Later in the script, I test again to ensure the folder and file is present and write to the console indicating that the folder is there.

The issue is that I"m not notified that the foler and file are present because the test-path is still showing "false"

I know the scrip can be made more technical, however, if it too technical it makes it hard for me to understand it and it how it works.

What do I need to change to have it write to console that the folder/file are present after it gets copied.

Am I using the "If" statements correctly?

Should I be using some sort of script block and if so how/where?

Thank you for your time

//////////// CODE

$Workstation = Read-Host "Computer Name"

Write-Host "Prepping Remote Computer " -BackgroundColor Blue -ForegroundColor White
Write-Host ""

# Check if PC is Online
$PCOnline = Test-Connection -ComputerName $WorkStation -Quiet
If ($PCOnline -eq $False) {Write-Host "Check Hostname as PC is not On or does not Exist" -BackgroundColor Blue -ForegroundColor White}

# Java Version
$JavaCurrentVersion = 'jre-8u25-windows-i586.exe'

# Create Variable for TEMP Folder and Java Existance

# $FolderTEMP = Test-Path \$WorkstationC$TEMP -PathType Container
$FolderTEMP = Test-Path \$WorkstationC$TEMP
$AppJava = Test-Path \$WorkstationC$TEMPjre-8u25-windows-i586.exe -PathType Leaf

# If Folders Don't Exist – Create them and notify actions
If ($FolderTEMP -eq $false) {Write-Host "TEMP Folder in $WorkStation NOT present . . . Creating Directory" -BackgroundColor Blue -ForegroundColor White}
If ($FolderTEMP -eq $false) {New-Item -Path \$WorkstationC$TEMP -ItemType Directory}
Write-Host ""

# If ($FolderTEMP -eq $True) {Write-Host "TEMP Folder Created . . . Starting Transfer of Java to $Workstation" -BackgroundColor Blue -ForegroundColor White}

If ( (Test-Path "$FolderTEMP") -eq  $True) {Write-Host "TEMP Folder Created . . . Starting Transfer of Java to $Workstation" -BackgroundColor Blue -ForegroundColor White}

Write-Host ""

# If Java not preset in Temp Copy

If ($AppJava -eq $false) {Copy-Item 'C:ToolsJAVAjre-8u25-windows-i586.exe' -Destination "\$WorkStationC$TEMPjre-8u25-windows-i586.exe"}

# Confirm Java is in temp location
$AppJava1 = Test-Path \$WorkstationC$TEMPjre-8u25-windows-i586.exe -PathType Leaf

If ($AppJava1 -eq $True) {Write-Host "Java has been succesfully Copied to $WorkstationTEMP Folder" -BackgroundColor Blue -ForegroundColor White}
If ($AppJava1 -eq $False) {Write-Host "Java is NOT present at $WorkStationTEMP folder" -BackgroundColor Blue -ForegroundColor White}