问题描述
我写这个脚本是为了在远程机器上执行命令。 如果我不带参数输入它,它运行良好
PS D:\batch> .\remoteExecute.ps1 -computer myServer -command 'dir'
Directory: C:\Users\AC0476BF\Documents
Mode LastWriteTime Length Name PSComputerName
---- ------------- ------ ---- --------------
----- 5/31/2021 4:27 PM WindowsPowerShell myServer
Executing: 'dir' on remote computer myServer
PS D:\batch> .\remoteExecute.ps1 -computer myserver -command 'dir c:\windows'
The term 'dir c:\windows' is not recognized as the name of a cmdlet,function,script file,or operable program. Check the spelling of the name,or if a path was included,verify that the path is correct and try again.
+ CategoryInfo : ObjectNotFound: (dir c:\windows:String) [],CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : myServer
Executing: 'dir c:\windows' on remote computer myServer
我错过了什么?
param (
[Parameter(Mandatory = $true,HelpMessage = "Remote computer")]
[String]$computer,[Parameter(Mandatory = $true,HelpMessage = "Command to be executed on the remote computer")]
[String]$command,[Parameter(Mandatory = $false,HelpMessage = "Logging of command enabled to filename specified")]
[String]$logFile,HelpMessage = "Execute in Command Shell. Default is PowerShell")]
[Switch]$cmdshell
)
try {
if ($cmdshell) {
$command = "cmd --% /c '" + $command + "'"
}
if ($logFile) {
$executeCommand = $command + " *>> " + $logfile
}
else {
$executeCommand = $command
}
Invoke-Command -computerName $computer -ScriptBlock { & $using:executeCommand }
$message = "Executing: '" + $executeCommand + "' on remote computer " + $computer
Write-Host $message
exit 0
}
catch {
$message = $PSItem.ToString()
Write-Host $message
# logging goes here...
throw
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)