Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在powershell脚本中选择阵列类型数据?_Powershell - Fatal编程技术网

如何在powershell脚本中选择阵列类型数据?

如何在powershell脚本中选择阵列类型数据?,powershell,Powershell,我不是powershell开发者。因此,我需要周围专家的帮助。 我有一个脚本,它完成了我应该做的一切,但是现在我无法实现这个扩展 目前,我们将文本文件中的以下行读取到变量VmList中: LineA LineB LineC LineD 移交的文件如下: $Lines = Get-Content $IniValue LineA,1,1,1,1,1,1,1 LineB,1,0,1,1,1,1,1 LineC,1,1,1,1,1,1,1 LineD,1,0,1,1,0,1,1 之后,他们又排起了

我不是powershell开发者。因此,我需要周围专家的帮助。 我有一个脚本,它完成了我应该做的一切,但是现在我无法实现这个扩展

目前,我们将文本文件中的以下行读取到变量VmList中:

LineA
LineB
LineC
LineD
移交的文件如下:

$Lines = Get-Content $IniValue
LineA,1,1,1,1,1,1,1
LineB,1,0,1,1,1,1,1
LineC,1,1,1,1,1,1,1
LineD,1,0,1,1,0,1,1
之后,他们又排起了队

现在,我们希望根据工作日从变量$lines中删除这些行

为此,我们将行更改如下:

$Lines = Get-Content $IniValue
LineA,1,1,1,1,1,1,1
LineB,1,0,1,1,1,1,1
LineC,1,1,1,1,1,1,1
LineD,1,0,1,1,0,1,1
,1,1,1,1,1,1,1,1
分别代表一个工作日。第一个数字1代表星期一,第二个代表星期二

In line A, Tuesday is therefore set to 1
So in line B the Tuesday is set to 0
So in line C the Tuesday is set to 1
So in line D the Tuesday is set to 0
因此,在周一、周三、周四、周六、周日,变量$line中的行A、B、C、D应该保留

因此,对于星期二的执行,应该从变量$line中删除lineB和lineD

因此,对于星期五执行,只需从变量$line中删除lineD

清理行后,将从所有行中删除附加的链1,1,1,1,1,1,1

非常感谢你的帮助

试试这个:

Add-Type -AssemblyName System.Collections

# Generic list => better than array
[System.Collections.Generic.List[string]]$dayList = @()

# input file
$csvFile    = 'C:\test\weekdays.csv' 

# read file to generic list
$dayList    = [System.IO.File]::ReadLines( $csvFile )

# get current weekday as a number
$currentDay = (Get-Date).DayOfWeek.value__

# remove all records with 0 on current day
[void]$daylist.RemoveAll( {param($s) @($s -split ',')[$currentDay-1] -eq '0'}  )

# show list
$daylist

我使用here字符串进行演示。
在现实生活中,您可能希望使用
$Lines=Get Content$IniValue

$lines = @"
LineA,1,1,1,1,1,1,1
LineB,1,0,1,1,1,1,1
LineC,1,1,1,1,1,1,1
LineD,1,0,1,1,0,1,1
"@ -split '\r?\n'

# get today's DayOfWeek
$today = (Get-Date).DayOfWeek

$lines | ForEach-Object {
    $parts = $_ -split ','
    # if Sunday, the index should be 7, otherwise the integer number of todays DayOfWeek
    $index = if ($today -eq 'Sunday') { 7 } else { [int]$today }
    if ([int]$parts[$index]) { $parts[0] }
}
星期二的输出:


if([int]$parts[$index])
中使用
[int]
的原因是,从行中拆分的数字可能看起来像数字,但实际上是字符串
“0”
“1”
。如果不强制转换为整数,if将检查
if(“0”)
if(“1”)
,因为这两个字符串都不是空字符串,所以结果将始终为真。

我会这样做。将输入文件视为csv,或使其成为csv。今天是星期二

$lines = import-csv $inivalue -header vm,monday,tuesday,wednesday,thursday,friday,saturday,sunday
$day = (get-date).DayOfWeek
$lines | where { $_.$day -eq 1 } | ft


vm    monday tuesday wednesday thursday friday saturday sunday
--    ------ ------- --------- -------- ------ -------- ------
LineA 1      1       1         1        1      1        1
LineC 1      1       1         1        1      1        1
然后获取虚拟机的列表:

$lines | where { $_.$day -eq 1 } | foreach vm


LineA
LineC
我试着做“where day”,但它只适用于int:

$lines | where { [int]$_.$day } | ft


vm    monday tuesday wednesday thursday friday saturday sunday
--    ------ ------- --------- -------- ------ -------- ------
LineA 1      1       1         1        1      1        1
LineC 1      1       1         1        1      1        1

如果需要行名称和周数据(所有列),可以执行以下操作:

%u
是星期一为1的星期几编号的格式说明符。因为星期天是0,你必须处理那个特殊情况。在拆分中获取索引
[-1]
,将获取拆分数组中的最后一项,即星期日(
0-1


如果您只想返回第一列数据,那么这是最简单的方法。

使用鼠标选中文本的编辑菜单中的
{}
工具,将格式设置为
code/data/requiredOutput/ExactErrMsgs
。祝你好运。你能用一个示例文本文件提供完整的代码吗?你应该删除标记“shell”,因为它与UNIX shell混淆。@f6a4很抱歉,我没有任何代码,但对于这部分。。。我的意思是除了上面描述的:@snwfdhmp好的,对不起。。。完成。。。