File retrieval scheduled job calling powershell script failing

Joshua Comeau
Kilo Sage

sometimes it is being noticed that the file data is being stripped 

 

would this powershell script have somewhere in it that is striping the data?

 

param(
[Parameter()]
[string]$SecurityHash,

[Parameter()]
[string]$Command,

[Parameter()]
[string]$FilePath,

[Parameter()]
[string]$FileName,

[Parameter()]
[string]$Filter,

[Parameter()]
[string]$Destiny
)

$global:status = "Success"
$global:credentials = ""

function getCredentials() {
param([string]$SecurityHash)

If(!$SecurityHash) {
return
}

$decoded = [Text.Encoding]::Utf8.GetString([Convert]::FromBase64String($SecurityHash))
$creds = $decoded.Split(":")
$passwd = ConvertTo-SecureString -String $creds[1] -AsPlainText -Force
$adm_credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $creds[0], $passwd

return $adm_credentials

}

function ListFiles() {
param(
[string]$FilePath,
[string]$filter
)

$dirContent = ""
try {
If(!$global:credentials) {
New-PSDrive -Name iolsnow -PSProvider FileSystem -Root $FilePath | Out-Null
} Else {
New-PSDrive -Name iolsnow -PSProvider FileSystem -Root $FilePath -Credential $global:credentials | Out-Null
}

If(-Not(Test-Path -Path "iolsnow:\")) {
$global:status = "Error"
$dirContent = "Error drive couldn't connect"
} Else {
If(!$filter) {
$dirContent = ls "iolsnow:\" -Name
} Else {
$dirContent = ls "iolsnow:\" -Include $filter -Name
}
$global:status = "Success"
}
} catch {
$global:status = "Error"
$dirContent = $_.Exception.Message
}
Remove-PSDrive -Name iolsnow -ErrorAction SilentlyContinue

return $dirContent
}


function GetFileContent() {
param(
[string]$FilePath,
[string]$fileName
)

If(!$fileName) {
$global:status = "Error"
$encContent = "FileName is empty"
} Else {
$encContent = ""
try {
If(!$global:credentials) {
New-PSDrive -Name iolsnow -PSProvider FileSystem -Root $FilePath | Out-Null
} Else {
New-PSDrive -Name iolsnow -PSProvider FileSystem -Root $FilePath -Credential $global:credentials | Out-Null
}

If(-Not(Test-Path -Path "iolsnow:\")) {
$global:status = "Error"
$encContent = "Error drive couldn't connect"
} Else {
$fullPath = "iolsnow:\" + $fileName
If (-Not(Test-Path -Path $fullPath)) {
$global:status = "Error"
$encContent = "FileName provided does not exist in the system"
} Else {
$global:status = "Success"
$fileContent = Get-Content -Path $fullPath -Raw
$encContent = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($fileContent))
}
}
} catch {
$global:status = "Error"
$encContent = $_.Exception.Message
}
Remove-PSDrive -Name iolsnow -ErrorAction SilentlyContinue
}


return $encContent
}

function GetFullFileContent() {
param(
[string]$FilePath,
[string]$fileName
)

If(!$fileName) {
$global:status = "Error"
$encContent = "FileName is empty"
} Else {
$encContent = ""
try {
If(!$global:credentials) {
New-PSDrive -Name iolsnow -PSProvider FileSystem -Root $FilePath | Out-Null
} Else {
New-PSDrive -Name iolsnow -PSProvider FileSystem -Root $FilePath -Credential $global:credentials | Out-Null
}

If(-Not(Test-Path -Path "iolsnow:\")) {
$global:status = "Error"
$encContent = "Error drive couldn't connect"
} Else {
$fullPath = "iolsnow:\" + $fileName
If (-Not(Test-Path -Path $fullPath)) {
$global:status = "Error"
$encContent = "FileName provided does not exist in the system"
} Else {
$global:status = "Success"
$fileContent = Get-ChildItem -Path "iolsnow:\" -Filter $fileName | ForEach-Object { "Filename:{0}`n{1}" -f $_.Name, (Get-Content $_.FullName -Raw) }
$encContent = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($fileContent))
}
}
} catch {
$global:status = "Error"
$encContent = $_.Exception.Message
}
Remove-PSDrive -Name iolsnow -ErrorAction SilentlyContinue
}


return $encContent
}

function MoveFileToCompletedFolder() {
param(
[string]$FilePath,
[string]$fileName
)

If(!$fileName) {
return $false
}


$encContent = ""
try {
If(!$global:credentials) {
New-PSDrive -Name iolsnow -PSProvider FileSystem -Root $FilePath | Out-Null
} Else {
New-PSDrive -Name iolsnow -PSProvider FileSystem -Root $FilePath -Credential $global:credentials | Out-Null
}

If(-Not(Test-Path -Path "iolsnow:\")) {
$global:status = "Error"
$encContent = "Error drive couldn't connect"
} Else {
$global:status = "Success"
$fullPath = "iolsnow:\ + $fileName
$newPath = "iolsnow:\Completed\"
Move-Item -Path $fullPath -Destination $newPath
$encContent = "File has moved successfully"
}
} catch {
$global:status = "Error"
$encContent = $_.Exception.Message
}
Remove-PSDrive -Name iolsnow -ErrorAction SilentlyContinue

return $encContent
}

function copyTestFile() {
param(
[string]$FilePath,
[string]$fileName,
[string]$Destiny
)

$global:status = "Error"
$output = "";
If(!$FilePath ) {
return "No Filepath provided"
}

If(!$fileName ) {
return "No Filename provided"
}

If(!$Destiny ) {
return "No Destiny path provided"
}


try {
New-PSDrive -Name iolsnow -PSProvider FileSystem -Root $FilePath -Credential $global:credentials | Out-Null
New-PSDrive -Name iolsnownew -PSProvider FileSystem -Root $Destiny -Credential $global:credentials | Out-Null

$fullPath = "iolsnow:\" + $fileName
Copy-Item $fullPath -Destination "iolsnownew:\"

$global:status = "Success"
$output ="File(s) copied successfully"
} catch {
$global:status = "Error"
$output = $_.Exception.Message
}

Remove-PSDrive -Name iolsnow -ErrorAction SilentlyContinue
Remove-PSDrive -Name iolsnownew -ErrorAction SilentlyContinue

return $output
}

function removeFile() {
param(
[string]$FilePath,
[string]$fileName
)

$global:status = "Error"
If(!$FilePath ) {
return "No Filepath provided"
}

If(!$fileName ) {
return "No Filename provided"
}

$output = "";
try {
New-PSDrive -Name iolsnow -PSProvider FileSystem -Root $FilePath -Credential $global:credentials | Out-Null

$fullPath = "iolsnow:\" + $fileName
$output = Remove-Item $fullPath

$global:status = "Success"
##$output ="File(s) copied successfully"
} catch {
$global:status = "Error"
$output = $_.Exception.Message
}

Remove-PSDrive -Name iolsnow -ErrorAction SilentlyContinue

return $output
}

$global:credentials = getCredentials $SecurityHash

If( $Command -eq "ListDir") {

$global:status = "Success"
try {
$output = ListFiles $FilePath $Filter
} catch {
$output = $_.Exception.Message
$global:status = "Error"
}
} ElseIf ( $Command -eq "MoveCompletedFile") {
$global:status = "Success"
try {
$output = MoveFileToCompletedFolder $FilePath $Filter
} catch {
$output = $_.Exception.Message
$global:status = "Error"
}
} ElseIf ( $Command -eq "CopyTestFile") {
$global:status = "Success"
try {
$output = copyTestFile $FilePath $FileName $Destiny
} catch {
$output = $_.Exception.Message
$global:status = "Error"
}
} ElseIf ( $Command -eq "DeleteTestFile") {
$global:status = "Success"
try {
$output = removeFile $FilePath $FileName
} catch {
$output = $_.Exception.Message
$global:status = "Error"
}
} ElseIf ( $Command -eq "GetAllFile") {
$global:status = "Success"
try {
$output = GetFileContent $FilePath $FileName
} catch {
$output = $_.Exception.Message
$global:status = "Error"
}
} Else {
try {
$output = GetFullFileContent $FilePath $FileName
} catch {
$output = $_.Exception.Message
$global:status = "Error"
}
}


$response = @{
status = $global:status
body = $output
} | convertTo-Json

Write-Output $response

0 REPLIES 0