如何从带有参数的远程计算机执行本地脚本

时间:2016-01-15 10:04:23

标签: powershell network-printers

我正在尝试从远程计算机上找到一种在我的电脑上运行PowerShell脚本的方法。

我需要能够从我们域中的任何PC清除随机网络打印机的打印队列。

我的电脑上有一个小脚本(示例mypc01),我需要能够从其他电脑执行它(例如somepc01)并传递参数,如:

$ printservername(“1prinsrv”)& $打印机名称( “hp00014”)

问题:我需要启动此脚本的pc没有管理员权限(不是我可以使用启动的问题),pc没有安装最新的PowerShell(大问题)我需要执行的命令在旧版本的PowerShell中不存在)

脚本:

 $printername = Read-Host "give the printername in please"
 $servername = Read-Host "give the servername in please"
 Get-PrintJob -ComputerName $servername -PrinterName $printername | ` where         JobStatus -like "Error*","Paused*" | Remove-PrintJob

有人可以帮我找到正确的方法吗?

1 个答案:

答案 0 :(得分:0)

假设您在自己的计算机上安装了PowerShell 3.0或更高版本,则可以将Invoke-Command-FilePath-ArgumentList参数一起使用,但首先您需要将脚本重写为支持参数绑定:

param(
    [string]$PrinterName = $(Read-Host "Input printer name"),
    [string]$ServerName = $(Read-Host "Input server name")
)

Get-PrintJob -ComputerName $servername -PrinterName $printername | Where {
  $_.JobStatus -like "Error*" -or $_.JobStatus -like "Paused*" 
} | Remove-PrintJob

现在我们可以做到:

Invoke-Command -ComputerName anypc01 -FilePath C:\ClearPrintQueue.ps1 -ArgumentList "printer123","printerserver01"

反过来(从远程调用计算机上的本地脚本),你可以这样做:

Invoke-Command -ComputerName mypc {
    C:\ClearPrintQueue.ps1 -PrinterName "printer123"
}