Help with PSexec command
Hi everyone so I have this script that pulls a file from another machine and then unzips it.
function Expand-ZIPFile($file, $destination)
{
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file)
foreach($item in $zip.items())
{
$shell.Namespace($destination).copyhere($item)
}
}
#Create directory
New-Item -ItemType directory -Path "C:\Users\Public\Desktop\Android sdk" -Force
#Move zip to directory
Copy-Item "\\tsvc2552142x011\C`$\Users\cody-horton\Desktop\adt.7z" "C:\Users\Public\Desktop\Android sdk" `
-Recurse -Force
#Unzip file
Expand-ZIPFile File "C:\Users\Public\Desktop\Android sdk\adt.7z" `
Destination "C:\Users\Public\Desktop\Android sdk"
#Delete zip folder
Remove-Item "C:\Users\Public\Desktop\Android sdk\adt.7z" -Recurse
So it works but I'm having trouble using the PSexec command. I can successfully open up the computers command prompt by using psexec "computer name" cmd. Then if I try to run my script it doesn't work.
I'm not sure what I'm doing wrong or if there was a better way to do this. Thanks any help is appreciated.
- Changed type
Codyh123
20 hours 33 minutes ago
wrong type selected
February 12th, 2014 12:30am
Do you get an error message? Do you specify the username? Might be worth adding a username just incase it's impersonation and a double hope issue with network access.
February 12th, 2014 12:44am
Hi thanks for the reply. I was using this command once I was using the other machines command prompt runas/user:UserName"ProgramNamePathToProgramFile"
So I would put my user name which is an admin in AD then the path to the ps1 file I wanted to execute.
February 12th, 2014 12:53am
Some ideas that might or might not help. The script uses an admin share. Perhaps the program does not run elevated even though you are an admin? (I've been having trouble with elevation myself lately.) It is possible to have a script
start another script (or itsself) elevated. I think it will have to open a new command window. That might not be what you want. For example,
start-process powershell -verb runas -argument script.ps1
Perhaps creating a share will remove the need for C$ and elvated access.
When starting the script, try using PowerShell.exe with -ExecutionPolicy unrestricted.
Have you considered enter-pssession? Invoke-command?
February 12th, 2014 2:11am
Alright I've tried psexec and cant get it to work as an admin. So I've been trying the invoke-command. I've enabled remoting on the test machine, however I can't seem to run the script as admin. I'm passing my credentials to it but it still fails and says
access is denied.
Here's the code
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "myusername",$pass
Invoke-Command -ComputerName tsvc2552142x006 -Credential $credentials -FilePath "\\tsvc2552142x006\C`$\Users\cody-horton\Desktop\zip.ps1"
Thanks
February 14th, 2014 10:29pm
February 14th, 2014 10:51pm
Thanks I don't have access to the server that runs the DC so it looks like I'll have to keep trying with psexec. I'll mark this as answered.
February 15th, 2014 12:27am
Thanks I don't have access to the server that runs the DC so it looks like I'll have to keep trying with psexec.
I'd handle this by copying the file to the remote machine before you try to use Invoke-Command. That way you can reference a local path and not run into double hop problems.
Something like:
Copy-Item -Path \\server\fileToCopy.zip -Destination \\remotePC\c$\Temp
Invoke-Command blah blah blah C:\Temp\fileToCopy.zip
I'll mark this as answered.
That's gonna be hard, since you have this thread set as a Discussion instead of a Question. =]
February 15th, 2014 12:43am
Okay I'll look at that Monday thanks for all the help. Also sorry I noticed it wouldn't let me select an answer I'll try to edit that and if your answer works I'll mark it as the correct answer. Thanks again.
Edit: On a side note psexec would have worked great but when ever I use it to open up another machines command propmpt, then from there I open powershell inside that
psexec computername cmd
#next
powershell
#finally opening up the file doesn't work
c:\users\path to file\zip.ps1
it just doesn't work I'm not sure if this is a bug with the psexec command. It doesn't give me any error messages it just freezes.
Thanks
- Edited by
Codyh123
20 hours 33 minutes ago
February 15th, 2014 9:58am
Hi Codyh123,
If your script hangs when run powershell script with PSexec, Please try to pass -i agrument to PsExec to run the command in an interactive mode, for example:
PSExec -i \\RPC001 -u myID -p myPWD PowerShell C:\script\StartPS.ps1
This issue also can be found in this article:
Running PowerShell using PsExec
I hope this helps.
February 16th, 2014 5:15am