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

1
  • 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 or higher, but note its other effects. See this answer for more information. Commented Jun 5, 2021 at 20:53

2 Answers 2

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"
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.