如何将参数传递给PowerShell脚本以匹配csv文件中的列

时间:2018-05-29 17:53:26

标签: powershell

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
from itertools import permutations

fig = plt.figure()
ax = fig.gca(projection='3d')

# data to plot cube edges
a = np.linspace(0,1,10)
b = np.zeros_like(a)
c = np.zeros_like(a)
c.fill(1)

# draw wire cube to help visualization
for i in permutations((a,b,b),3):  

    ax.plot(i[0],i[1],i[2], 'k--', linewidth=2)

for i in permutations((a,c,c),3):  

    ax.plot(i[0],i[1],i[2], 'k--', linewidth=2)

for i in permutations((a,c,b),3):  

    ax.plot(i[0],i[1],i[2], 'k--', linewidth=2)

# Making the grid and data for contours
X,Y = np.mgrid[0:1:10j, 0:1:10j]
Z = np.random.randint(5, size=X.shape)

# Plotting contours, here the trick is to swap (X,Y,Z) out to prevent 
# projection
ax.contourf(X,Y,Z, zdir='z', offset=1, cmap=cm.coolwarm)
ax.contourf(Y,Z,X, zdir='y', offset=0, cmap=cm.coolwarm)
ax.contourf(Z,Y,X, zdir='x', offset=0, cmap=cm.coolwarm)
ax.contourf(Y,Z,X, zdir='y', offset=1, cmap=cm.coolwarm)
ax.contourf(Z,Y,X, zdir='x', offset=1, cmap=cm.coolwarm)

# Setting axis limits and labels
ax.set_xlabel('X')
ax.set_xlim(0, 1)
ax.set_ylabel('Y')
ax.set_ylim(0, 1)
ax.set_zlabel('Z')
ax.set_zlim(0, 1)
ax.set_axis_off()

# get a proper view 
ax.view_init(elev=-22., azim=-135)
plt.show()

CSV文件如下所示

$current_pc     = "$env:computername"
$filePath      = "c:\users\Ara\Desktop\parameters.csv"
$machineParams = Import-CSV $filePath
$info          = if($machineParams){$machineParams | where {$_.branch -eq $($current_pc.Substring($current_pc.length - 3,3))}}
             else{write-output "CSV not loaded"}
$info

Start-Process -FilePath "c:\users\Ara\Desktop\setup.msi"  -ArgumentList “/qf”, "DATABASE_ID=$($info.param1)" , "ODBC_DATABASE_NAME=$($info.ODBC_param2)" ,  "ODBC_ENGINE_NAME=$($info.param3)" -wait

我的问题是我应该在脚本中包含哪些逻辑? 要运行branch param1 param2 param3 ------ ------ ------ ------ 188 apr101 1.0.0.1 0 101 apr104 1.0.0.1 1 ,它将获取csv文件中的188旁边的值

2 个答案:

答案 0 :(得分:0)

我只读取$current_pc

的参数
## Q:\Test\2018\05\29\SO_50590213.ps1
$current_pc     = "$env:computername"
$CurrBranch     = $current_pc.Substring($current_pc.length-3,3)
$filePath       = "c:\users\Ara\Desktop\parameters.csv"
$machineParams  = Import-CSV $filePath | where {$_.branch -eq $CurrBranch}
$info = if($machineParams){
    $machineParams
} else {
    write-output "no params for $CurrBranch"
}
$info

Start-Process -FilePath "c:\users\Ara\Desktop\setup.msi"  `
  -ArgumentList “/qf”, "DATABASE_ID=$($info.param1)", `
  "ODBC_DATABASE_NAME=$($info.ODBC_param2)", `
  "ODBC_ENGINE_NAME=$($info.param3)" -wait

示例输出(此pcs名称以610结尾)

branch param1 param2  param3
------ ------ ------  ------
610    aprxxx 1.0.0.1 1

或文件中没有匹配的分支

no params for 610

答案 1 :(得分:0)

您可以使用param块并将默认ID定义为计算机名子字符串。

param (
    $ID = (@($ENV:computername) | ForEach-Object {$_.Substring($_.length - 3,3)})
)
$filePath      = "c:\users\Ara\Desktop\parameters.csv"
$machineParams = Import-CSV $filePath | Where-Object {$_.branch -eq $ID}
$info          = if ($machineParams) {
                    $machineParams
                } else {
                    # Use a throw to error out when invalid csv 
                    throw "no params for $ID"
                }
$info

Start-Process -FilePath "c:\users\Ara\Desktop\setup.msi"  -ArgumentList “/qf”, "DATABASE_ID=$($info.param1)" , "ODBC_DATABASE_NAME=$($info.ODBC_param2)" ,  "ODBC_ENGINE_NAME=$($info.param3)" -wait

然后你可以调用你的脚本,当你定义ID参数时,它将覆盖默认方案,并且在不使用时将使用computername方案。

# Use computername scheme
install.ps1
# Use defined ID
install.ps1 -ID 188

编辑:使用@LotPings建议进行更新,以便先前过滤