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

huangapple go评论72阅读模式
英文:

Importing CSV and using Get-Hotfix to export to CSV

问题

以下是代码的翻译部分:

$ipaddress = [这里我要放什么?]
$cpuname = [这里我要放什么?]
$OutputFile = 'MyFolder\Computer.csv'
$Username = '$cpuname\MyUsername' [这样可以吗?]
$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"
}

希望这能帮助你。如果有任何疑问,请随时提出。

英文:

So, I'm having some difficulty with some code I'm trying to use to get hotfixes from a lot of computers (which are listed in a CSV file under the column "IP Address") and export that result to a csv. They each require a local computer account to log in (in the same CSV under the column "CPU Name"). I don't really care if it's one csv for the whole thing or a csv for each result. Here's the code so far:

$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"
    }

Am I close?

答案1

得分: 1

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

关于用户名,有几件事情:

  • 您需要使用双引号来扩展用户名。
  • 您还需要读取“CPU名称”属性的方式之一是在双引号内的更大表达式中使用子表达式,例如"$($variable.property)more text"。或者您可以连接字符串$variable.property + 'more text'
  • 由于“CPU名称”在列名中有空格,因此您需要将其括在引号中。
  • 由于用户名来自计算机名称,而计算机名称来自CSV文件,因此定义用户名变量必须在“foreach”循环中进行。

此脚本中的安全实践有问题,但超出了问题的范围。例如,在脚本中保存密码,尤其是具有对许多具有相同密码的网络化机器的管理特权的本地帐户...

$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

请注意,上述内容仅是脚本的一部分,已根据您的请求进行了翻译。如果需要进一步的说明或帮助,请告诉我。

英文:

The IP Address and the Computer Name are coming from the CSV, so you don't need to statically define them.

Regarding the username, a couple things:

  • You need to use double quotes to expand.

  • Also you'll need to read the 'CPU Name' property one way to do this
    would be using a subexpression inside of the double quoted larger expression e.g. "$($variable.property)more text". Or you could concatenate the string $variable.property + 'more text'

  • Since CPU Name has a space in the column name you'll need to enclose that in quotes.

  • Since the User name comes from the Computer name which comes from the
    CSV, defining the User name variable needs to be in the foreach loop.

The security practices in this script are questionable, but outside the scope of the question. e.g. Saving passwords in scripts, especially a local account with administrative privileges to a large number of networked machines that all have that same password...

$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

huangapple
  • 本文由 发表于 2020年1月7日 01:33:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定