`param (
[string]$CaseIdentifier
)
Write-Host "CaseIdentifier value: $CaseIdentifier"
Write-Host "Script Start"
try {
Add-Type -Path "C:Program Files (x86)WinSCPWinSCPnet.dll" # Update with your actual path
Write-Host "WinSCP .NET Assembly loaded successfully."
} catch {
Write-Host "Error loading WinSCP .NET Assembly: $_"
exit 1
}
Import-Module -Name ImportExcel
try {
$modulePath = (Get-Module -Name ImportExcel).Path
Write-Host "Module path: $modulePath"
if (-not ($modulePath)) {
Write-Host "ImportExcel module not loaded."
exit 1
} else {
Import-Module $modulePath
Write-Host "ImportExcel module imported successfully."
}
} catch {
Write-Host "Error importing ImportExcel module: $_"
exit 1
}
$excelFilePath = "srctestresourcesDatatablesWITSOnline_NNAmultivinInquiry.xlsx"
Write-Host "Excel file path: $excelFilePath"
$sheetName = "General_Data"
Write-Host "Sheet name: $sheetName"
try {
# Read data from the Excel sheet
$exceldata = Import-Excel -Path $excelFilePath -WorksheetName $sheetName
Write-Host "Excel data imported successfully."
} catch {
Write-Host "Error importing Excel data: $_"
exit 1
}
foreach ($row in $exceldata) {
try {
$hostName = $row.HostName
Write-Host "Hostname: $hostName"
$userName = $row.UserName
Write-Host "Username: $userName"
$passWord = $row.Password
Write-Host "Password: $passWord"
$timeStamp = Get-Date -Format "yyyyMMddHHmmss"
# Debug: Print the value of CaseIdentifier
Write-Host "CaseIdentifier before switch: $CaseIdentifier"
# Debug: Print the switch statement is about to execute
Write-Host "Executing switch statement with CaseIdentifier: $CaseIdentifier"
switch ($CaseIdentifier.Trim().ToLower()) {
"case1" {
Write-Host "Inside case1 block"
# $bodFilePath = $row.smyRNABodfilepath
# Write-Host "smyRNABodfilepath value: $($row.smyRNABodfilepath)"
# $remotePath = $row.ftplocationPath1
# Write-Host "Selected case1"
}
"case2" {
Write-Host "Inside case2 block"
# $bodFilePath = $row.losAngelesBodfilepath
# Write-Host "losAngelesBodfilepath value: $($row.losAngelesBodfilepath)"
# $remotePath = $row.ftplocationPath2
# Write-Host "Selected case2"
}
"case3" {
Write-Host "Inside case3 block"
$bodFilePath = $row.AguasBodfilepath
Write-Host "AguasBodfilepath value: $($row.AguasBodfilepath)"
$remotePath = $row.ftplocationPath3
Write-Host "Selected case3 "
}
"case4" {
Write-Host "Inside case4 block"
$bodFilePath = $row.Aguas2Bodfilepath
Write-Host "Aguas2Bodfilepath value: $($row.Aguas2Bodfilepath)"
$remotePath = $row.ftplocationPath4
Write-Host "Selected case4"
}
default {
Write-Host "Invalid case identifier: $CaseIdentifier"
exit 1
}
}
# Debug: Ensure $bodFilePath and $remotePath are set
Write-Host "BOD file path: $bodFilePath"
Write-Host "Remote path: $remotePath"
# Remote path where you want to upload the file
$ftpFileLocation = $remotePath -replace '${timeStamp}', $timeStamp
Write-Host "FTP file location: $ftpFileLocation"
} catch {
Write-Host "Error reading properties: $_"
exit 1
}
}
try {
# Set up session options for FTP
Write-Host "Setting up session options"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = $hostName
UserName = $userName
Password = $passWord
}
# Create Session
$session = New-Object WinSCP.Session
# Connect
Write-Host "Opening session"
$session.Open($sessionOptions)
# Upload file
Write-Host "Uploading file"
$transferOptions = New-Object WinSCP.TransferOptions
$transferResult = $session.PutFiles($bodFilePath, $ftpFileLocation, $False, $transferOptions)
# Check for errors
$transferResult.Check()
# Output a success message
Write-Host "File uploaded successfully."
Write-Host "Remote path: $remotePath"
} catch {
Write-Host "Error during FTP operation: $_"
} finally {
# Disconnect, clean up
Write-Host "Disposing session"
$session.Dispose()
Write-Host "Script End"
}
I am trying to running my powershell script to open FileZila through Java.But in powershell code i have different case statments and control is not entering into switch after read the excel sheet. not able to find out the issue. We are calling this scripts using Java.