Home > database >  Powershell Functions doesn't take parameters properly
Powershell Functions doesn't take parameters properly

Time:08-09

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.

CodePudding user response:

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
  • Related