Need help in fixing the powershell script

Welcome Forums General PowerShell Q&A Need help in fixing the powershell script

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #159612

    Participant
    Topics: 1
    Replies: 0
    Points: 12
    Rank: Member

    This is the script basically written to fetch the data from a single csv file for backup and issue that we are facing is the script gets executed but we are not able to find the file under C:\temp. Can you please help us in fixing this issue.

    Function Set-abcBackup {
    
    [CmdletBinding()] 
    
    Param(
    [Parameter(Mandatory=$true)]
    [String]$Filepath,
    
    [Parameter(Mandatory=$false)]
    [String]$Folder
    
    )
    
    Begin {
    $Folder = "$Filepath\Backup_Files"
    
    If(!(Test-Path $Folder)) {
    
        mkdir $Folder -Force | Out-Null
    
                             }
    
    $latestfile = Get-ChildItem "$Filepath\*.csv" | ? {$_.LastWriteTime -ge (Get-Date).Date}
    
    $filefullname = ($latestfile).FullName
    }
    
    Process {
    Get-Content $filefullname | select -Skip 22 > "$Folder\RemovedColumns.csv"
    
    $Getdata = Import-Csv "$Folder\RemovedColumns.csv"
    
    $Getdata | Export-Csv "$Folder\RemovedColumnsModified.csv" -NoTypeInformation
    
    $Newdata = Import-Csv "$Folder\RemovedColumnsModified.csv"
    
    $count = 0
    Foreach($data in $newdata) {
    
        If($data.Client -notlike "Client") {$count1 = $count++}
    
        Else {$count1 = $count
        break
    }
    }
    
    $count2 = $count1+1
    
    $count2 -as [int] | Out-Null
    
    Get-Content "$Folder\RemovedColumnsModified.csv" | select -Skip $count2 > "$Folder\Backup1.csv"
    
    $AllFiles = Import-Csv "$Folder\Backup1.csv"
    
    Foreach($file in $AllFiles) {
    
        $Client = $file.Client
        $Agent = $file.Agent
        $Status = $file.Status
        $BackupSet = $file.'Backup Set'
        $StartTime = $file.'Start Time'
        $Type = $file.Type
        $EndTime = $file.'End Time or Current Phase'
        $ScanType = $file.'Scan Type'
    
    If(($Client -notlike "*Protect*") -and ($Client -notlike "*Notes:*") -and ($Client -notlike "*Job*") -and ($Client -notlike "*#*") -and ($Client -notlike "*Sysbase*") -and ($Client -notlike "*Client*") -and ($Client -notlike "*SP_abc*") -and ($Client -notlike "*All*") -and ($Client -notlike "*4*") -and ($Client -notlike $null)){
    
    $Client1 = $Client }
    
    If(($Status -like "*Comple*") -or ($Status -like "*Fail*")){$Status1 = $Status}
    
    If(($StartTime -notlike $null) -and ($StartTime -notlike "*N/A*") -and ($StartTime -notlike "*Current*") -and ($StartTime -notlike "*Out of*") -and ($StartTime -notlike "*Not*") -and ($StartTime -notlike "*VM Gue*")){$StartTime1 = $StartTime}
    
    If(($EndTime -notlike $null) -and ($EndTime -notlike "*Operating*") -and ($EndTime -notlike "*Linux*") -and ($EndTime -notlike "*CentOS*") -and ($EndTime -notlike "*Microsoft*") -and ($EndTime -notlike "*N/A*")){$EndTime1 = $EndTime}
    
    If(($Agent -notlike $null) -and ($Agent -notlike "*Virtual Ser*") -and ($Agent -notlike "*Active Dir*") -and ($Agent -notlike "*Apps*") -and ($Agent -notlike "*File System*") -and ($Agent -notlike "*Machine*") -and ($Agent -notlike "*SQL ser*") -and ($Agent -notlike "*Oracle*")) {$Agent1 = $Agent}
    
    If(($BackupSet -like "*Comple*") -or ($BackupSet -like "*Fail*")){$BackupSet1 = $BackupSet}
    
    If(($Type -notlike $null) -and ($Type -notlike "*INCR*") -and ($Type -notlike "*SYN*") -and ($Type -notlike "*Log*") -and ($Type -notlike "*FULL*") -and ($Type -notlike "*Start Ti*")){$Type1 = $Type}
    
    If(($ScanType -notlike $null) -and ($ScanType -notlike "*Classic*") -and ($ScanType -notlike "*Scan*") -and ($ScanType -notlike "*N/A*") -and ($ScanType -notlike "*N/A*") -and ($ScanType -notlike "*End Ti*")){$ScanType1 = $ScanType}
    
    $PSObject = New-Object PSObject
    Add-Member -InputObject $PSObject -MemberType NoteProperty -Name "Name" -Value $Client1
    Add-Member -InputObject $PSObject -MemberType NoteProperty -Name "BackupStatus" -Value $Status1
    Add-Member -InputObject $PSObject -MemberType NoteProperty -Name "StartTime" -Value $StartTime1
    Add-Member -InputObject $PSObject -MemberType NoteProperty -Name "EndTime" -Value $EndTime1
    $PSObject | Export-Csv "$Folder\FirstPart.csv" -NoTypeInformation -Force -Append
                       
    $PSObject1 = New-Object PSObject
    Add-Member -InputObject $PSObject1 -MemberType NoteProperty -Name "Name" -Value $Agent1 -Force
    Add-Member -InputObject $PSObject1 -MemberType NoteProperty -Name "BackupStatus" -Value $BackupSet1 -Force
    Add-Member -InputObject $PSObject1 -MemberType NoteProperty -Name "StartTime" -Value $Type1 -Force
    Add-Member -InputObject $PSObject1 -MemberType NoteProperty -Name "EndTime" -Value $ScanType1 -Force
    $PSObject1 | Export-Csv "$Folder\SecondPart.csv" -NoTypeInformation -Force -Append
                               }
    
    $csv1 = Import-Csv "$Folder\FirstPart.csv"
    $csv2 = Import-Csv "$Folder\SecondPart.csv"
    $merge = $csv1 + $csv2 
    $merge | select -Property * -Unique | Export-Csv "$Folder\xyz_Backup_Final.csv" -NoTypeInformation -Force
    
    $File = Import-Csv "$Folder\xyz_Backup_Final.csv"
    
    $File | % {
    $Name = $_.Name
    $Operation = "Backup"
    $Client = "xyz"
    $BStatus = $_.BackupStatus
    $End = $_.EndTime
    $Start = $_.StartTime
    $End_Time = [DateTime]::Parse($end)
    $Start_Time = [DateTime]::Parse($start) 
    $_.starttime = ($Start_Time).tostring("dd/MM/yyyy hh:mm:ss")
    $_.endtime= ($End_Time).tostring("dd/MM/yyyy hh:mm:ss")
    $ST = $_.StartTime
    $ET = $_.EndTime
    $Total_Duration=$End_Time-$Start_Time
    $duration = $Total_Duration.tostring("hh':'mm':'ss")
    
    $PSObject = New-Object PSObject
    Add-Member -InputObject $PSObject -MemberType NoteProperty -Name "Name" -Value $Name
    Add-Member -InputObject $PSObject -MemberType NoteProperty -Name "Operation" -Value $Operation
    Add-Member -InputObject $PSObject -MemberType NoteProperty -Name "BackupStatus" -Value $BStatus
    Add-Member -InputObject $PSObject -MemberType NoteProperty -Name "Client" -Value $Client
    Add-Member -InputObject $PSObject -MemberType NoteProperty -Name "StartTime" -Value $ST
    Add-Member -InputObject $PSObject -MemberType NoteProperty -Name "EndTime" -Value $ET
    Add-Member -InputObject $PSObject -MemberType NoteProperty -Name "Duration" -Value $duration
    $PSObject | Export-Csv "$Folder\xyz_Backup.csv" -Force -NoTypeInformation -Append
    
    } 
    }
    
    End {
    Move-Item -Path "$Folder\xyz_Backup.csv" -Destination "C:\Temp\xyz_Backup.csv" -Force
    
    Del $Folder -Recurse -Force
    
    #Get-ChildItem $Filepath -Filter "*.csv" | Remove-Item -Force
    }
    }
    
    Set-abcBackup -Filepath "C:\Path\To\CSV" -ErrorAction SilentlyContinue
    
    

     

  • #159618

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    Well, can yo format the code, it'll look good and more readable when formatted. Please go though below post for instructions.

    https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did/

    PS: You can edit the same post.

  • #162272

    Participant
    Topics: 2
    Replies: 54
    Points: 278
    Helping Hand
    Rank: Contributor

    Let's cover the basics:

    1. Your script doesn't create C:\Temp and it's not a normal system directory. Does that directory already exist?
    2. Does the user executing the script have permission to write to C:\Temp?
    3. Do you receive any error messages when the script executes?
    4. Have you tried running the script with debugging enabled?
    5. Have you performed any troubleshooting steps already? For instance, have you checked any of the variable storage steps to see if there are actual values stored in the variables?

The topic ‘Need help in fixing the powershell script’ is closed to new replies.