Author Posts

August 22, 2013 at 5:15 am

I am new to Powershell. I am attempting to create a powershell script that will archive a “LOGS” folder located in the root of “C” drive from a workstation. The “LOGS” folder consisted of several subfolders. Each subfolder holds different types of logs. The script should create a folder with the computername on the server share, create a subfolder with today’s date and move the “LOGS” folder to the server share inside the today’s date subfolder which is inside the computername folder.

When I open a powershell console on a local computer and run the script, it moved the “LOGS” folder to the server share without any issues. The problem is that I would like to run the script on a remote computer. Let’s say, copy the script to a remote computer and run it remotely. These are the commands I have tried:

Invoke-Command –filepath c:\backup.ps1 –computername computer002
Invoke-Command –Computername computer002 –ScriptBlock {C:\backup.ps1} –Credential DOMAIN\User
Invoke-Command –Computername computer002 –b.exe {C:\backup.ps1}

If I copy the script “backup.ps1” on the local computer and run it from a DOS command prompt with this command : “powershell.exe C:\backup.ps1”. It worked just fine. If run it in a powershell console on the local computer it worked fine as well. How do you run a powershell script on a remote computer? Thanks!

This is what the script looks like:
[string]$btoday = Get-Date –format “MM-dd-yyyy”
[string]$backuppath = New-Item "\\server0009\archivedata\${Env:ComputerName}\$btoday" –Type directory –Force
Move-Item –Path C:\TEST1 –Destination $backuppath

August 22, 2013 at 5:29 am

Your examples will not work unless the backup.ps1 script exists in the C:\ folder on the remote machines. Since your script is very simple I would suggest just passing the code in a script block like so:

$ScriptBlock = {
[string]$btoday = Get-Date –format “MM-dd-yyyy”
[string]$backuppath = New-Item “\\server0009\archivedata\${Env:ComputerName}\$btoday” –Type directory –Force
Move-Item –Path C:\TEST1 –Destination $backuppath

Invoke-Command -ComputerName Computer002 -ScriptBlock $ScriptBlock

August 22, 2013 at 9:16 am

I copied the backup.ps1 script in the C:\ on the remote machine and still doesn't work. I can run it locally on any machine and it will work. Thanks!