首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导入CSV并使用Get-Hotfix导出CSV。

导入CSV并使用Get-Hotfix导出CSV。
EN

Stack Overflow用户
提问于 2020-01-06 17:33:56
回答 1查看 310关注 0票数 0

因此,我在试图从许多计算机获取修补程序(列在列"IP地址“下的csv文件中)并将结果导出到CSV时遇到了一些困难。它们都需要一个本地计算机帐户才能登录(在列"CPU名称“下的同一个CSV中)。我并不在乎整个过程是一个csv,还是每个结果都是csv。以下是目前为止的代码:

代码语言:javascript
复制
$ipaddress = [What do I put here?]
$cpuname = [What do I put here?]
$OutputFile = 'MyFolder\Computer.csv'
$Username = '$cpuname\MyUsername' [Is this ok?]
$Password = 'MyPassword'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force

$SecureString = $pass
$MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$SecureString

$Computers = Import-CSV "C:\MyFolder\Computers.csv"

ForEach ($ipaddress in $Computers) {
    }

  try  

    { 
Get-HotFix -Credential $MySecureCreds -ipaddress $IPAddress | Select-Object PSComputerName,HotFixID,Description,InstalledBy,InstalledOn | export-csv $OutputFile
    } 

catch

    { 
Write-Warning "System Not reachable:$ipaddress"
    }

我接近了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-07 00:47:54

IP地址和计算机名来自CSV,因此您不需要静态地定义它们。

关于用户名,有几件事:

  • 您需要使用双引号来展开。
  • 还需要读取'CPU名称‘属性,一种方法是在双引号的大表达式(如"$($variable.property)more text" )中使用子表达式。或者您可以连接字符串$variable.property + 'more text'
  • Since CPU Name在列名中有一个空格,需要用引号括起来。
  • ,因为用户名来自来自CSV的计算机名,定义用户名变量需要在foreach循环中。

这个脚本中的安全实践是有问题的,但超出了问题的范围。例如,在脚本中保存密码,特别是具有管理权限的本地帐户,这些帐户都具有相同的密码.

代码语言:javascript
复制
$InputFile = 'C:\MyFolder\Input.csv'
$OutputFile = 'C:\MyFolder\Output.csv'
$Password = Read-Host | ConvertTo-SecureString -AsPlainText -Force
$Computers = Import-CSV $InputFile

$HotfixOutput = foreach ($Computer in $Computers) {

    $Username = "$($Computer.'CPU Name')\MyUsername"
    $MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$Password

    try { 
        Get-HotFix -Credential $MySecureCreds -ipaddress $Computer.'IP Address' | Select-Object PSComputerName, HotFixID, Description, InstalledBy, InstalledOn
    } 

    catch { 
        Write-Warning "System Not reachable: $($Computer.'IP Address')"
    }

}

$HotfixOutput | Export-Csv $OutputFile -NoTypeInformation

Remove-Variable Password
Remove-Variable MySecureCreds
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59616507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档