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

Powershell 将日志复制到网络共享位置不起作用,powershell,Powershell,将日志复制到网络共享位置不起作用 我在网上找到了下面的脚本,并根据我的需要进行了修改。该脚本按预期生成logs.zip到SystemRoot\CCM\logs,但是当我尝试将logs.zip复制到网络共享驱动器时,它失败了 我正在使用下面的命令将logs.zip复制到“\networkshare\software\logs” $Computerlogshare = “\\networkshare\software\Logs” + $env:Computername Copy-Item $env

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

我在网上找到了下面的脚本,并根据我的需要进行了修改。该脚本按预期生成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 
首先,不要在代码中使用卷曲的“智能引号”,而是用直引号代替。 然后,使用\\networkshare\software\Logs+$env:Computername连接路径,这将导致\\networkshare\software\LogsYourMachineName

我猜您需要使用,因此生成的目标路径将变为 \\networkshare\software\Logs\YourMachineName。 在执行复制项目之前,需要检查该路径是否存在,如果不存在,则需要创建该路径

大概是这样的:

$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 

希望对您有所帮助

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

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

您是否可以从正在启动脚本的网络访问网络共享。请提供调试日志或错误消息。由于您是新手,因此可能不知道这一点,但通常通过单击✓ 在左边。这将有助于其他有类似问题的人更轻松地找到它。要澄清引号问题,“\\networkshare\software\Logs”具有特殊的开始和结束引号,这些引号在Powershell中无效,可能已在Word文档或电子邮件中复制或复制。如果启用了智能引号,Word会将粘贴的文本替换为倾斜文本。在Word和记事本中,视觉上的差异是细微的,但在StackOverflow使用的代码字体中,视觉上的差异更为明显。将文本更改为\\networkshare\software\Logs可能会修复错误。接得好,西奥。