0

I try to make a script who create folder in a list of remote server. I make this but the problem who i see is the function not make correctly the path.

This is the code:

$p_serverstq = "\\127.0.0.1\test_share\logs" ,"\\192.168.1.10\test_share\logs"

$p_carpetas = "\servicio_prueba\winevt", "\servicio_prueba\iislog", "\servicio_prueba\applogs"


function crea_carpeta {
    param($server, $carpeta)
  
    #New-Item -ItemType directory "$server\$carpeta"    
    Write-Host "$server\$carpeta"
}


foreach ($server in $p_serverstq) {

    foreach($carpeta in $p_carpetas) {

      crea_carpeta($server, $carpeta)
     
    }

}

i put this "write-host" and i see the function dont make correctly the route

this is the output:

\127.0.0.1\test_share\logs \servicio_prueba\winevt
\127.0.0.1\test_share\logs \servicio_prueba\iislog
\127.0.0.1\test_share\logs \servicio_prueba\applogs
\192.168.1.10\test_share\logs \servicio_prueba\winevt
\192.168.1.10\test_share\logs \servicio_prueba\iislog
\192.168.1.10\test_share\logs \servicio_prueba\applogs\

between the "...\logs" and "\servicio...." have a space i dont know how fix this.

Thanks and sorry for my bad english

mklement0
  • 382,024
  • 64
  • 607
  • 775
Nicolas
  • 7
  • 2
  • 1
    In short: PowerShell functions, cmdlets, scripts, and external programs must be invoked _like shell commands_ - `foo arg1 arg2` - _not_ like C# methods - `foo('arg1', 'arg2')`. If you use `,` to separate arguments, you'll construct an _array_ that a command sees as a _single argument_. To prevent accidental use of method syntax, use [`Set-StrictMode -Version 2`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode) or higher, but note its other effects. See [this answer](https://stackoverflow.com/a/65208621/45375) for more information. – mklement0 Jun 05 '21 at 20:53

2 Answers2

2

Call your function like this

crea_carpeta $server $carpeta
# or
crea_carpeta -server $server -carpeta $carpeta

Do not call like this

crea_carpeta($server, $carpeta)

Also, to join 2 parts of a path together use

Join-Path -Path $server -ChildPath $carpeta
# instead of "$server\$carpeta"
Daniel
  • 4,792
  • 2
  • 7
  • 20
2

Good practice when working with paths, always use Split-Path, Join-Path, Resolve-Path.

In addition, arguments should be called using -ArgumentName in Powershell.
When you called your function using ($server, $carpeta) you are actually passing as argument an array to the $Server parameter.

Example

PS /> ($server, $carpeta)
\\192.168.1.10\test_share\logs
\servicio_prueba\applogs
PS /> ($server,$carpeta).GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                       
-------- -------- ----                                     --------                                                                                                       
True     True     Object[]                                 System.Array                                                                                                   

PS /> [string]($server,$carpeta)
\\192.168.1.10\test_share\logs \servicio_prueba\applogs

PS /> Write-Host ($server, $carpeta)
\\192.168.1.10\test_share\logs \servicio_prueba\applogs
$p_serverstq = "\\127.0.0.1\test_share\logs" ,"\\192.168.1.10\test_share\logs"
$p_carpetas = "\servicio_prueba\winevt", "\servicio_prueba\iislog", "\servicio_prueba\applogs"


function crea_carpeta {
param(
    [string]$Server,
    [string]$Carpeta
)
  
    #New-Item -ItemType directory "$server\$carpeta"    
    Join-Path $server -ChildPath $carpeta
}


foreach ($server in $p_serverstq)
{
    foreach($carpeta in $p_carpetas)
    {
        crea_carpeta -Server $server -Carpeta $carpeta
    }
}

Output

\\127.0.0.1\test_share\logs\servicio_prueba\winevt
\\127.0.0.1\test_share\logs\servicio_prueba\iislog
\\127.0.0.1\test_share\logs\servicio_prueba\applogs
\\192.168.1.10\test_share\logs\servicio_prueba\winevt
\\192.168.1.10\test_share\logs\servicio_prueba\iislog
\\192.168.1.10\test_share\logs\servicio_prueba\applogs
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37