简体   繁体   English

Powershell - 删除包含单词的 excel 行

[英]Powershell - Delete excel rows that contain a word

I'm really new to Powershell and I feel like I've looked all over and can't quite figure out what is wrong with my code.我对 Powershell 真的很陌生,我觉得我已经看了一遍,无法弄清楚我的代码有什么问题。

My goal is a powershell script that can run against an Excel workbook and delete rows with a specific string in the cell (in this case it is local admin accounts).我的目标是一个 powershell 脚本,它可以针对 Excel 工作簿运行并删除单元格中具有特定字符串的行(在本例中是本地管理员帐户)。

Currently my script launches the excel sheet opens, but no rows are deleted.目前我的脚本启动 excel 工作表打开,但没有删除任何行。 The code exits without error.代码退出没有错误。 Any help would be greatly appreciated任何帮助将不胜感激

$ObjExcelCellTypeLastCell = 11

$ObjExcel = New-Object -ComObject Excel.Application
$ObjExcel.Visible = $True
$ObjExcel.DisplayAlerts = $True
$Workbook = $ObjExcel.Workbooks.Open("File\Path\") 
$Worksheet = $Workbook.Worksheets.Item(1)
$used = $Worksheet.usedRange
$lastCell = $used.SpecialCells($ObjExcelCellTypeLastCell)
$row = $lastCell.row

for ($i = $Worksheet.usedrange.rows.count; $i -gt 0; $i--)
 {
    If ($Worksheet.Cells.Item($i, 1) = "Local Admin") {
        $Range = $Worksheet.Cells.Item($i, 1).EntireRow
        $Range.Delete()
        $i = $i + 1

    Else 
    Break
    }
Exit
}

I don't know much about powershell but i think your if statement $Worksheet.Cells.Item($i, 1) = "Local Admin" is wrong, you should use -eq also maybe you need to call the Close method on the workbook object that you just Open 'd我不太了解 powershell 但我认为你的 if 语句$Worksheet.Cells.Item($i, 1) = "Local Admin"是错误的,你应该使用-eq也可能你需要调用Close方法您刚刚Open的工作簿 object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM