【发布时间】:2018-04-12 02:02:38
【问题描述】:
我是 powershell 的新手,在遍历目录中具有特定扩展名的文件名之后,我试图解析 WriteAllBytes 方法中的值。我收到错误“使用“2”参数调用“WriteAllBytes”的异常:“空路径名不合法。”
目标是读取主机中的文件名和文件内容,并在远程系统的不同目录下创建具有相同名称和内容的文件。
以下是我正在使用的示例代码。非常感谢您提出宝贵的建议。
#Powershell Version 2.0
$path = $Env:userprofile+"\AppData\Local\Temp\Test_Data\"
$servers = @("server.xyz")
foreach($server in $servers)
{
$username = 'TestUser'
$password = 'TestPassword'
$pw = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ($username, $pw)
foreach ($file in Get-ChildItem $path -Filter *.txt)
{
$TextFileName = $file.Name
$FileToCreateCopyInRemote = "D:\Logs\" + $TextFileName
$myfile = [System.IO.File]::ReadAllBytes($path+$file)
$s = New-PSSession -computerName $server -credential $cred
Enter-PSSession $s
Invoke-Command -Session $s -ArgumentList $myfile -Scriptblock {[System.IO.File]::WriteAllBytes($FileToCreateCopyInRemote, $args)}
Remove-PSSession $s
}
}
Write-Host "Completed"
注意: 调用命令 -Session $s -ArgumentList $myfile -Scriptblock {[System.IO.File]::WriteAllBytes("D:\Logs\Dest.txt", $args)}
这种方法在远程系统中创建单个文件,但我必须在 Dest.txt 的位置解析每个文件名值
【问题讨论】:
标签: powershell-2.0