因此,我在试图从许多计算机获取修补程序(列在列"IP地址“下的csv文件中)并将结果导出到CSV时遇到了一些困难。它们都需要一个本地计算机帐户才能登录(在列"CPU名称“下的同一个CSV中)。我并不在乎整个过程是一个csv,还是每个结果都是csv。以下是目前为止的代码:
$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"
}我接近了吗?
发布于 2020-01-07 00:47:54
IP地址和计算机名来自CSV,因此您不需要静态地定义它们。
关于用户名,有几件事:
"$($variable.property)more text" )中使用子表达式。或者您可以连接字符串$variable.property + 'more text'CPU Name在列名中有一个空格,需要用引号括起来。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 MySecureCredshttps://stackoverflow.com/questions/59616507
复制相似问题