【问题标题】:Create folder in remote server with PowerShell [duplicate]使用 PowerShell 在远程服务器中创建文件夹 [重复]
【发布时间】:2021-08-23 10:45:34
【问题描述】:

我尝试制作一个在远程服务器列表中创建文件夹的脚本。我做了这个,但我看到的问题是函数没有正确地制作路径。

这是代码:

$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)
     
    }

}

我把这个“写主机”,我看到功能没有正确地制作路线

这是输出:

\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\

在“...\logs”和“\servicio....”之间有一个空格,我不知道如何解决这个问题。

感谢和抱歉我的英语不好

【问题讨论】:

  • 简而言之:必须调用 PowerShell 函数、cmdlet、脚本和外部程序-foo('arg1', 'arg2')。如果您使用, 分隔参数,您将构造一个命令将其视为单个参数数组。为防止意外使用方法语法,请使用Set-StrictMode -Version 2 或更高版本,但请注意其其他影响。请参阅this answer 了解更多信息。

标签: powershell


【解决方案1】:

像这样调用你的函数

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

不要这样打电话

crea_carpeta($server, $carpeta)

另外,将路径的两个部分连接在一起使用

Join-Path -Path $server -ChildPath $carpeta
# instead of "$server\$carpeta"

【讨论】:

    【解决方案2】:

    使用路径时的良好做法,始终使用Split-PathJoin-PathResolve-Path

    此外,应在 Powershell 中使用 -ArgumentName 调用参数。
    当您使用($server, $carpeta) 调用您的函数时,您实际上是将array 作为参数传递给$Server 参数。

    示例

    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
        }
    }
    

    输出

    \\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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-16
      • 2011-09-30
      • 2022-01-28
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多