Powershell - 根据CSV文件更改变量

时间:2013-12-09 18:08:55

标签: variables powershell csv wmi

我正在尝试使用CSV文件进行PowerShell自动计算机命名。我们的计算机名称带有通用型号,告诉我实际的计算机类型。例如,Model#2537CU1是Lenovo T410。我想拉出型号并将变量更改为T410。

我已经可以通过对计算机系统类的get-wmiobject调用来获取模型编号,但是要使用“类型”数据创建变量,就像T410我必须使用一堆If和elseif语句。这是由于我们支持的数量或型号/类型。我的目标是编辑CSV而不是脚本本身。我只是希望相应的模型为机器的“类​​型”获得正确的变量。

所以这是我的头到目前为止,我知道我已经离开了:

$model = Get-WmiObject -Class Win32_ComputerSystem | select -ExpandProperty Model
$modelCSV = "0"
$typeCSV = "0"
Import-Csv -Path "C:\TechDiv\ComputerModel_Type.csv" |
ForEach-Object {
    $modelCSV += $_.Model
    $typeCSV += $_.Type
    }
if ($modelCSV -like $model)
   {
        $model = $typeCSV
    }

我的CSV看起来像这样:

Model, Type
0401B7U, A70Z
0401R6U, A70Z
1165A3U, A70Z
25521H8, 0E31
2552Ck1, 0E31
7360, 0M58
7483, 0M58
7630, 0M58

最终,我的计算机命名脚本将连接模型和序列号,以便于收集和更新。模型/序列名称的示例(0的填充左侧序列填充10个空格):T410000R84D06Z

2 个答案:

答案 0 :(得分:1)

我将如何做到这一点:

$model = Get-WmiObject -Class Win32_ComputerSystem | select -ExpandProperty Model
$modelCSV = "0"
$typeCSV = "0"
$modelList = Import-Csv -Path "C:\TechDiv\ComputerModel_Type.csv" |
$modelName = $modelList | Where-Object{$_.Model -eq $model}

答案 1 :(得分:0)

我将该CSV加载到哈希表中:

$ModelHash = @{}
Import-Csv -Path "C:\TechDiv\ComputerModel_Type.csv" |
ForEach-Object { $ModelHash[$_.Model = $_.Type }

$model = Get-WmiObject -Class Win32_ComputerSystem | select -ExpandProperty Model

$Type = $ModelHash[$Model]