[英]howto compare an item of a powershell piped array
I need a powershell script which gets information from another powershell script. 我需要一个从另一个Powershell脚本获取信息的Powershell脚本。
It seems to me the it is an array what I get in my script, so I tried to compare one item or the whole array against a string. 在我看来,这是我在脚本中得到的一个数组,因此我尝试将一个项目或整个数组与字符串进行比较。
I will execute then this command on our Exchange cluster: 然后,我将在我们的Exchange群集上执行以下命令:
Get-ClusterResource |fl State|.\CheckDAG1.ps1
The first script is an inbuild Exchange script to get the state of a fileshare witness, the second script is mine and looks like this: 第一个脚本是用于获取文件共享见证人状态的内置Exchange脚本,第二个脚本是我的脚本,看起来像这样:
Param (
[parameter(ValueFromPipeline=$True)]
[string[]]$Status
)
echo $Input.count
echo $Input
if ($input[2] -contains "Online") {
echo "1"}
else {
echo "0"}
The output is this: 输出是这样的:
5 State : Online 0
So I can see that the array has 5 items, item 2 is the written line, but the result is 0
. 所以我可以看到该数组有5个项目,第2个项目是写入的行,但结果是
0
。
What can I do so that the result is 1
as I expect? 我应该怎么做才能使结果如预期的那样为
1
?
Get-ClusterResource
returns an object, that PowerShell will display as a table in the Console. Get-ClusterResource
返回一个对象,PowerShell将在控制台中将其显示为表。 The properties of the object are shown as the table headers. 对象的属性显示为表标题。
Example: 例:
(I'm using a single cluster named resource for the examples) (我在示例中使用的是一个名为资源的集群)
To use these properties you can select them: 要使用这些属性,可以选择它们:
Get-ClusterResource -Name "Cluster Disk 1" | Select-Object State
Which will return just the single property: 它将仅返回单个属性:
PS > State ----- Online
Then using the ExpandProperty
param will return just the value of the property: 然后,使用
ExpandProperty
参数将仅返回属性的值:
Get-ClusterResource -Name "Cluster Disk 1" | Select-Object -ExpandProperty State
PS > Online
So applying the above to your code: 因此,将以上内容应用于您的代码:
.\CheckDAG1.ps1 -Status (Get-ClusterResource -Name "Cluster Disk 1" | Select-Object -ExpandProperty State)
CheckDAG1.ps1: CheckDAG1.ps1:
Param (
[parameter(ValueFromPipeline=$True)]
[string]$Status
)
if ($Status -eq "Online") { echo "1" }
else { echo "0" }
Where do we start... 我们从哪里开始...
If you Get-Cluster 'foo' | Get-ClusterResource | fl State
如果
Get-Cluster 'foo' | Get-ClusterResource | fl State
Get-Cluster 'foo' | Get-ClusterResource | fl State
Get-Cluster 'foo' | Get-ClusterResource | fl State
, you'll get a list looking like Get-Cluster 'foo' | Get-ClusterResource | fl State
,您将获得一个看起来像的列表
Status : Online Status : Online Status : Offline
Try using the following... 尝试使用以下内容...
$(Get-Cluster 'foo' | get-clusterresource).State
This will give you a neat list of strings, but back to your original script. 这将为您提供整洁的字符串列表,但返回到原始脚本。 Are you 100% sure you're getting the value of the property and not a qualified name of the type like
Microsoft.PowerShell.Commands.Internal.Format.FormatEndData
? 您是否100%确定要获取属性的值,而不是像
Microsoft.PowerShell.Commands.Internal.Format.FormatEndData
这样的类型的合格名称? I'd double check. 我会仔细检查。
## Returns a single 1 or 0. 1 if there is at least 1 online resource, 0 if not
if ($status -contains 'Online') { 1 } else { 0 }
## Returns a 1 or 0 for each status in the array. 1 if the status is online, otherwise 0
$status | % { if ($_ -eq 'Online') { 1 } else { 0 } }
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.