'Using Powershell in Excel to check row status = "yes" then trigger bat file, if fail to run bat file skip and do next bat file & generate log
I have a spreadsheet with 4 columns. so I would like to run a powershell script to check every cell in the status column and if yes, run the bat file. In the excel file there is a list of apps, their status, and a corresponding path where the bat file is stored. If there is an error in the run of the bat file, it should skip to the next bat file and at the end generate a log file if there were errors or not for corresponding files.
#1
PS C:\Users\rezas> $Path = "D:\RONY\bad\list.xlsx"
Import-Excel -Path $Path
ID Application name Status bat file
-- ---------------- ------ --------
1 App_1 YES D://scripts/App_1.bat
2 App_2 YES D://scripts/App_2.bat
3 App_3 YES D://scripts/App_3.bat
4 App_4 NO D://scripts/App_4.bat
5 App_5 NO D://scripts/App_5.bat
6
7
.
.
#2 I can't figure out yet :(
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$Workbook = $objExcel.Workbooks.Open(“D:\RONY\bad\list.xlsx”)
$Worksheet = $Workbook.Sheets.Item("App_list")
$UsedRange = $WorkSheet.UsedRange
$Rows = $UsedRange.Rows.Count
$Objs = @()
for($Row=2; $Row -le $Rows; $Row++){
$Objs += [PSCustomObject]@{
$ID = $UsedRange.Cells($Row,1).Text
$Application_name = $UsedRange.Cells($Row,2).Text
$Status= $UsedRange.Cells($Row,3).Text
$bat_file= $UsedRange.Cells($Row,4).Text
}
}
ForEach ($ComputerName in $Objs){
if ($Status -eq 'Yes')
{
$bat_filet
}
Else
{
if ($Status -eq "No")
{
$bat_filet
}
}
}
$objExcel.Quit()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
