Powershell脚本不将数据输出到ISE之外的文件中

时间:2018-06-07 13:48:32

标签: windows powershell batch-file

我知道其他人也有类似的问题,但没有一个是这样的。我制作了一个ps1脚本,将XML对象的文件转换为表示某些数据的行的CSV文件。昨晚我能够运行批处理文件并转换文件,但是当我从批处理运行时它今天早上保存了一个空的CSV文件,但是当我在Powershell ISE中运行它时它工作正常。

我从具有-STA模式的批处理文件中运行它以使其能够打开对话框窗口:

powershell -sta C:\Users\*******\Downloads\JiraXMLtoCSV.ps1

这是脚本(很难让这个代码块lol原谅'}':

    # This function will open a file-picker for the user to select their Jira XML Export
    Function Get-JiraXMLFile(){ 
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null;
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog;
$OpenFileDialog.initialDirectory = Get-Location;
$OpenFileDialog.filter = "XML files (*.xml)|*.xml";
$OpenFileDialog.ShowDialog() | Out-Null;
$OpenFileDialog.filename;
$OpenFileDialog.ShowHelp = $true;
}

    # This function will open the file save dialong to allow the user to choose location and name of the converted XML-to-CSV file
    Function Get-SaveFile(){ 
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null;

$SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog;
$SaveFileDialog.initialDirectory = Get-Location;
$SaveFileDialog.filter = "CSV files (*.csv)|*.csv";
$SaveFileDialog.ShowDialog() | Out-Null;
$SaveFileDialog.filename;
$SaveFileDialog.ShowHelp = $true;
} 




    # Invoke the file-picker function and obtain input file 
    $inputFile = Get-JiraXMLFile;

    #initialize list for items that will be extracted from XML Input File
    $list = @(); 

    # Loop through all the items in Jira XML export file
    foreach ( $item in $XMLFile.rss.channel.item ) {

# Create a new hash object
$issue = @{}; 

# Gather wanted attributes
$issue.Key = $item.key.InnerXML;
$issue.StatusColor = $item.statusCategory.colorName;
$issue.Status = $item.status.InnerXML;

# Check for comments 
if ( $item.comments ) {
    # Record the comments with column name/header format as follows: comment #0 | comment #2|...
    # Change this value to 1 if you want to see it start at comment #1 instead of comment #0
    $incrementalCounter = 0;
    # Loop through all comments on the issue
    foreach ( $comment in $item.comments.comment ) {
        $issue.("comment #"+$incrementalCounter) = $comment.InnerXML;
        $incrementalCounter += 1;
    }

}
#Create an object to be added to the list
$object = New-Object –TypeName PSObject –Prop $issue;
Write-Output $object;

# add this issue to the list to convert/export to CSV
$list += $object;

}

# Open File Saving window to choose file name and location for the new
$OutputFile = Get-SaveFile;
$list | Export-Csv -Path ($OutputFile) -NoTypeInformation;

如果你想要一些示例XML来帮助我了解我做错了什么:

    <rss version="0.92">
    <channel>
    <title>XML Export</title>
    <link>...</link>
    <description>An XML representation of a search request</description>
    <language>en-us</language>
    <issue start="0" end="7" total="7"/>
    <build-info>...</build-info>
    <item>
    <title>[AJT-46] another new story</title>
    <project id="1652" key="AJT">Advanced Training</project>
    <description/>
    <environment/>
    <key id="220774">AJT-46</key>     
    <status id="16615" iconUrl="https://website.com/" description="Desc text">To Do</status>
    <statusCategory id="2" key="new" colorName="gray"/>
    <labels></labels>
    <created>Tue, 5 Jun 2018 11:25:38 -0400</created>
    <updated>Tue, 5 Jun 2018 11:29:00 -0400</updated>
    <due/>
    </item>
    </channel>
    </rss>

它昨晚正在工作,现在当我今天早上出现时没有工作,所以没有改变,我知道,我也没有重启。它仍然适用于Powershell ISE,这很好,但我需要批处理文件方法,我正在为它做的人。任何帮助,建议等都表示赞赏!感谢

1 个答案:

答案 0 :(得分:0)

我所做的更改现在有效,双重换行分开:

# Invoke the file-picker function and obtain input file 
[Xml]$inputFile = Get-JiraXMLFile;


# Grab all the items we exported, ignore the header info
if ( $inputFile ) {
    #$XmlComments = Select-Xml "//comment()" -Xml $inputFile;
    #$inputFile.RemoveChild($XmlComments);
    $items = Select-Xml "//rss/channel/item" -Xml $inputFile;
}


# Iterate over items and grab important info to be put into CSV format
foreach ( $item in $items ){
# Create a new hash object
$issue = @{}; 


# Gather wanted attributes
if( $item.Node.key){
    $issue.Key = $item.Node.key.InnerXML;
}