Powershell script not working properly

Welcome Forums General PowerShell Q&A Powershell script not working properly

Viewing 2 reply threads
  • Author
    Posts
    • #183165
      Participant
      Topics: 1
      Replies: 0
      Points: 11
      Rank: Member

      Hi Team I have powershell script that renames an existing file to a new file with appending sysdate, I am using the following script for this, this is called as part of another batch file, the file renaming is working properly but the sysdate is not appending properly for example today is 16th october but the file is renamed with 14th ocotober

      
      function RenameFile
      (
      [string]$source_dir,
      [string]$source_file,
      [string]$source_filedate,
      [string]$target_dir,
      [string]$target_prefix,
      [string]$target_suffix
      )
      {
      $source_file_name = get-ChildItem $source_dir -recurse | where {$_.name -like $source_file.replace("",$source_filedate)} | select -exp name
      if ([string]::IsNullOrEmpty($source_file_name))
      {
      throw 'File Missing: ' + $source_file
      
      }
      else
      {
      $source_file = $source_dir + $source_file_name
      $file = Get-Item $source_file
      
      $a = Get-Date -format ddMMyyyy
      $target_file = $target_dir + $target_suffix+"_"+"$a"+$target_prefix
      Copy-Item $source_file $target_file
      }
      }
      
      $source_directory = "E:\CSBE\output_files\DUEDIL\BE\FTP\"
      $target_directory = "E:\CSBE\output_files\DUEDIL\BE\FTP\SP_NEGINFO"
      
      Remove-Item "E:\CSBE\output_files\DUEDIL\BE\FTP\SP_NEGINFO\temp\*" -Force -Recurse
      
      $todays_date = Get-Date -format "yyyyMMdd"
      RenameFile -source_dir $source_directory -source_file BE_NEGATIVE_INFORMATION__D.dsv -source_filedate $todays_date -target_dir $target_directory INSOLVENCY_BE -target_prefix.dsv
      
      $target_directory_temp = "E:\CSBE\output_files\DUEDIL\BE\FTP\SP_NEGINFO\temp\"
      RenameFile -source_dir $source_directory -source_file BE_NEGATIVE_INFORMATION__D.dsv -source_filedate $todays_date -target_dir $target_directory_temp INSOLVENCY_BE -target_prefix.dsv
      
      cd "E:\CSBE\output_files\DUEDIL\BE\FTP\SP_NEGINFO\temp\"
      
      dir "*.dsv" | ForEach-Object { & "\\Csgimp19\csbe2\bin\7Z.exe" a -tzip ($_.BaseName+".zip") $_.Name }
      
    • #183201
      Participant
      Topics: 2
      Replies: 136
      Points: 714
      Helping Hand
      Rank: Major Contributor

      I don't see anything odd there, add verbose statements and run it again with -Verbose.

      Thank you.

    • #183279
      Participant
      Topics: 1
      Replies: 63
      Points: 355
      Helping Hand
      Rank: Contributor

      I don't see any obvious flaws so I would look at a few basic items:

      1. Is the system date on the server running the script correct?
      2. How long was this running?  (is it possible the script has been running since 10/14? )
      3. Has it ever worked properly?
      4. If you run it manually does it work?
Viewing 2 reply threads
  • You must be logged in to reply to this topic.