我试图使用invoke-command在远程计算机上执行exe.使用远程桌面登录计算机后,在远程计算机上执行exe需要1GB内存,并在一分钟后执行完成.而当我在同一台机器上使用Invoke-Command执行相同的exe时,进程返回OutOfMemoryException并突然结束.我的invoke命令很简单Invoke-Command -Session $someSessionVariable -ScriptBlock {Invoke-Expression "abc.exe --arg arg"} -AsJob.
我是否遗漏了有关远程调用限制的内容?
提前致谢.
我想使用Powershell来调用远程机器上的批处理文件.此批处理文件包含参数.这是我到目前为止所拥有的:
$script = "\\fileshare\script.cmd"
$server = $args[0]
$args [string]::join(',',$args[1 .. ($args.count-1)])
Invoke-Command -computername $server {$script + ' ' + $args}
Run Code Online (Sandbox Code Playgroud)
经过一些搜索,我发现Invoke-Command函数在一个全新的进程中运行它的scriptblock,因此你不能在其中放入变量(它们不会被扩展).这就是-ArgumentList标签的用途.所以我尝试了这个......
Invoke-Command -computername $server {\\fileshare\script.cmd} -ArgumentList "FirstArgument"
Run Code Online (Sandbox Code Playgroud)
这也不起作用......我的批处理脚本告诉我它没有传递任何参数.我找不到任何明确说明的内容,但看起来-ArgumentList参数只适用于Powershell脚本(它不会将它们提供给批处理脚本).
有关如何使用Invoke-Command调用带参数的批处理文件的任何想法?
我正在编写一个Powershell脚本,它执行我的构建/部署过程中的一个步骤,它需要在远程计算机上运行一些操作.该脚本相对复杂,因此如果在远程活动期间发生错误,我需要详细的堆栈跟踪,指出错误发生在脚本中的位置(超出已经生成的日志记录).
问题在于,当从远程机器中继终止异常时,Invoke-Command丢失堆栈跟踪信息.如果在本地计算机上调用脚本块:
Invoke-Command -ScriptBlock {
throw "Test Error";
}
Run Code Online (Sandbox Code Playgroud)
返回所需的异常详细信息:
Test Error
At C:\ScriptTest\Test2.ps1:4 char:2
+ throw "Test Error";
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (Test Error:String) [], RuntimeException
+ FullyQualifiedErrorId : Test Error
Run Code Online (Sandbox Code Playgroud)
但如果远程运行:
Invoke-Command -ComputerName $remoteComputerName -ScriptBlock {
throw "Test Error";
}
Run Code Online (Sandbox Code Playgroud)
异常堆栈跟踪指向整个Invoke-Command块:
Test Error
At C:\ScriptTest\Test2.ps1:3 char:1
+ Invoke-Command -ComputerName $remoteComputerName -ScriptBlock {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (Test Error:String) [], RuntimeException
+ FullyQualifiedErrorId : Test Error
Run Code Online (Sandbox Code Playgroud)
我可以手动将异常传回本地机器:
$exception = Invoke-Command -ComputerName $remoteComputerName -ScriptBlock {
try …Run Code Online (Sandbox Code Playgroud) 我有一个问题,我有一个脚本:
PSSession管理员帐户)我想在服务器上启动进程,所以我用PSSession连接(没问题)
我做Invoke-Command:
# $pathProg path to my program
Invoke-Command -session $mySession -command {Start-Process $($args[0])} -ArgumentList $pathProg
Run Code Online (Sandbox Code Playgroud)
但它什么都不做(我用VNC验证)
我也做Invoke-Command:
# $pathProg path to my program
Invoke-Command -session $mySession -command {&$($args[0])} -ArgumentList $pathProg
Run Code Online (Sandbox Code Playgroud)
这个程序很好(好)但我的脚本等待结束程序(不好)
有人有想法吗?
谢谢
第一个短代码,然后问题
$session = New-PSSession -ComputerName someServer
$servicesList = "Service1", "Service2", "Service3"
Invoke-Command -ScriptBlock {
Param ($newServicesList)
Write-Host $newServicesList
} -ArgumentList $servicesList -Session $session
Remove-PSSession $session
Run Code Online (Sandbox Code Playgroud)
问题是为什么Invoke-Command块中的Write-Host只给出这个输出?
Service1
Run Code Online (Sandbox Code Playgroud)
谢谢你的回答
我一直试图解决这个问题.我已经看过其他文章,并尝试了一切,但我没有得到任何地方.我试图将一个数组作为参数传递给另一个PS脚本.这是我正在尝试的:
$IPArray = Get-Content C:\ListofIps.txt
Invoke-Command -Computername $server -Credential $cred -FilePath "C:\script.ps1" -ArgumentList (,$IPArray)
Run Code Online (Sandbox Code Playgroud)
$ IPArray中的5个值未传递给我调用的脚本.
在此先感谢,伙计们,我真的很感激您能给予的任何帮助......
我在远程会话下使用Invoke-Expression并且在抛出异常时 - 它只返回RemoteException而没有任何堆栈跟踪信息.例:
try
{
Invoke-Expression "$command 2>&1"
}
catch
{
Write-Host $_
}
Run Code Online (Sandbox Code Playgroud)
如果我将重定向错误排除到output(2>&1) - 我得到了正确的错误,但它调用了不需要的调试控制台(来自$ command),这是使用重定向隐藏的.
Start-Process -NoNewWindow -FilePath $CmdExe -ArgumentList $Arguments
Run Code Online (Sandbox Code Playgroud)
使用Start-Process我可以看到完整的堆栈跟踪,但也有不需要的调试控制台.
如何从远程会话下的抛出异常中获取完整的堆栈跟踪和正确的异常?谢谢.
我编写了一个非常简单的 powershell 脚本来将 json 字符串发布到服务器。当我执行脚本时遇到一个奇怪的问题。
服务器位于我们的内网内,所有内网服务器的防火墙均已禁用。
我尝试在不同的地方执行脚本:
在我自己的计算机上的第三方命令提示符应用程序中启动 powershell.exe
Citrix seesion 中的 Powershell 窗口(Windows 10、powershel 5)
我检查了所有 6 个地方都有 ExecutionPolicy“RemoteSigned”,
但是,我只能成功执行最后 3 个位置的脚本,
在前 3 位,我收到错误
Invoke-RestMethod : Unable to connect to the remote server
At F:\Temp\test.ps1:46 char:9
+ $resp = Invoke-RestMethod -Method POST -Body $result ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-RestMethod],
WebException
+ FullyQualifiedErrorId : System.Net.WebException,Microsoft.PowerShell.Com
mands.InvokeRestMethodCommand
Run Code Online (Sandbox Code Playgroud)
此外,我也尝试过Invoke-WebRequest …
我试图测量一些在PowerShell中写入文件的方法.毫无疑问,但我不明白为什么Measure-Command下面的第一个声明要比第二个声明执行更长的时间.
它们是相同的,但在第二个中我写了一个脚本块发送到Invoke-Command第一个脚本块,我只运行命令.
关于Invoke-Command速度的所有信息我都能找到关于远程处理的信息.
此块大约需要4秒钟:
Measure-Command {
$stream = [System.IO.StreamWriter] "$PSScriptRoot\t.txt"
$i = 0
while ($i -le 1000000) {
$stream.WriteLine("This is the line number: $i")
$i++
}
$stream.Close()
} # takes 4 sec
Run Code Online (Sandbox Code Playgroud)
并且下面的代码完全相同,但是在传递给的脚本块中写入Invoke-Command大约需要1秒钟:
Measure-Command {
$cmdtest = {
$stream = [System.IO.StreamWriter] "$PSScriptRoot\t2.txt"
$i = 0
while ($i -le 1000000) {
$stream.WriteLine("This is the line number: $i")
$i++
}
$stream.Close()
}
Invoke-Command -ScriptBlock $cmdtest
} # Takes 1 second
Run Code Online (Sandbox Code Playgroud)
怎么可能?
我正在尝试Azure Table Storage API使用 Powershell执行,Invoke-RestMethod但它返回415 error。
这是Powershell代码:
$accessKey = "accesskey"
$account_name = "storage account"
$table_name = "table storage"
$date = "Fri, 10 Nov 2017 00:47:55 GMT"
$api_version = "2015-12-11"
$table_url = "https://$account_name.table.core.windows.net/$table_name"
$data_type = "application/json"
$signature = "generated signature"
$body = @{
Address="Mountain View";
Age="23"
}
$json = $body | Convertto-JSON
$table_headers = @{
"x-ms-date" = $date
"x-ms-version" = $api_version
"Authorization" = "SharedKey $account_name`:$signature"
"Content-Type" = $data_type
}
Invoke-RestMethod -Method POST -Uri $table_url -Headers …Run Code Online (Sandbox Code Playgroud) invoke-command ×10
powershell ×9
arrays ×1
azure ×1
batch-file ×1
cmdlets ×1
remoting ×1
session ×1
windows ×1