Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

Need to delete the unwanted columns from excel

$
0
0

HI There,

I am trying to delete unwanted excel columns from the excel file and to display that in HTML file, i am trying to test it the powershell window closes with out any error. and my script is

# Copy the sourcefile ; Generally, The Source is taken from any other shared path
$sourcefile = "https:// file path

#This section will install the software
foreach ($ComputerName in $Servername)
{
 $destinationFolder = "D:\Temp"
 #This section will copy the $sourcefile to the $destinationfolder. If the Folder does not exist it will create it.
 if (!(Test-Path -path $destinationFolder))
 {
  New-Item $destinationFolder -Type Directory
 }
 Copy-Item -Path $sourcefile -Destination $destinationFolder

Param([string]$wbname)

Try
{
 $err1 = 0;
 #testing only #$wbname = "D:\Temp\cmdb.xls"

 $xl = New-Object -comobject Excel.Application
 $xl.DisplayAlerts = $False

 $wb = $xl.Workbooks.open($wbname)
 $sheet = $wb.Sheets.Item(1)

#delete required columns
while( ($sheet.Cells.Item(1,1).Text -eq "") -and ($sheet.Cells.Item(4,1).Text -eq "") -and ($sheet.Cells.Item(7,1).Text -eq "") |
-and ($sheet.Cells.Item(26,1).Text -eq "") -and ($sheet.Cells.Item(28,1).Text -eq "") -and ($sheet.Cells.Item(29,1).Text -eq "") |
-and ($sheet.Cells.Item(30,1).Text -eq "") -and ($sheet.Cells.Item(31,1).Text -eq "") -and ($sheet.Cells.Item(32,1).Text -eq ""))
{[void]$sheet.Cells.Item(1,1).EntireColumn.Delete()}

#delete rows
while( ($sheet.Cells.Item(1,1).Value() -NotLike "Identifier") -and ($sheet.Cells.Item(1,2).Value() -NotLike "Identifier")){[void]$sheet.Cells.Item(1,1).EntireRow.Delete()}

#cleanup
$wb.Close($true)
$xl.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
Remove-Variable xl

}
Catch
{
    $err1 = 1;
}
Finally
{
    if($err1 -eq 1)
    {
        #Write-Output "99";
 #Write-Output $_.Exception.Message;
stream = [System.IO.StreamWriter] "D:\error.txt"
 $stream.WriteLine($Error[0].InvocationInfo.PositionMessage)
 $stream.WriteLine($wbname)
  [Environment]::Exit("99");
    }
    Else
    {
        #Write-Output "0";
        [Environment]::Exit("0");
    }

}

 


Viewing all articles
Browse latest Browse all 8583

Trending Articles