我正在编写一个脚本来读取状态文件以记录设备的状态。该文件遵循这种规则': 第一行是" [ACTIVE_JOB]"其次是每行列出的乔布斯。 然后,有一行说" [COMPLETE_JOB]"然后是每行上列出的作业。在该列表的最后,它涉及到我不关心的其他一些东西,但是其他东西的第一行'总是以" ["开头。 所以这就是交易,当我把东西推到一个数组中,然后对阵列进行拆分时,如果没有活动作业,它似乎将第一个完成的作业推入活动作业数组。 我只是想创建一个活动作业和已完成作业的列表或数组,但是当没有活动作业时它会绊倒我。
无论如何,这里是我正在研究的snippit(您可以取消注释PATH变量集以在测试文件读取之间交替,如果这样可以更容易):
#$Path = 'C:\Scripts\NoJOBS.txt'
#$Path = 'C:\Scripts\NoActiveJobs.txt'
#$Path = 'C:\Scripts\ManyJobs.txt'
$StatusFile = Get-Content $path ## Reads the Status File as an array
#$StatusArray = @()
#$ActiveJobs = @()
#$CompleteJobs = @()
$StatusArray = New-Object System.Collections.ArrayList
$ActiveJobs = New-Object System.Collections.ArrayList
$CompleteJobs = New-Object System.Collections.ArrayList
foreach ($Line in $StatusFile) {
if ($Line -like "*PUBLISHER*") {Break}
#$StatusArray += $Line
$StatusArray.Add($Line)
}
$Seperator = "[ACTIVE_JOB]","[COMPLETE_JOB]"
$ActiveJobs,$CompleteJobs = $StatusArray.Split($Seperator,2,[System.StringSplitOptions]::RemoveEmptyEntries)
#$ActiveJobs = [System.Collections.ArrayList]$ActiveJobs
#$CompleteJobs = [System.Collections.ArrayList]$CompleteJobs
foreach ($Job in $ActiveJobs) {
#if($job.Contains("[")){ $ActiveJobs.Remove($Job) }
if($job.Contains("[")){ $ActiveJobs = $ActiveJobs | ? {$_ -ne $job} }
}
foreach ($Job in $CompleteJobs) {
#if($job.Contains("[")){ $CompleteJobs.Remove($Job) }
if($job.Contains("[")){ $CompleteJobs = $CompleteJobs | ? {$_ -ne $Job } }
}
如果没有作业,这就是文件的内容(另存为NoJOBS.txt):
[ACTIVE_JOB]
[COMPLETE_JOB]
[PUBLISHER1]
NAME=PP-50 1
STACKER1=62
STACKER2=9
STACKER3=0
如果有1个已完成的作业,但没有活动作业(另存为NoActiveJobs.txt),则说明如下:
[ACTIVE_JOB]
[COMPLETE_JOB]
JOB1=Frank_L_Smith-085149-08092017
[Frank_L_Smith-085149-08092017]
STATUS=99
最后,这里有1个活动作业和许多已完成的作业(Save as ManyJobs.txt):
[ACTIVE_JOB]
JOB1=April_Ztest-124856-31082017
[COMPLETE_JOB]
JOB1=Mel_R_Smith-141307-31082017
JOB2=Mel_R_Smithe-132029-31082017
JOB3=Melvin_R_Smithy-131037-31082017
JOB5=Will_R_Smith-123534-31082017
[Mel_R_Smith-141307-31082017]
PUBLISHER=PP-50 1
STATUS=4
PUBLICATION_NUMBER=1
COMPLETION_NUMBER=1
ERRORDISC_NUMBER=0
在Win10上使用PSVersion 5.1.14393.1066和.NET 4.7
答案 0 :(得分:0)
从TheScritpingGuy添加了这个功能,它就像一个魅力!谢谢@BenH !!
Function Get-IniContent {
<#
.Synopsis
Gets the content of an INI file
.Description
Gets the content of an INI file and returns it as a hashtable
.Notes
Author : Oliver Lipkau <oliver@lipkau.net>
Blog : http://oliver.lipkau.net/blog/
Source : https://github.com/lipkau/PsIni
http://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91
Version : 1.0 - 2010/03/12 - Initial release
1.1 - 2014/12/11 - Typo (Thx SLDR)
Typo (Thx Dave Stiff)
#Requires -Version 2.0
.Inputs
System.String
.Outputs
System.Collections.Hashtable
.Parameter FilePath
Specifies the path to the input file.
.Example
$FileContent = Get-IniContent "C:\myinifile.ini"
-----------
Description
Saves the content of the c:\myinifile.ini in a hashtable called $FileContent
.Example
$inifilepath | $FileContent = Get-IniContent
-----------
Description
Gets the content of the ini file passed through the pipe into a hashtable called $FileContent
.Example
C:\PS>$FileContent = Get-IniContent "c:\settings.ini"
C:\PS>$FileContent["Section"]["Key"]
-----------
Description
Returns the key "Key" of the section "Section" from the C:\settings.ini file
.Link
Out-IniFile
#>
[CmdletBinding()]
Param(
[ValidateNotNullOrEmpty()]
[ValidateScript({(Test-Path $_) -and ((Get-Item $_).Extension -eq ".txt")})]
[Parameter(ValueFromPipeline=$True,Mandatory=$True)]
[string]$FilePath
)
Begin
{Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"}
Process
{
Write-Verbose "$($MyInvocation.MyCommand.Name):: Processing file: $Filepath"
$ini = @{}
switch -regex -file $FilePath
{
"^\[(.+)\]$" # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
"^(;.*)$" # Comment
{
if (!($section))
{
$section = "No-Section"
$ini[$section] = @{}
}
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount
$ini[$section][$name] = $value
}
"(.+?)\s*=\s*(.*)" # Key
{
if (!($section))
{
$section = "No-Section"
$ini[$section] = @{}
}
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Processing file: $FilePath"
Return $ini
}
End
{Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}
}