【问题标题】:Why can't Powershell Copy-Item -Source -Destination to a UNC path known as a Variable see below为什么 Powershell Copy-Item -Source -Destination 不能到称为变量的 UNC 路径,见下文
【发布时间】:2012-08-11 18:49:11
【问题描述】:
#VARIABLES
$Source = Read-host "Please provide DIR location you wish to copy its contents from.   
ie.  (UNC, Absolute paths, etc)"
$Serverlist = Read-Host "Please provide the location of the server list. (UNC, Absolute   
paths, etc)"
$Servers = Get-Content -Path $Serverlist
#$Destination1 = \\$Server\c$\inetpub\wwwroot\'
#$Destination2 = \\$Server\c$\wmpub\wmroot\'
#EXECUTION
$Servers | foreach-object      {                            
    Copy-Item -Path $Source*.wsx -Destination $Server\c$\inetpub\wwwroot\ -  
force
    Copy-Item -Path $Source*.nsc -Destination $Server\c$\wmpub\wmroot\ -force
                           }
#EVENTVWR LOGGING
$EventLog = New-Object System.Diagnostics.EventLog('Application')
$EventLog.MachineName = "."
$EventLog.Source = "Streaming Media Engineering"
$EventLog.WriteEntry('Copy Successful',"Information", 200)
#VIEWING OVERLOADS
($myevent.WriteEntry).OverloadDefinitions
Add-Type -AssemblyName System.Speech
$synthesizer = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
$synthesizer.Speak('Files have been moved over successfully...')

在尝试执行 Copy-Item 循环时,我似乎无法让脚本读取 $Destination1 和 $Destination2 路径中的 $Server 变量。请帮助已经花了几天的时间......

【问题讨论】:

  • 您收到的错误信息是什么?
  • 基本没有错误但没有预期的结果。
  • 您的服务器列表在每个服务器之前都包含 \\,对吧?你可以试试Copy-Item -Path $Source*.* -filter *.wsx -Destination $Server\c$\inetpub\wwwroot\ -Force 看看有没有什么不同? Copy-Item -Path (join-path $Source *.wsx) -Destination $Server\c$\inetpub\wwwroot\ -Force呢?

标签: variables copy powershell-2.0 unc copy-item


【解决方案1】:

变量 $Server 未在管道内设置。您必须将其分配给 $Servers 中的当前管道对象:

# For PS2 and above:
$Servers | ForEach-Object {
$Server = $_
...

# If you are sure that you will always use PS3 or above
$Servers | ForEach-Object {
$Server = $PSItem
...

希望这会有所帮助。

附:我注意到这个问题是 2 年前发布的,我现在是一个掘墓人:P

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2022-07-07
    • 2018-07-31
    • 1970-01-01
    相关资源
    最近更新 更多