我有一个现有的powershell脚本,其代码如下:
<Contact hideAll="false" hideCompanyName="false" hideEmailAddress="false">
</Contact>
我需要从XML文档中提取这些值,而不是硬编码变量。
<HIDEONLINECONTACT hideEmailAddress="true" hideCompanyName="false" />
当前输出
<Contact hideCompanyName="false" hideEmailAddress="false">
</Contact>
完整代码:
我需要新输出具有相同的格式。但如果值为true,则仅显示变量(节点?)。 我是PowerShell的新手,请原谅任何不正确的命名参考。
原始代码
[CmdletBinding(DefaultParameterSetName="calcDate")]
Param(
[Parameter(Mandatory=$true,Position=1)]
[string[]]$files,
#[string[]]$files = "input.xml",
[Parameter(Mandatory=$false,Position=2)]
[string]$channelID = "4813",
[Parameter(Mandatory=$false, Position=3)]
[string]$userNamePrefix = "SRC",
[Parameter(Mandatory=$false)]
#[string]$saveAs='.\test.xml',
[string]$saveAs='.\AC$(Get-Date -Date $date -Format "MMddyyyy").xml',
[Parameter(Mandatory=$false)]
[switch]$useScheduled=$false,
[Parameter(Mandatory=$false)]
[ValidateSet("Exclude", "HS","something","somethingelse")]
[string]$defaultJobLevel="Exclude",
[Parameter(Mandatory=$false,ParameterSetName='calcDate')]
[ValidateSet("InputFileName", "Yesterday", "Today", "Tomorrow")]
[string]$dateSource="Today",
[Parameter(Mandatory=$false,ParameterSetName='calcDate')]
[string]$inputFileNameSpec="(?'Month'[01]?[0-9])\.(?'Day'[0-3]?[0-9]).
(?'Year'(20)?[0-9]{2})\.xml",
[Parameter(Mandatory=$true,ParameterSetName='supplyDate')]
[DateTime]$suppliedDate
)
BEGIN
{
$editions = @{
"36" = "acdc";
"42" = "custcarefeb2011";
"34" = "div1jan2011";
"35" = "div2jan2011";
"44" = "edujul2012";
"43" = "energyjan2011";
"39" = "hcjan2011";
"38" = "hcrnjan2011";
"40" = "itdec2011";
"37" = "nursejan2011";
"47" = "salesmarketjun2013";
"45" = "socnwtaug2012";
"46" = "trucksep2012";
"41" = "veteransfeb2011";
"48" = "manjun2013";
"50" = "maxrecnov2014";
}
function IsValidBuyEdition([string]$editionList)
{
[boolean]($editionList -split "," |? {
$editions.ContainsKey($_.Trim())})
}
function GetJob($AD)
{
$jobID = "SR-$($AD.ORDERNUMBER)"
$encodedEmail = $AD.CONTACTEMAIL -replace "@", ""
$encodedEmail = $encodedEmail.SubString(0, [Math]::Min(8,
$encodedEmail.Length))
$userName = $userNamePrefix + $($AD.CUSTOMERNUMBER) + "_" +
$encodedEmail
$jobLevel = switch($defaultJobLevel)
{
"Exclude" { "" }
"HS" { "<JobLevel xxxxId=`"16`"/>"}
"UnderGrad" { "<JobLevel xxxxId=`"10`"/>"}
"EntryLevel" { "<JobLevel xxxxId=`"11`"/>"}
"Experienced" { "<JobLevel xxxxId=`"12`"/>"}
"Manager" { "<JobLevel xxxxId=`"13`"/>"}
"Executive" { "<JobLevel xxxxId=`"14`"/>"}
"Senior" { "<JobLevel xxxxId=`"15`"/>"}
}
$jobType = switch($AD.DOCVARS.JOBTYPE.ToLower())
{
"employee" { 1 }
"temporary" { 2 }
"contract" { 2 }
"temporary/contract" { 2 }
"intern" { 3 }
"seasonal" { 20 }
}
$jobStatuses = @{"fulltime" = 4 ; "parttime" = 5; "casual" = 5;
"perdiem" = 26}
$jobStatus = ""
$jobStatuses.Keys | % {
if (($AD.DOCVARS.JOBSTATUS -replace " ", "") -like "*$_*")
{
$jobStatus += "<JobStatus
monsterId=`"$($jobStatuses[$_])`"/>
"
}
}
if ($jobStatus -eq "")
{
$jobstatus = "<JobStatus xxxxxxId=`"4`"/>
"
Write-Warning -Message "Job $($AD.adnumber) defaulted JobStatus
to Full Time"
}
$industryTags = ""
$industry = $AD.DOCVARS.INDUSTRY -split '#'
$industries = @{
"All"=0;
"Agriculture/Forestry/Fishing"=1;
"Metals and Minerals"=2;
"Energy and Utilities"=3;
"Construction - Industrial Facilities and Infrastructure"=4;
"Aerospace and Defense"=6;
"Automotive and Parts Mfg"=7;
"Biotechnology/Pharmaceuticals"=8;
"Chemicals/Petro-Chemicals"=9;
"Consumer Packaged Goods Manufacturing"=10
}
if ($industry.Count -gt 1)
{
$industryTags = @"
<Industries>
<Industry>
<IndustryName xxxxxxId="$(($industry | SELECT -LAST
1).Trim())"/>
</Industry>
</Industries>
"@
}
else
{
$matchedIndustry = ""
$industries.Keys | % {
if ( $_ -ilike "*$($AD.DOCVARS.INDUSTRY)*") {
$matchedIndustry = [string]$industries[$_]}
}
if ($matchedIndustry -ne "")
{
$industryTags = @"
<Industries>
<Industry>
<IndustryName xxxxId="$matchedIndustry"/>
</Industry>
</Industries>
"@
}
}
if ($AD.DOCVARS.xxxxxJOBCATEGORY.description.Trim() -match
'^\d+$')
{
$jobOccupationCategory
=$AD.DOCVARS.xxxxxOBCATEGORY.description
}
else
{
$jobOccupationCategory =$AD.DOCVARS.xxxxxJOBCATEGORY.InnerText
}
$tempDate = $AD.SelectSingleNode("(./CHARGES/xxxx/STARTDATE)[1]")
$date = if ($tempDate -eq $null) { [DateTime]$AD.SCHEDULEDSTARTDATE
} else { [DateTime]$tempDate.InnerText }
$ActiveDate = Get-Date -Date $date -Format s
$tempDate = $AD.SelectSingleNode("(./CHARGES/MONSTR/ENDDATE)[1]")
$date = if ($tempDate -eq $null) { [DateTime]$AD.SCHEDULEENDDATE }
else { [DateTime]$tempDate.InnerText }
$ExpireDate = Get-Date -Date $date -Format s
$phone = $AD.DOCVARS.CUSTPHONENUMBER
if ($phone -eq $null -or $phone -eq "") { $phone = "1-111-111-1111"
}
@"
<Job jobRefCode="$jobID" jobAction="addOrUpdate" jobComplete="true"
xmlns="http://schemas.xxxx.com/Xxxxx"
xmlns:xsi="http://www.xxxxx.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.xxxxx.com/Xxxxxx
http://schemas.monster.com/Current/xsd/Monster.xsd">
<Sites>
$(($AD.BUYEDITIONS -split "," |? {
$editions.ContainsKey($_.Trim())} | %{ "<Site>$($editions[$_.Trim()])
</Site>"}))
</Sites>
<RecruiterReference>
<UserName>$userName</UserName>
</RecruiterReference>
<CompanyReference>
<CompanyXCode>x$($userName.ToLower())x</CompanyXCode>
<CompanyName><![CDATA[$($AD.DOCVARS.ONLINEFIRMNAME)]]>
</CompanyName>
</CompanyReference>
<Channel xxxxxxId="$channelID"/>
<JobInformation>
<JobTitle><![CDATA[$($AD.DOCVARS.JOBTITLE)]]></JobTitle>
$jobLevel
<JobType xxxxxxId="$jobType"/>
$jobStatus
<Contact hideAll="false" hideAddress="true"
hideStreetAddress="true" hideCity="false" hideState="false"
hidePostalCode="true" hideCountry="true" hideContactInfoField="true"
hideCompanyName="false" hideEmailAddress="false" hideFax="false"
hidePhone="true">
<StructuredName>
<GivenName>Given Name</GivenName>
<FamilyName><![CDATA[$($AD.CONTACTLASTNAME)]]>
</FamilyName>
</StructuredName>
<CompanyName><![CDATA[$($AD.DOCVARS.ONLINEFIRMNAME)]]>
</CompanyName>
<Address>
<StreetAddress><![CDATA[Street Address]]>
</StreetAddress>
<StreetAddress2><![CDATA[Street Address 2]]>
</StreetAddress2>
<City>$($AD.DOCVARS.JOBCITY)</City>
<State>CA</State>
<CountryCode>USA</CountryCode>
<PostalCode>$($AD.DOCVARS.JOBZIPCODE)</PostalCode>
</Address>
<Phones>
<Phone phoneType="work">$phone</Phone>
</Phones>
<E-mail>$($AD.DOCVARS.RESPONSEEMAIL)</E-mail>
</Contact>
<PhysicalAddress>
<City>$($AD.DOCVARS.JOBCITY)</City>
<State>CA</State>
<CountryCode>US</CountryCode>
<PostalCode>$($AD.DOCVARS.JOBZIPCODE)</PostalCode>
</PhysicalAddress>
<DisableApplyOnline>false</DisableApplyOnline>
<HideCompanyInfo>false</HideCompanyInfo>
<JobBody>$($AD.ADTEXT.InnerXml)</JobBody>
</JobInformation>
<JobPostings>
<JobPosting jobPostingAction="add">
<Location>
<City>$($AD.DOCVARS.JOBCITY)</City>
<State>CA</State>
<CountryCode>US</CountryCode>
<PostalCode>$($AD.DOCVARS.JOBZIPCODE)</PostalCode>
<Continent>NA</Continent>
</Location>
<JobCategory xxxxxId="$jobOccupationCategory"/>
<JobOccupations>
<JobOccupation parentCategoryId="$jobOccupationCategory"
monsterId="$(($AD.DOCVARS.OCCUPATIONID -split "#", 2| SELECT -last
1).Trim())"/>
</JobOccupations>
<BoardName monsterId="1"/>
<JobPostingDates>
<JobActiveDate>$ActiveDate</JobActiveDate>
<JobExpireDate>$ExpireDate</JobExpireDate>
</JobPostingDates>$industryTags
</JobPosting>
</JobPostings>
</Job>
"@
}
function GetJobs($jobs)
{
@"
<nis:Jobs xmlns:nis="http://schemas.monster.com/Monster/NIS"
xmlns="http://schemas.xxxx.com/Xxxx"
xmlns:xsi="http://www.xxxx.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.xxxx.com/XXXxxxx
http://schemas.xxxxxx.com/Current/XSD/Job.xsd
http://schemas.xxxxxxx.com/Monster/NIS
http://schemas.xxxxx.com/Current/Extensions/NIS/XSD/NISJob.xsd">
$($jobs | % { "
<nis:NISJob>
"
([xml]$_).outerxml
"
<nis:AdditionalProducts>
<nis:Diversity desired='true'/>
<nis:Autorefresh desired='false' />
<nis:CareerAdNetwork desired='false' />
<nis:Bolding desired='false' />
</nis:AdditionalProducts>
</nis:NISJob>"
})
</nis:Jobs>
"@
}
}
PROCESS
{}
END
{
$toProcess = resolve-path $files
$toProcess |% {
$currentFile = resolve-path $_
$currentFileName = split-path -Path $currentFile -Leaf
$atsWebPagDoc = [xml]((Get-Content $currentFile).TrimEnd(0x1A))
$jobs = @()
$atsWebPagDoc.ATSWEBPAG.AD |
WHERE { $_.CLASS -EQ 2255 -and [bool]
(IsValidBuyEdition($_.BUYEDITIONS)) -EQ $true } |
#SELECT -FIRST 1 -skip 1 |
% {
$jobs += [xml](GetJob($_))
}
[xml]$jobsXML = GetJobs($jobs)
if ($suppliedDate -ne $null)
{
$date = $suppliedDate
}
else
{
switch($dateSource.ToLower())
{
"InputFileName"
{
if ($currentFileName -match $inputFileNameSpec)
{
$date = Get-Date -Month $Matches["Month"] -Day
$Matches["Day"] -Year $Matches["Year"]
} else
{
$date = Get-Date
}
}
"Yesterday" {$date = (Get-Date).AddDays(-1)}
"Today" {$date = Get-Date}
"Tomorrow" {$date = (Get-Date).AddDays(1)}
}
}
$saveFile = Invoke-expression "`"$saveAs`""
#Out-File -FilePath $saveFile -InputObject $jobsXML.OuterXml -Force
-Encoding ascii
$jobsXML.Save($saveFile)
#$jobs.outerxml
#Invoke-Item $saveFile
}
}
完整的原始XML:
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <!-- Transformed by XXX Webpag with XXXXX.XSL v1.12
-->
- <XXXWEBPAG SOURCE="xxxx" CREATIONDATE="02/15/2018" CREATIONTIME="17:09" PUBLICATIONDATE="02/16/2018">
- <AD adnumber="1111111-11111-0">
<DOCNAME>1111-1111-0</DOCNAME>
<CLASS>2255</CLASS>
<ORDERNUMBER>222222</ORDERNUMBER>
<CUSTOMERNUMBER>3333</CUSTOMERNUMBER>
<FIRMNAME>SOME NAME</FIRMNAME>
<CONTACTFIRSTNAME />
<CONTACTLASTNAME>ATTN: SOMETHING</CONTACTLASTNAME>
<CUSTOMERURL />
<CONTACTEMAIL>someboday@something.org</CONTACTEMAIL>
<LOCATIONFAX />
<CUSTOMERBILLADDRESS1>Some Address</CUSTOMERBILLADDRESS1>
<CUSTOMERCITY>Sometown</CUSTOMERCITY>
<CUSTOMERSTATE>SomeState</CUSTOMERSTATE>
<CUSTOMERZIP>11111</CUSTOMERZIP>
<CLASSDESCRIPTION>Some Description</CLASSDESCRIPTION>
<EDITIONID>34</EDITIONID>
<STARTDATE>02/16/2018</STARTDATE>
<SCHEDULEDSTARTDATE>01/21/2018</SCHEDULEDSTARTDATE>
<ENDDATE>02/17/2018</ENDDATE>
<SCHEDULEENDDATE>02/19/2018</SCHEDULEENDDATE>
<SORTKEY>KEYOFSOMESORT</SORTKEY>
<BUYEDITIONS>11,34,45</BUYEDITIONS>
- <CHARGES>
<CHARGE id="BLD2">BLD2</CHARGE>
<CHARGE id="BRD1">BRD1</CHARGE>
- <!-- Original charge was EMB1
-->
- <XXXXX id="XXXXX" rundays="30">
<STARTDATE>01/21/2018</STARTDATE>
<ENDDATE>02/20/2018</ENDDATE>
</XXXXX>
<CAJ id="CAJ">1</CAJ>
<CHARGE id="MBOLD">MBOLD</CHARGE>
<CHARGE id="LOGO">LOGO</CHARGE>
</CHARGES>
<BILLSOURCE>Liner</BILLSOURCE>
<BUYTYPE>0217</BUYTYPE>
<PUBCODE>Xxxx</PUBCODE>
- <WEBEDITIONS>
<WEBEDITION>11</WEBEDITION>
</WEBEDITIONS>
- <DOCVARS>
<AGENCY>0</AGENCY>
<CUSTPHONENUMBER>707-111-1111</CUSTPHONENUMBER>
<HIDEONLINECONTACT hideEmailAddress="true" hideCompanyName="true" />
<INDUSTRY>XXXX#11</INDUSTRY>
<JOBCITY>SomeCity</JOBCITY>
<JOBSTATUS>Relief</JOBSTATUS>
<JOBTITLE>Senior Assitant</JOBTITLE>
<JOBTYPE>Employee</JOBTYPE>
<JOBZIPCODE>111111</JOBZIPCODE>
<xxxxxxJOBCATEGORY description="Other">11</xxxxxxJOBCATEGORY>
<OCCUPATIONID>Other#11892</OCCUPATIONID>
<ONLINEFIRMNAME>FirmName</ONLINEFIRMNAME>
<RESPONSEEMAIL>someone@somthing.com</RESPONSEEMAIL>
<RESPONSEPHONE>111-111-1111</RESPONSEPHONE>
<SALARY>$00</SALARY>
</DOCVARS>
- <IMAGES>
<IMAGE sourcepath="\\xxxxx\xxxx\xxxx\xxxx\1111\111 1 column color-8.gif" />
</IMAGES>
- <ADTEXT>
- <![CDATA[
Extra-help
$11.11 - $22.22/hour
Apply Now
To view position details,
benefits, & apply online, visit www.something.com
or call HR, 1111-1111-1111. EOE
]]>
</ADTEXT>
</AD>