首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Powershell脚本不将数据输出到ISE外部的文件

Powershell脚本不将数据输出到ISE外部的文件
EN

Stack Overflow用户
提问于 2018-06-07 21:48:32
回答 1查看 155关注 0票数 0

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

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

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

下面是脚本(很难让这个代码块笑出‘}’的意思):

    # 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中工作,这很好,但我需要为我制作它的人提供批处理文件方法。任何帮助,建议等,我们将不胜感激!谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-08 04:18:22

我所做的更改现在起作用了,用两个换行符分隔:

# 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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50742930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档