如何处理创建文件并使用Powershell写入文件?

时间:2019-10-23 03:55:53

标签: powershell

我想创建一个文件并将内容写入该文件。 新文件将基于总的SWPO文件创建。我尝试过此脚本,可以创建新文件。但是,当我只放置1个$PO_Path文件时,它将创建2个新文件。实际上,这两个文件是相同的,但是其中一个文件没有$c。像这样

ID_ABC18XXR3CT123_.job
ID_ABC18XXR3CT123_AE.job

如果我将$PO_Path文件放到1以上,则效果很好。 任何人都可以帮助我。谢谢。

Function Create_OriJob
{
    $BID = "18XXR3CT123"
    $Job_Path = $Config_File.Automation_Config.Path.OriJob
    $PO_Path = $Config_File.Automation_Config.Path.POfiles

    if(Test-Path -Path "$PO_Path\*$BID*")
    {   
        Write-Output "SWPO File Found"
        # Start-Sleep -s 3
        $PO_Content = Get-Content -path "$PO_Path\*$BID*"
        $POfile = Get-ChildItem -Name "$PO_Path\*$BID*"
        $Get_CRM = $PO_Content | Where-Object {$_ -like "*;CRM*"}
        $CRM = $Get_CRM.Substring(5,2)
        $CRM = $CRM.split()
        $POCountry = Get-ChildItem -Name "$PO_Path"
        $GetCountry = $POCountry.Substring(15,3)
        $GetCountry = $GetCountry.split()

        For($i = 0; $i -lt $POfile.Length; $i++){ 
            try{
                $po = $POfile[$i]
                $c = $CRM[$i]
                $cc = $GetCountry[$i]
                New-Item -ItemType File -Path "$Job_Path\$JobType`_$Prefix$BID`_$c.job" -Force
                $Title =  $Config_File.Automation_Config.Out_Job.Title
                $Auto = $Config_File.Automation_Config.Out_Job.Auto
                $Proc = $Config_File.Automation_Config.Out_Job.Process
                $Auto =  $Config_File.Automation_Config.Out_Job.Auto
                $PO_Conf =  $Config_File.Automation_Config.Out_Job.PO
                $BIDINFO = $Config_File.Automation_Config.Out_Job.BIDINFO
                $BuildID =  $Config_File.Automation_Config.Out_Job.BID
                $PFX =  $Config_File.Automation_Config.Out_Job.PFX
                $CRM_Conf =  $Config_File.Automation_Config.Out_Job.CRM
                $CountryConf = $Config_File.Automation_Config.Out_Job.Country
                $Platform = $Config_File.Automation_Config.Out_Job.Platform
                $TSJobcreate = Get-Date

$Output_JOB = @"
<?xml version="1.0" encoding="UTF-8"?>
<$Title>
    <$Auto>
        <$Proc>$Auto</$Proc>
        <$PO_Conf>$po</$PO_Conf>
    </$Auto>
    <$BIDINFO>
        <$BuildID>$BID</$BuildID>
        <$PFX>$Prefix</$PFX>
        <$CRM_Conf>$c</$CRM_Conf>
        <$CountryConf>$cc</$CountryConf>
    </$BIDINFO>
    <$Platform>
        $All_SSID
    </$Platform>
    <Timestamp>
        <JobCreate>$TSJobcreate</JobCreate>
    </Timestamp>                
</$Title>
"@
                $Output_JOB | Out-File "$Job_Path\$JobType`_$Prefix$BID`_$c.job" -NoNewline
                Write-Host "Output"
            }
            catch{
                Write-Output "Something wrong!"
            }
        }  
        Write-Output "Continue to create operational job"
        Create_OpJob 
    }
    else{
        Write-Host "SWPO Not Found, Do Error checking file"
        Error_Monitoring

    }
    #Error_Monitoring  
}

0 个答案:

没有答案