Help Remote Scripting problems

I created a powershell (DBOff.ps1) script that shuts down my server database before it shutdowns my server (Windows 2008 R2). This script is held locally on the server itself and is fully functional. The problem is I need to create a batch file and a second Powershell script to run said original script using another computer. These two scripts will be called from the office Windows 7 machine in case of a power failure. All remoting configurations have been made and If I manually enter the commands in the PowerShell console they work just fine.
Here is the Batch File:
powershell.exe -file C:\Users\baftp\Documents\APCPowershellWork\InvokePowershell.ps1


Here is my Local Windows 7 InvokePowershell.ps1 script


Create New Remote Session


#$MicrosLab= Enter-pssession -computername -credential baftp

start-sleep -s 3


Find and Run the Powershell Script on remote PC


Invoke-Command -computername $CN -credential $USRNM {Set-Location

Even though I have successfully connected to the remote PC this is the error i get
Cannot find path ‘C:\Users\baftp\Documents\PSScripts\microsoff.ps1’ because it
does not exist.
+ CategoryInfo : ObjectNotFound: (C:\Users\baftp…s\microsoff.p
s1:String) [Set-Location], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLo

I used the invoke- command thinking this would help the problem since Enter-PSSession wasnt working. What am I doing wrong?

Also is there a way to save username and password in the -credential cmdlet. Im creating this batch file for a user that will not know the password.
I am Using Powershell 2.0 on both machines
Thanks in advance

As a note, the forums support the HTML “pre” tag for code formatting.

Enter-PSSession isn’t really meant to be used in the way you’re using it. It’s meant for interactive use. You’re not meant to “enter a session and then run commands” from within a script; the script contents will always be run in the local instance. You’re using Invoke-Command, but by using -ComputerName you’re not utilizing an existing session - you’re doing an ad-hoc session, which means the Set-Location command isn’t effective. Also, Set-Location doesn’t run a script. It changes directories. So you’re mixing and matching a lot of techniques.

Read “Secrets of PowerShell Remoting” and see if that doesn’t help a bit. In the meantime…

If I’m on PC-A, and I want to run script REMOTE.PS1 on machine SERVER-B, then I would write script LOCAL.PS1 on PC-A. LOCAL.PS1 might look as follows:

$session = New-PSSession -ComputerName SERVER-B
Invoke-Command -Session $session -ScriptBlock { C:\path\to\script\remote.ps1 }

A problem with this is that the session will only remain open so long as the PowerShell window is open on PC-A. With PowerShell 2.0, you can’t disconnect the session and allow it continue running without PC-A’s involvement. Once the PowerShell window closes on PC-A, the entire script will terminate. Given the versions of everything that you’re using, I’m not sure if there’s a good way around that. Just understand that “kicking something off by using Invoke-Command” does not mean the something will continue running if the session gets disconnected.

As for saving the username, check out the Articles on this site. Dave Wyatt wrote a very comprehensive article about saving credentials that you should read.