微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 TXT 或 CSV 获取主机名,存储在变量中

如何解决从 TXT 或 CSV 获取主机名,存储在变量中

这是我的第一个 Powershell 命令,已经一个星期了,我不知道是什么问题。我想做的是以下内容

  1. 选择一个包含主机名列表的 TXT 或 CSV 文件
  2. 存储在变量中
  3. 稍后我将为每个系统运行一个循环命令

我的第一个问题是由于某种原因我的 Get-Content 命令似乎不起作用,


# Remote run the install for each system

foreach ($System in $SystemList) {
    if (test-Connection -Cn $System -quiet) { 
        copy-item $SetupFolder -Destination \\$System\$Dest -recurse -Force

        if (Test-Path - Path $) {
            Invoke-Command -ComputerName $System -ScriptBlock {powershell.exe $Path /S} -credential $Credentials

            Write-Host -ForegroundColor Green "Installation Successful on $System"
        }

    } else {
    Write-Host -ForegroundColor Red "$System is not online,Install Failed"
    }

}

最终,我将运行一个调用 $SystemList 变量的命令

arr1 = [
  {
   "id":4390,"name":"panadol"
 },{
   "id":4392,"name":"aspirin"
 },{
   "id":1390,"name":"vantage"
 },{
   "id":2390,"name":"cream"
 },]

arr2 = [
 {
   "id":3390,"name":"morphine"
 },{
   "id":3231,"name":"flagyl"
 },]

console.log([...arr2,...arr1])

解决方法

$FileBrowser.ShowDialog() 返回显示对话框的结果 - 而不是实际选择的文件路径。

为此,您需要$FileBrowser.FileName

$dialogResult = $FileBrowser.ShowDialog()

if($dialogResult -eq 'OK'){
  # user selected a file
  $GetList = $FileBrowser.FileName
}
else{
  # user canceled the dialog,either `return`,throw an error,or assign a static value to `$GetList`,like this
  $GetList = "C:\default\path\to\hostnames.txt"
}

# Now we can safely proceed with `Get-Content`:
$SystemList = Get-Content -LiteralPath $GetList

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。