Windows &引用;RPC服务器不可用";仅当运行脚本时
我对Powershell有问题。我尝试运行我的脚本Windows &引用;RPC服务器不可用";仅当运行脚本时,windows,powershell,Windows,Powershell,我对Powershell有问题。我尝试运行我的脚本 Param ( [string]$ComputerName ) $ComputerName = $ComputerName -replace " ", "," Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName | select csname, @{LABEL='LastBootUpTime' ;EXPRESSION= {$_.ConverttoDateTime($_
Param (
[string]$ComputerName
)
$ComputerName = $ComputerName -replace " ", ","
Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName | select csname, @{LABEL='LastBootUpTime' ;EXPRESSION= {$_.ConverttoDateTime($_.lastbootuptime)}}
我用以下方法运行它:
\GetBootTime.ps1-ComputerName localhost,
我收到错误消息:
获取WMIOObject:RPC服务器不可用。(不适用于
HRESULT:0x800706BA)
但是,如果我运行:
Get-WmiObject Win32_OperatingSystem -ComputerName localhost,<another computer> | select csname, @{LABEL='LastBootUpTime' ;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
Get WmiObject Win32_OperatingSystem-ComputerName localhost,|选择csname,@{LABEL='LastBootUpTime';EXPRESSION={$\.ConverttoDateTime($\.lastboottime)}
剧本的主线是什么
然后它就起作用了。
有什么建议吗?您的问题是将
Computername
定义为字符串[string]
,而不是字符串数组[string[]
。因此,输入的localhost,example
不是解释为两台计算机,而是一台名为“localhost,example”的计算机。如果使用[string[]]
(或不定义它),则,
字符将被解析为字符串数组中的分隔符。由于Get WmiObject
可以获取一个数组,因此它将为数组的每个元素运行一次
您可以使用-split
对空格和逗号进行自己的解析,但最好首先提供格式正确的数组-split
之所以使用,是因为$ComputerName-replace”“,“
只会将[string]
用逗号代替空格,而不会在数组中拆分为多个元素
Param (
[string[]]$ComputerName
)
# Manual parsing to split space delimited into two elements
# e.g. 'localhost Example' into @(localhost,Example) - Not a good practice
$ComputerName = $ComputerName -split " "
Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName | select csname, @{LABEL='LastBootUpTime' ;EXPRESSION= {$_.ConverttoDateTime($_.lastbootuptime)}}
最可能的错误是告诉您它无法在
上与WMI通信。您可以将测试简化为\GetBootTime.ps1-ComputerName
。你问题的“但是如果我运行:”部分不是很清楚-在这个上下文中$ComputerName有什么值?@FrankBoyne如果我运行。\GetBootTime.ps1-ComputerName它工作得很好似乎是当我运行两台计算机时它就不工作了。也希望能让事情变得更清楚