Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Retrieve Account Disablement CSV File sporadically not working

Joshua Comeau
Kilo Sage
Sometimes when trying to pull the csv file it is not moving 
I want that whenever theirs a file that it will move it regardless of the date & time:

Script:
//Move the file to other folder
        var credentials_sys_id = gs.getProperty('mid_file_retrieval.credentials');
        var credentialsGR = new GlideRecord('windows_credentials');
        credentialsGR.get(credentials_sys_id);
        if (credentialsGR.isValid()) {
            var Encrypter = new GlideEncrypter();
            var decrypted = Encrypter.decrypt(credentialsGR.password);
            var hashedCredentials = GlideStringUtil.base64Encode(credentialsGR.user_name + ':' + String(decrypted));

            for(var index in filesProcessed) {
                var fileNameToMove = filesProcessed[index];
                var probe = SncProbe.get("Command");
                probe.setName('powershell scripts\\PowerShell\\RetrieveNetworkFileContent.ps1 -SecurityHash ' + hashedCredentials + ' -Command MoveCompletedFile -FilePath \'\"\\\\max1\\Dept\\Integrations\"\' -Filter \'\"' + fileNameToMove + '\"\'');
                probe.addParameter("skip_sensor", true);
                var midServer = gs.getProperty('mid_file_retrieval.mid_server');
                probe.create(midServer);
            }


PowerShell Script mentions the following:

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