标签: invoke-command

ScriptBlock中的多个命令

我在Scriptblock参数中运行多个命令时遇到问题.所有文档都指向使用分号在cmdlet之间进行分隔.使用通过分号分隔cmdlet的相同方法在我的本地计算机上工作,但不能通过invoke-command在远程计算机上工作.

例如,下面的代码只返回结果Get-Service,而不是Get-Process.

Invoke-Command -ComputerName cas-bkupexec -ScriptBlock { Get-Service;  Get-Process }
Run Code Online (Sandbox Code Playgroud)

如何实现两个命令成功运行并获得每个命令的输出所需的结果?

powershell invoke-command

5
推荐指数
1
解决办法
1万
查看次数

从 Invoke-Command 返回的无关数据

我正在使用 PowerShell 从远程服务器列表收集数据,然后将其转换为 JSON 对象。一切工作正常,但我得到了一些我似乎无法排除的非常奇怪的输出。

我尝试过管道传输Invoke-Command结果并排除属性。我还尝试从返回的哈希文件中手动删除这些项目,但我似乎无法让它们消失。

我缺少什么?

编辑:

为了找出问题所在,这里有一个简化但仍然损坏的脚本:

$returnedServer = @{}
$pass = cat "C:\...\securestring.txt" | convertto-securestring
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "UserName",$pass
$s = @("xx.xxx.xxx.xxx","xx.xxx.xxx.xxx") 


foreach($server in $s)
{
    $returnedServer.$server += ,(Invoke-Command -ComputerName $server -ScriptBlock 
{
1
}-credential $mycred | select -ExcludeProperty PSComputerName,RunSpaceID,PSShowComputerName) 

$returnedServer| ConvertTo-Json
Run Code Online (Sandbox Code Playgroud)

哪个输出:

{
"xx.xxx.xxx.xxx":  [
                       {
                           "value":  1,
                           "PSComputerName":  "xx.xxx.xxx.xxx",
                           "RunspaceId":  "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                           "PSShowComputerName":  xxxx
                       }
                   ],
"xx.xxx.xxx.xxx":  [
                       {
                           "value":  1,
                           "PSComputerName":  "xx.xxx.xxx.xxx",
                           "RunspaceId":  "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"",
                           "PSShowComputerName":  xxxx
                       }
                   ]
}
Run Code Online (Sandbox Code Playgroud)

powershell json remote-server invoke-command

4
推荐指数
1
解决办法
4432
查看次数

Powershell-将多个参数传递给Invoke-Command

我正在尝试编写单行代码以远程利用netbackup的某些功能。我知道如何在最后使用-args [0]和[1]将参数传递给Invoke Command,并重复参数。我要完成的示例:

CC =国家/地区代码(由于命名约定,将重复

SS =网站(也根据命名约定重复)

Invoke-Command -ComputerName RemoteServer -ScriptBlock {& "C:\Program Files\Veritas\NetBackup\bin\admincmd\bpplinfo.exe" CC0SITE_VMW_BRON -set -L -M CC0SITEb0100d0a.s0SITE.CC.DOMAIN.COM} 
Run Code Online (Sandbox Code Playgroud)

在获得用户输入并声明参数之后,它似乎没有传递给invoke-command

Invoke-Command -ComputerName RemoteServer -ScriptBlock {& "C:\Program Files\Veritas\NetBackup\bin\admincmd\bpplinfo.exe" $args[0]0$args[1]_VMW_BRON -L -M $args[0]0$args[1]b0100d0a.s0$args[1].$args[0].DOMAIN.com} -Args $CCode, $Site
Run Code Online (Sandbox Code Playgroud)

powershell invoke-command

4
推荐指数
1
解决办法
2万
查看次数

$Using:var 在 Invoke-Command 内的 Start-Job 中

我正在使用Invoke-Command,并且在-ScriptBlock我正在使用的范围内Start-Job。我必须使用$Using:varinside Start-Job,但会话正在本地会话中查找声明的变量(之前声明Invoke-Command)。这是我正在做的事情的一个非常简短的例子:

Invoke-Command -ComputerName $computer -ScriptBlock {
    $sourcePath = 'C:\Source'
    $destPath = 'C:\dest.zip'
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    $includeBaseDirectory = $false
    Start-Job -Name "compress_archive" -ScriptBlock {
        Add-Type -AssemblyName System.IO.Compression.FileSystem
        [System.IO.Compression.ZipFile]::CreateFromDirectory("$using:sourcePath","$using:destPathTemp",$using:compressionLevel,$using:includeBaseDirectory)
    }
}

Invoke-Command : The value of the using variable '$using:sourcePath' cannot be retrieved because it has not been set in the local session.
At line:1 char:1
+ Invoke-Command -ComputerName vode-fbtest -ScriptBlock {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Command], …
Run Code Online (Sandbox Code Playgroud)

powershell invoke-command powershell-jobs

4
推荐指数
1
解决办法
2318
查看次数

本地计算机上带有 FilePath 的 PowerShell Invoke-Command - 模糊参数错误?

我想使用 Invoke-Command 在本地计算机上的文件中运行脚本,以便我可以使用 -ArgumentList 传入参数。我遇到了一个我不明白的错误,所以我简化了我的命令。当我这样做时:

Invoke-Command -FilePath 'getprocess.ps1'
Run Code Online (Sandbox Code Playgroud)

getprocess.ps1的内容是:

Get-Process
Run Code Online (Sandbox Code Playgroud)

我收到的错误消息是:

Invoke-Command :无法使用指定的命名参数解析参数集。

行:1 字符:1

+ 调用命令 -FilePath 'getprocess.ps1'

+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException

+FullyQualifiedErrorId:AmbigouslyParameterSet、Microsoft.PowerShell.Commands.InvokeCommandCommand

我对这个错误消息感到困惑。这是什么意思?我该如何让它发挥作用?

powershell invoke-command

4
推荐指数
1
解决办法
7695
查看次数

Powershell 调用命令访问被拒绝错误 - 不是双跳

我正在使用 Server 2016 和一些 Windows 10 客户端构建一个新网络。我已在所有客户端上成功运行 Enable-PSRemoting。从我运行的服务器:

Invoke-Command -ComputerName $computer -Scriptblock {'test'}
Run Code Online (Sandbox Code Playgroud)

这会导致 [computer] 连接到远程服务器失败,并显示以下错误消息:访问被拒绝。+ CategoryInfo:OpenError:(计算机:字符串)[],PSRemotingTransportException + ExcellentQualifiedErrorId:AccessDenied,PSSessionStateBroken

因为我只让远程计算机处理一个字符串,所以我相信这与我在论坛中看到的常见双跳问题无关。

我还可以在客户端计算机上成功运行 WinRM:

Test-WSMan $computer
Run Code Online (Sandbox Code Playgroud)

如果有人深入了解访问被拒绝错误的其他原因,我将非常感谢一些关于在哪里查找的想法。

谢谢。

powershell access-denied invoke-command

3
推荐指数
1
解决办法
3万
查看次数

powershell:用参数启动程序的脚本?

当我在下面运行Powershell脚本时,我收到以下错误.如何通过参数PowerShell运行程序?该脚本将是一个组策略登录.

Invoke-Expression:找不到接受参数'\ TBHSERVER\NETLOGON\BGInfo\BGIFILE.bgi/timer:0/s ilent/nolicprompt'的位置参数.在X:\ Systems\scripts\PowerShell\UpdateDesktopWithBGInfo.ps1:6 char:18 + Invoke-Expression <<<< $ logonpath $ ArguList + CategoryInfo:InvalidArgument :( :) [Invoke-Expression],ParameterBindingException + FullyQualifiedErrorId:PositionalParameterNotFound, Microsoft.PowerShell.Commands.InvokeExpressionCommand

$LogonPath = $env:LOGONSERVER + "\NETLOGON\BGInfo\Bginfo.exe" 
$ArguList = $env:LOGONSERVER + '\NETLOGON\BGInfo\BGIFILE.bgi /timer:0 /silent /nolicprompt '
invoke-command $LogonPath
Invoke-Expression $logonpath $ArguList
Run Code Online (Sandbox Code Playgroud)

authentication powershell group-policy invoke-command

2
推荐指数
2
解决办法
2万
查看次数

将 invoke 命令的输出封装在一个变量中 - PowerShell

我有一个在远程机器上安装远程桌面服务的脚本(来自 DC)。

我现在正处于检查连接代理(服务器)和连接主机(服务器)上是否安装了 RDS 的阶段。

我想使用 invoke-command 因为远程 powershell 会话似乎太复杂了。

这是我的代码:

$res = Invoke-Command -ComputerName "testpc.eil.local" -ScriptBlock {
if((Get-WindowsFeature -Name "Remote-Desktop-Services").Installed -eq 1)
{
   #i need this output (true or false or a string)
}
else
{
     #i need this output (true or false or a string)
}
}


Write-Host $res
Run Code Online (Sandbox Code Playgroud)

但我的问题是,如何将 invoke-command 中脚本块的输出封装在 DC 可以访问的变量中?如果 RDS 已成功安装或未能写入日志文件,我正在尝试写入

我们如何封装函数的输出并将其传递给运行它的机器?

谢谢

variables powershell scripting invoke-command storing-data

2
推荐指数
1
解决办法
1万
查看次数

如何从 PowerShell 运行 *.exe 文件

我在 C:\Folder 有一个文件夹,其中包含文件 input.xml、output.xml 和 licensegenerator.exe。Licensegenerator.exe 获取我们放入 input.xml 的变量,并使用 output.xml 文件为我们的程序之一创建临时许可证。我们通常通过命令行导航到 C:\Folder 目录,然后运行以下命令:

LicenseGenerator.exe "C:\Folder\input.xml" "C:\Folder\output.xml"
Run Code Online (Sandbox Code Playgroud)

我正在尝试编写一个脚本来在 PowerShell 中执行完全相同的操作,但我正在努力……这是我所拥有的:

$inputtest = "C:\Folder\Input.xml"
$outputtest = "C:\Folder\Output.xml"
$licensegen = "C:\Folder\LicenseGenerator.exe"

Invoke-Command $licensegen "$inputtest" "$outputtest"
Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我收到错误:

调用命令:找不到接受参数的位置参数
'C:\Folder\Output.xml'。
在行:5 字符:1
+ 调用命令 $licengegen "$inputtest" "$outputtest"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~
    + CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

我也试过运行,Invoke-Expression但得到完全相同的错误(除了开头说“调用表达式”)。有人知道我在这里做错了什么吗?

powershell invoke-command

2
推荐指数
1
解决办法
3万
查看次数

Powershell - 从远程机器上运行 exe 的 Invoke-Command 捕获输出

我需要在一组远程服务器上配置审计策略。我正在尝试使用 Invoke-Command commandlet 在每台服务器上运行 auditpol.exe。问题是我似乎无法从 auditpol 命令中捕获任何输出。

我尝试了显而易见的(将 Invoke-Command 的结果分配给一个字符串):

> $command =  "Start-Process -FilePath `"auditpol.exe`" -ArgumentList `"/set`", `"/subcategory`", `"```"File System```"`", `"/success:enable`""
> $command
"auditpol.exe" -ArgumentList "/set", "/subcategory", "`"File System`"", "/success:enable"

> $out = Invoke-Command -ComputerName MyServer -ScriptBlock {$command}
> $out
>
Run Code Online (Sandbox Code Playgroud)

但是 $out 是空的。

我还使用 Wait-Job 和 Receive-Job尝试了此 MSDN 博客中详述的方法。结果有些令人鼓舞,但尚无定论:

> $command =  "Start-Process -FilePath `"auditpol.exe`" -ArgumentList `"/set`", `"/subcategory`", `"```"File System```"`", `"/success:enable`""
> $command
"auditpol.exe" -ArgumentList "/set", "/subcategory", "`"File System`"", "/success:enable"
> $job = Invoke-Command -ComputerName MyServer …
Run Code Online (Sandbox Code Playgroud)

powershell invoke-command

2
推荐指数
1
解决办法
9607
查看次数