用于创建文件夹并将文件复制到远程服务器的 Powershell 脚本 选项 1:使用 Invoke-Command选项 2:没有 Invoke-Command

问题描述

我编写了一个下面的 PowerShell 脚本来在远程计算机上创建一个文件夹(如果不存在)并将文件复制到远程位置,我收到以下错误,任何人都可以帮助解决这个问题。

$serverlist = Get-Content "C:\serverlist.txt"
foreach ($server in $serverlist){
    Invoke-Command -ComputerName $server -ScriptBlock {

    $testpath = Test-Path "\\$server\D$\scripts"
        if($testpath -eq $true)
        {
            Write-Host "Path exist on $env:COMPUTERNAME,copying scirpt to the location"
            Copy-Item -Path \\hostx\copyshare\scirpts\IISLog.ps1 -Destination \\$server\D$\scripts -PassThru
        }
        else
        {
            #create a new folder on C drive
            Write-Host "Creating new folder...... "
            New-Item -Path \\$server\D$ -Name "scripts" -ItemType Directory
            sleep -Seconds 4
            Write-Host "Copying the script to destination...."
            Copy-Item -Path \\hostx\copyshare\scirpts\IISLog.ps1 -Destination \\$server\D$\scripts -Verbose
        }
    }
}

错误

The path is not of a legal form.
+ CategoryInfo          : InvalidArgument: (\\\D$\scripts:String) [New-Item],ArgumentException
+ FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.NewItemCommand
+ PSComputerName        : XXHOST


The network path was not found.
+ CategoryInfo          : NotSpecified: (:) [Copy-Item],IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
+ PSComputerName        : XXHost

我注意到在 if 循环开始后,$server 变量数据正在释放,不知道为什么会发生这种情况。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)