将日志复制到网络共享位置不起作用

时间:2019-10-01 17:55:10

标签: powershell

将日志复制到网络共享位置不起作用

我在线上找到了以下脚本,并根据需要对其进行了修改。该脚本会按预期将“ logs.zip”生成为“ SystemRoot \ CCM \ logs”,但是当我尝试将“ logs.zip”复制到网络共享驱动器时,它将失败

我正在使用以下命令将“ logs.zip”复制到“ \ networkshare \ software \ Logs”

 $Computerlogshare = “\\networkshare\software\Logs” + $env:Computername
Copy-Item $env:SystemRoot\CCM\Logs\logs.zip -Destination $Computerlogshare -force 


# Script to run SetupDiag to troubleshoot Windows 10 Setup
# Download SetupDiag.exe from https://go.microsoft.com/fwlink/?linkid=870142 and place in same directory as this script

# Get the CCM Logs location from registry
$LogLocation = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\CCM\Logging\@Global" -Name LogDirectory | Select -ExpandProperty LogDirectory
#$LogLocation = "$env:SystemRoot\CCM\Logs"

# Get the location we're running from (or use $PSScriptRoot)
$ScriptPath = Split-Path $MyInvocation.MyCommand.Path -Parent



# Check that .Net 4.6 minimum is installed
If (Get-ChildItem "HKLM:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" | Get-ItemPropertyValue -Name Release | ForEach-Object { $_ -ge 393295 })
{
    Try
    {
        Start-Process -FilePath "$ScriptPath\SetupDiag.exe" -ArgumentList "/Output:$LogLocation\SetupDiagResults.log" -Wait -ErrorAction Stop
    }
    Catch
    {
       "[ERROR] There was an error starting SetupDiag.exe: $_" | Out-file -FilePath "$LogLocation\SetupDiagResults.log" -Force 
    }
}
Else
{
    "[ERROR] .Net Framework 4.6 is required to run SetupDiag.exe" | Out-file -FilePath "$LogLocation\SetupDiagResults.log" -Force
}

 $Computerlogshare = “\\networkshare\software\Logs” + $env:Computername
    Copy-Item $env:SystemRoot\CCM\Logs\logs.zip -Destination $Computerlogshare -force 

2 个答案:

答案 0 :(得分:0)

首先,在代码中请勿使用卷曲的“ smart-quotes”,并将其替换为直的。
然后,您使用"\\networkshare\software\Logs" + $env:Computername连接路径,这将导致\\networkshare\software\LogsYourMachineName

我的猜测是您需要使用Join-Path,因此生成的目标路径将变为
\\networkshare\software\Logs\YourMachineName。 您需要在执行Copy-Item之前检查该路径是否存在,是否创建该路径。

类似这样的东西:

$Computerlogshare = Join-Path -Path "\\networkshare\software\Logs" -ChildPath $env:Computername
if (!(Test-Path -Path $Computerlogshare -PathType Container)) {
    New-Item -Path $Computerlogshare -ItemType Directory -Force | Out-Null
}
Copy-Item "$env:SystemRoot\CCM\Logs\logs.zip" -Destination $Computerlogshare -Force 

希望有帮助

答案 1 :(得分:0)

您可以尝试将路径更改为:

$Computerlogshare = Join-Path -Path "Microsoft.Powershell.Core\FileSystem::\\networkshare\software\Logs" -ChildPath $env:Computername