1
votes

Please help me. I'm trying trying to create a PowerShell script using do-while loop as follows:

  1. Ask user for folder name (foldername) and target IP Address (targetipaddress)
  2. If "foldername" does not exist, then create "foldername" in "C:\" drive
  3. Using "targetipaddress" as the part of target path, copy the contents from a sub folder of network drive path,"S:\" and paste into the "foldername" in the "C:\" path.
  4. If user enters "quit", then script finishes.

My code below:

$var = "welcome to IP address and folder creation world"
echo $var

# create foldername until user type "quit"
$output = (command)
do { 

$foldername = Read-Host "enter folder name "
$targetipaddress = Read-Host "enter target ip address "

# create folder name only if it does not exist
function createFolder {
$path = "\\$targetipaddress\C$\$foldername "
If (!(Test-Path $path))
{ 
      New-Item -ItemType Directory -Force $path
}

} # end of function createFolder

# copy contents from a sub folder of a network drive path and
# and paste them onto "C:\" drive path using "targetipaddress" and "foldername"

function copyContents {
 Copy-Item S:\sourcefolder\subfolder\*.*  \\$targetipaddress\C$\$foldername\ -Recurse
 }

 } while ($foldername = "quit") # end of do-while loop
1
Don't forget to ask your question. - Bill_Stewart
You define functions inside the do..while but never use them? - user6811411
hi LotPings, I'm confused if do-while loop is the right approach. Maybe it is "do loop until ". Thanks - Ben Q
Ben the problem is that you (re-)define the functions with every iteration of the do while. But you never use the function by invoking it by it's name. So move the function definition outside the do..while and use the name or remove the definition with it's curly braces as it has no advantage here. This is pretty basic stuff in any language. - user6811411
($foldername = "quit"): That's not how you compare for equality in PowerShell. Use -eq, not =. There are other comparison operators. - Mike Sherrill 'Cat Recall'

1 Answers

1
votes

See below for a working example, read carefully and compare this one with your own.

Some of the problems / changes.

  • echo? it should be write-host
  • unnecessary and unused functions.
  • $output = (command), it does nothing.
  • do while, i like to use while($true) and break out of it.
  • optional, error handling.
  • powershell does not use = (i believe you meant ==) for equels logic but -eq
  • a few other problems / changes.

some reading materials:

  • Comparison Operators See link from Mike Sherrill 'Cat Recall'
  • Continue
  • Break

If you get an error with this code please post the inputs voor $source, $foldername, $targetipaddress and $path and i will look at it.

Code:

clear-host #clears your console, optional.
$ErrorActionPreference = "Stop" #prevents al lot of posible disasters from happening.

Write-Host "welcome to IP address and folder creation world" #Powershell way of writing to the console.

$source = "S:\sourcefolder\subfolder\*"
#$source = "C:\source\*" i used this for testing on my local machine.

# create foldername until user type "quit"
While ($true){ 

    "

    "#Add some white space between the questions.

    $foldername = Read-Host "enter folder name "
    If ($foldername -eq "quit") {break} #exit the while loop

    $targetipaddress = Read-Host "enter target ip address " #you could use a hostname to.

    $path = "\\$targetipaddress\C$\$foldername"

    #if connection to targetipaddress fails then abort.
    if(!(Test-Connection $targetipaddress -ErrorAction SilentlyContinue)) #look at the ! it means it should be false.
    {
        Write-Warning "Could not connect to '$targetipaddress'"
        continue #goes back to the begining of the while loop.
    }

    #if path does not exist try to create it.
    if(!(Test-Path $path)) #look at the ! it means it should be false.
    {
        try { New-Item -ItemType Directory -Path (Split-Path -Parent $path) -Name $foldername | Out-null } #Out-null prevenst unwanted console output.
        catch {
            Write-Warning "Could not create folder '$foldername' on '$targetipaddress'"
            continue #goes back to the begining of the while loop.
        }
    }

    #try to copy files.
    try { Copy-Item -Path $source -Destination $path -Recurse -Force }
    catch {
        Write-Warning "An Error occured while copying the files from '$source' to '$path'"
        continue #goes back to the begining of the while loop.
    }

    #if all goes well then tell them ;)
    Write-Host "files copied successfully from $source to $path"

}