在函数中调用字符串之前,如何将字符串添加到从CSV文件调用的变量中?

时间:2019-07-17 21:16:29

标签: powershell networking

我正在尝试ping IP列表,但是我需要针对RAC和KVM测试它们的变体。我想获取IP地址(在 Data failed to compile: ---------- Rendering SLS 'base:certbot.cert' failed: Jinja error: string indices must be integers, not unicode Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 392, in render_jinja_tmpl output = template.render(**decoded_context) File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 1008, in render return self.environment.handle_exception(exc_info, True) File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 780, in handle_exception reraise(exc_type, exc_value, tb) File "<template>", line 13, in top-level template code File "/usr/lib/python2.7/dist-packages/salt/modules/file.py", line 4469, in check_perms orig_comment = ret['comment'] TypeError: string indices must be integers, not unicode 变量中),并在对其执行ping操作之前将“ rac”附加到该地址。我还需要对“ arac”,“ raca”,“ kv”,“ akv”,“ kva”和“ kvm”执行ping操作,但是我只是在每次运行脚本后将其手动替换为脚本。 / p>

$ColumnHeader

我尝试创建新变量Param( [Parameter(Mandatory=$true, position=0)][string]$csvfile ) $ColumnHeader = "Name" #$string = "rac" #$subServer = "$($ColumnHeader)$($string)" Write-Host "Reading file" $csvfile $ipaddresses = Import-Csv $csvfile | Select-Object $ColumnHeader Write-Host "Started Pinging.." foreach ($ip in $ipaddresses) { if (Test-Connection $ip.($ColumnHeader) -Count 1 -Quiet) { Write-Host $ip.("Name") "Ping succeeded." -Foreground Green } else { Write-Host $ip.("Name") "Ping failed." -Foreground Red } } Write-Host "Pinging Completed." $string,并在第14行中使用它们代替$subServer,但是我得到“无法验证参数'ComputerName'的参数。 ”错误。

编辑:我将第7行从$ColumnHeader更改为$subServer = $ColumnHeader + $string,并且我可以在第14行上成功代替$subServer = "$($ColumnHeader)$($string)"呼叫$subServer,但是当我取消注释{{ 1}}在第6行,我再次收到上述“ ComputerName”错误。

0 个答案:

没有答案