0

I just want to create a new directory named with a date an hour. And I want to copy something from another directory to new created directory. Like a back-up operation.

Well I'm new learner about powershell. I've tried to read all docs from microsoft. But I clearly don't understand what am I missing?

So my script is like this without a function block and it's working:

     if (Test-Path -Path "$pathBackup\$finalDate") {
       Remove-Item $pathBackup\$finalDate -Recurse -Force         
    }
 New-Item -Path $pathBackup -Name $finalDate -ItemType Directory
 [string]$backupDirectory = "$pathApplication\*"
 [string]$backupDestination = "$pathBackup$backupDate"
 Copy-item -Force -Recurse -Verbose $backupDirectory -Destination $backupDestination

But When I try this steps in a function block. For a modular struct. Because I want to use this function with different parameters. So my function is like this: and it's not working.

function createDir ($pathName, $folderName){ 

 if ((Test-Path -Path "$path$folderName")) {

     Remove-Item "$path$folderName" -Recurse -Force

 }

New-Item -ItemType Directory -Path $pathName -Name $folderName 
[string]$backupSource = "$pathApplication\*"
[string]$backupDestination = "$pathName$folderName"
Copy-item -Force -Recurse -Verbose $backupSource -Destination $backupDestination
 }

I think the ps reads my variables as a System.Object but I should use String. But I don't know how to do

I'm calling the function like this:

  createDir($pathBackup, $finalDate)

And Powershell doesn't recognize pathBackup and finalDate variables. Instead of this ps creates a folder which is named System.Object[] in location of powershell scripts path.

eagerdev
  • 83
  • 1
  • 6
  • 1
    [Edit] the question and show how 1) the function is called, and 2) what exactly happens then. – vonPryz Aug 08 '22 at 11:53

1 Answers1

0

First of all i would suggest to use the following parameter notation (but this is just personal preference). It would be interessting how you call the function. Do you even call the funciton ? Maybe its not working because you forgot to call the function ?

function createDir {
param(
    [Parameter(Mandatory=$true)]
    pathName,

    [Parameter(Mandatory=$true)]
    folderName,
 )

if ((Test-Path -Path "$path\$folderName")) {##Here is a missing backslash

     Remove-Item "$path\$folderName" -Recurse -Force ##Also here

 }

New-Item -ItemType Directory -Path $pathName -Name $folderName 
[string]$backupSource = "$pathApplication\*"
[string]$backupDestination = "$pathName$folderName"
Copy-item -Force -Recurse -Verbose $backupSource -Destination $backupDestination

 }
}
    ##FunctionCall
    createDir -pathName "C:\Test2\" -folderName "Test1"

EDIT: It seems your function call is the problem. enter image description here

Take a look in this article: StackOverflow How do I pass multiple parameters into a function in PowerShell?

You should call your function like:

createDir -pathName $pathBackup -folderName $finalDate
LKo.exp
  • 164
  • 7
  • I edited the function as you said and tried to call it as you said, but this time I got the following error: A positional parameter cannot be found that accepts argument '-'. As you said, I have some deficiencies in the functions part, I think I'd better review and learn it properly. – eagerdev Aug 08 '22 at 12:14
  • 2
    No `$` in the parameter names when calling the function. – notjustme Aug 08 '22 at 12:15
  • @notjustme. You are right. My bad. I copy pasted the variable names and forgot to remoe the $ ;) Now you should be good to go. Try again. Sorry :) – LKo.exp Aug 08 '22 at 12:16
  • 1
    createDir -pathName $pathBackup -folderName $finalDate I think this one is right. – eagerdev Aug 08 '22 at 12:17
  • @LKo.exp I'm not the OP, just spotted the error. :) – notjustme Aug 08 '22 at 12:18
  • 1
    @notjustme. Yeah i know ;) Thank you for spotting the mistake :) OP (eagerdev) should try again with updated syntax :) – LKo.exp Aug 08 '22 at 12:20
  • 2
    With the uncertainty of whether or not `$path` contains a trailing "\" I would suggest the use of `Join-Path` rather than simple string manipulation... – Keith Miller Aug 08 '22 at 12:42