Need guidance server to server

Humbly apologize if im not posting in the right place but need guidance to powershell script as I have read its much better then doing via batch.

I need to do the following actually daily at 6:15am;

  1. Check a folder on server B & delete content
  2. Copy a zip file server A to server B and unzip content to that folder
  3. Execute a . exe file and email once completed success/ unsuccessful

Any sample / guide will be helpful :pray:

Welcome to the forum. :wave:t4:

This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

We actually expect you to make an own attempt to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

Regardless of that - it is beyond the scope of this (or any) forum to teach you how to use a complex technology like a scripting language. You will have to learn the very fundamentals of PowerShell first.

thanks Olaf, I understand im not a developer and no experience in scripting.

this is what I can think of but not sure how to put it all together ;

##step 1## Get-ChildItem E:\reports | ForEach { $_.Delete()}

##step 2## Expand-Archive -Path C:\Archives\CompliancePlus_*.Zip | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-1) } | -DestinationPath E:\ reports

##step 3# Start-Process -FilePath “myprogram.exe” -WorkingDirectory “C:\Users\admin\testfolder\experiments”
Send-MailMessage -From ‘User01 user01@tkcom’ -To ‘User02 user02@tkcom’, -Subject '“Successfully updated” -Body “Today’s zip file has been successfully updated in the DB” -DeliveryNotificationOption OnSuccess, -SmtpServer ‘smtp.tkcom’ -Port 587

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

I’m not completely sure if I got what you mean. If all single steps working by themselfs you simply save them in text file with the extrension .ps1 and you have a PowerShell script.

well i want them to be as a single script if i do as you mentioned save as ps1, and set it in task schedule would it know to execute it one after the other?.

anyway my step2 code is throwing errors…so im stuck

as i did inform im not familiar with coding so not aware of the way to post it either sorry about that.

if its all too much trouble…i can delete this post if its a problem…my bad


Sorry if that sounds rude but how should we know that when you don’t tell us? Please keep in mind we cannot see your screen and we cannot read you mind. :smirk:

So please post your code formatted as code along with the COMPLETE error messages you get (formatted as code as well !! ).

That’s fine. We all have been there once. But that does not mean you cannot learn it. :smirk:

I’m still here. I’m still trying to understand your issue. :smirk:

1 Like

(post deleted by author)

Please do not post images of code or errors. Instead post it as text and fomrat it as code.

You can go back, edit your existing post and remove the image and replace it with the error message as text!!!

sorry abt that

PS C:\Windows\system32> Expand-Archive -Path \\xx.xx.xx.xx\SanctionList\JAG01TD_WorldCompliancePlus*.Zip  | Where-Objec
t { $_.LastWriteTime -gt (Get-Date).AddDays(-1) } | -DestinationPath D:\Omni_DataMart_Lexisnexis\DataSource -Force
-DestinationPath : The term '-DestinationPath' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:149
+ ... LastWriteTime -gt (Get-Date).AddDays(-1) } | -DestinationPath D:\Omni ...
+                                                  ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-DestinationPath:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

is it the right way now?

:+1:t4: :wink:

You messed up the syntax of the two cmdlets. The parameter -DestinationPath belongs to the Expand-Archive. This should work:

$Path = '\\xx.xx.xx.xx\SanctionList\JAG01TD_WorldCompliancePlus*.Zip'
$DestinationPath = 'D:\Omni_DataMart_Lexisnexis\DataSource'
Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force 
Get-ChildItem -Path $DestinationPath -Filter * -File |
    Where-Object { $_.LastWriteTime -le (Get-Date).AddDays(-1) } |
        Remove-Item -Force

I used variables for the paths just to make the code lines shorter for better readability. :wink:


Testing the code I just noticed now that the Where-Object does not work this way. So you will have to remove the unwanted files in a further step.

I updated my code suggestion accordingly.

this is what I get, should I save it as a ps1 file and run it as I just executed it in powershell ise

PS C:\Windows\system32> $Path = '\\x.x.x.x\SanctionList\JAG01TD_WorldCompliancePlus*.Zip'
$DestinationPath = 'D:\Omni_DataMart_Lexisnexis\DataSource'
Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force |
    Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-1) } 
Expand-Archive : The archive file path 
'\\x.x.x.x\SanctionList\JAG01TD_WorldCompliancePlus*.Zip' specified as 
input to the Path parameter is resolving to multiple file system paths. 
Provide a unique path to the Path parameter where the archive file has to be 
At line:3 char:1
+ Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (\\\...liancePlus*. 
   Zip:String) [Expand-Archive], IOException
    + FullyQualifiedErrorId : InvalidArchiveFilePath,Expand-Archive

the zip file is also password protected where do I insert the password during unzip?

See my already updated last answer!

As far as I know you cannot extract files from a password protected zip file with built in PowerShell cmdlets. Sorry

Ooops … I actually didn’t pay attention to that detail before. You have to specify a particular file. Wildcards are not allowed. :wink:

i used the updated code and

PS C:\Windows\system32> $Path = '\\x.x.x.x\SanctionList\JAG01TD_WorldCompliancePlus_"*".Zip'
$DestinationPath = 'D:\Omni_DataMart_Lexisnexis\DataSource'
Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force 
Get-ChildItem -Path $DestinationPath -Filter * -File |
    Where-Object { $_.LastWriteTime -le (Get-Date).AddDays(-1) } |
        Remove-Item -Force
Expand-Archive : The path '\\x.x.x.x\SanctionList\JAG01TD_WorldCompliancePlus_"*".Zip' either does not exist or is not a valid file system path.
At line:3 char:1
+ Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (\\\...ncePlus_"*".Zip:String) [Expand-Archive], InvalidOperationException
    + FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Expand-Archive

If i have multiple files ending by date in folder name how do I specify it? I used the linux * which works fine there but in powershell and its password protected zip file pls advise where to insert password?

just saw this ouch :laughing:

just saw this ouch :laughing:

was hoping something similar to batch script or robocopy possibilties

There are possibilities. :wink:

To list files you can use

To process all returned files you can use

You may start with reading the help for the cmdlets completely including the examples to learn how to use them.

i tried this now @Olaf it does extract the folder but it also takes each and every folder and tries to unzip into destination path with error below, im not even sure how it did unzip when I didnt specify password

PS C:\Windows\system32> $SourceDir = '\\x.x.x.x\SanctionList\'
$DestinationDir = 'D:\Omni_DataMart_Lexisnexis\DataSource'
$FileNameAndExtensio = "places.sqlite"
Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force 
Get-Childitem –Path "$SourceDir"  -Include "$FileNameAndExtension" -File -Recurse | Copy-Item  -Destination "$DestinationDir" | Where-Object { $_.LastWriteTime -le (Get-Date).AddDays(-1) } | Remove-Item -Force
Expand-Archive : The path '\\x.x.x.x\SanctionList\JAG01TD_WorldCompliancePlus_"*".Zip' either does not exist or is not a valid file system path.
At line:4 char:1
+ Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (\\x.x.x.x\...ncePlus_"*".Zip:String) [Expand-Archive], InvalidOperationException

im messing this up i guess