Please help me. I'm trying trying to create a PowerShell script using do-while loop as follows:
- Ask user for folder name (foldername) and target IP Address (targetipaddress)
- If "foldername" does not exist, then create "foldername" in "C:\" drive
- 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.
- 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
($foldername = "quit"): That's not how you compare for equality in PowerShell. Use-eq, not=. There are other comparison operators. - Mike Sherrill 'Cat Recall'