让脚本在远程服务器上工作

时间:2012-11-12 10:19:10

标签: powershell powershell-v2.0

我希望以下代码可以在多台计算机上运行 - 任何想法如何做到这一点?我有以下但它失败了,因为我目前没有在有问题的服务器中调用我认为。

谢谢,

CODE:

Write-Host "Script to check Storage Write, Read and Delete Times"
Write-Host "`n"

$computer = Get-Content -path d:\temp\servers.txt
$path = "f:\temp\test.txt"

Foreach ($storage in $computer)
{

$date = Get-Date
Write-Host "Script being run on $date"

$write = Measure-Command { new-item -Path $path -ItemType File -Force } | select TotalMilliseconds 

Write-Host "Writing file on $storage took $write"

$read = Measure-Command { Get-Content -Path $path } | select TotalMilliseconds 

Write-Host "Reading file on $storage took $read"

$delete = Measure-Command {Remove-Item -Path $path -Force } | select TotalMilliseconds 

Write-Host "Deleting file on $storage took $delete"
Write-Host "`n"
}

1 个答案:

答案 0 :(得分:1)

您需要退一步重新考虑这种方法。您每次都向f:\ temp发出文件系统命令,该命令位于本地系统上。

有两种方法可以使远程计算机执行文件系统任务。最简单的方法是使用UNC路径。也就是\\server\share格式。假设您有本地管理员访问权限:

Foreach ($storage in $computer) {
$uncpath = $("\\{0}\f`$\temp\text.txt" -f $storage)
$write = Measure-Command { new-item -Path $uncpath -ItemType #...
# rest of code uses $uncpath for access
}

请注意,使用UNC路径会给局域网带来一些麻烦,因此这种类型的测试可能会或可能不够准确。

第二种方法是使用Powershell远程连接在远程系统上连接并在那里发出命令。请查看New-PSSessionEnter-PSSessionExit-PSSession cmdlet。

相关问题