[英]import CSV info to powershell script
我正在嘗試運行將CSV信息導入PowerShell的腳本,以卸載遠程計算機上的軟件。 CSV文件包含諸如主機名,IP地址和已安裝應用程序名稱之類的信息。 CSV文件如下所示(抱歉輸入錯誤):
Name,Serial Number,IP Address,MAC Address,Installed Applications computer1,ABC123,1.1.1.1,12:34:45:67:89,Adobe Air
基本上,該想法是卸載“名稱”(計算機主機名)上的“已安裝的應用程序”(對於此示例為Adobe Air)。
PowerShell腳本:
$csv = Import-Csv C:\path\report.csv
$DisplayName = $csv."Installed Applications"
$path = Split-Path -Path $MyInvocation.MyCommand.Path
$computers = $csv.Name
foreach ($server in $computers) {
$app = Get-WmiObject -Class Win32_Product -ComputerName $server |
Where-Object {$_.DisplayName -match $csv."Installed Applications"}
$pathobj = New-Object -TypeName PSobject
$pathobj | Add-Member -MemberType NoteProperty -Name Computername -Value $server.ToUpper()
$pathobj | Add-Member -MemberType NoteProperty -Name Software -Value $csv."Installed Applications"
$var = ($app.Uninstall()).ReturnValue
if($var -eq 0) {
$pathobj | Add-Member -MemberType NoteProperty -Name Status -Value "Successfully Uninstalled"
$pathobj | Add-Member -MemberType NoteProperty -Name Date -Value $(Get-Date)
} else {
$pathobj | Add-Member -MemberType NoteProperty -Name Status -Value "Unable to uninstall"
$pathobj | Add-Member -MemberType NoteProperty -Name Date -Value $(Get-Date)
}
#Write-Output $pathobj | Export-Csv "$path\ObsoleteStatus.csv" -NoTypeInformation -Append
}
當我執行它時,它總是給我:
You cannot call a method on a null-valued expression. At C:\Users\sbellec\Desktop\test5.ps1:18 char:28 + $var = ($app.Uninstall <<<< ()).ReturnValue + CategoryInfo : InvalidOperation: (Uninstall:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
我嘗試了不同的方法來修復它,但沒有結果。 關於我在做什么錯的任何想法嗎?
$computers = $csv.Name
僅選擇一列,因此在運行foreach時,您將在此列中進行迭代;
$_.DisplayName -match $csv."Installed Applications"
以相同的方式檢查Wheather DisplayName是否與“已安裝的應用程序”列匹配,因此您在此處不會得到任何明智的結果。
您只需要在整個csv陣列上運行foreach
foreach ($row in $csv) {
然后使用$ row.Name和$ row。“ Installed Applications”作為與同一服務器行對應的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.