powershell脚本,csv文件中单元格中的文本放置

时间:2017-09-13 15:07:12

标签: excel powershell csv

所以,从这里继续我的可爱旅程通过powershell: Loop for two variables

我有一个ps1,它为一堆事务和一堆节点运行循环,并将它们发送到csv文件。

$url = "https://someserver/trans="
$transactions = '1','2','3','4' #There are 4 transactions
$nodes = 'node1','node2','node3','node4','node5','node6' #There are 10 nodes

Remove-Item ATM.csv -Force

# So far so good
# Below is what I'd use as a function in bash. No sure what/how to do in PS:
#OUTPUT:
foreach($transaction in $transactions)
{
    foreach($node in $nodes)
    {

    "$transaction;$node" |out-file -Append ATM.csv
    curl -k -u user@pass $url$transaction$node | findstr "<value>" | out-file -Append ATM.csv
  }
}

在excel中打开文件,我最终得到A列下的输出:

   transaction1;node1 (in the first row, left-most cell)
   value1 (from the curl. It's actually a number and it sits in the row right under the first entry)
等等2,3等等,其余的。只填充最左侧的列(A列)。

我想要的是将值放在三列中的方法,这样csv将如下所示:

Column A    | Column B | Column C
transaction1| node1    | valueX
transaction2| node2    | valueY

等等。该脚本或其他脚本必须执行此操作,运行脚本的此作业的最终用户不会每天打开Excel并开始运行宏,他需要从脚本中准备好最终的csv。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

这样的事情会解决你的问题,唯一没有包含的就是从Invoke-WebRequest(curl)中选择值本身,因为这会根据返回的内容而改变。

foreach($transaction in $transactions)
{
    foreach($node in $nodes)
    {
    $value = Invoke-WebRequest -Uri $url$transaction$node -UseBasicParsing | Select-Object -Expand Content

    Add-Content -Path ATM.csv -Value "$transaction,$node,$value"
    }
}

答案 1 :(得分:1)

您目前正在用两个不同的行编写输出。一种解决方案可能是在Out-File中使用NoNewLine参数:

Started by user Jenkins Admin
Building remotely on ubuntu16.04-slave-one (build-maven-project) in workspace /var/jenkins/workspace/build-cc-restapi-dev
Cloning the remote Git repository
Cloning repository git@mylocalserver:CCININ/fieldwork-server.git
 > /usr/bin/git init /var/jenkins/workspace/build-cc-restapi-dev/ccinfieldworkserver # timeout=10
Fetching upstream changes from git@mylocalserver:CCININ/fieldwork-server.git
 > /usr/bin/git --version # timeout=10
using GIT_SSH to set credentials Using jenkins ID on Gitlab to fetch code from GITLAB
 > /usr/bin/git fetch --tags --progress git@mylocalserver:CCININ/fieldwork-server.git +refs/heads/*:refs/remotes/origin/*
 > /usr/bin/git config remote.origin.url git@mylocalserver:CCININ/fieldwork-server.git # timeout=10
 > /usr/bin/git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > /usr/bin/git config remote.origin.url git@mylocalserver:CCININ/fieldwork-server.git # timeout=10
Fetching upstream changes from git@mylocalserver:CCININ/fieldwork-server.git
using GIT_SSH to set credentials Using jenkins ID on Gitlab to fetch code from GITLAB
 > /usr/bin/git fetch --tags --progress git@mylocalserver:CCININ/fieldwork-server.git +refs/heads/*:refs/remotes/origin/*
 > /usr/bin/git rev-parse refs/remotes/origin/dev^{commit} # timeout=10
 > /usr/bin/git rev-parse refs/remotes/origin/origin/dev^{commit} # timeout=10
Checking out Revision 039b646001804b5fba636e514e85c9093a95ef5d (refs/remotes/origin/dev)
Commit message: "second change to see what happens"
 > /usr/bin/git config core.sparsecheckout # timeout=10
 > /usr/bin/git checkout -f 039b646001804b5fba636e514e85c9093a95ef5d
 > /usr/bin/git rev-list 039b646001804b5fba636e514e85c9093a95ef5d # timeout=10
[build-cc-restapi-dev] $ /opt/maven/bin/mvn clean package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.153 s
[INFO] Finished at: 2017-09-13T15:50:15+00:00
[INFO] Final Memory: 4M/10M
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/var/jenkins/workspace/build-cc-restapi-dev). Please verify you invoked Maven from the correct directory. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE

我个人会创建一个Powershell对象并在最后创建csv:

"$transaction;$node" |out-file -Append ATM.csv -nonewline
curl -k -u user@pass $url$transaction$node | findstr "<value>" | out-file -Append ATM.csv

}