Link to home
Start Free TrialLog in
Avatar of Nilay Joshi
Nilay Joshi

asked on

Copy-item powershell and Psexec powershell question

Hello,

I am planning on using psexec command to start .ps1 script that i created and copied on to the remote server using copy-item powershell.  The question i have is the copy-item command works great if i want to copy the item to one server but if i want to copy to multiple servers items that i wrote down in .txt file which command or switch do i run? Here is the command i am using right now.

copy-item -path "local server" where it is stored -destination '\\servername\c$"

Second question,

I am able to copy the .ps1 script i want but when i run the psexec it doesn't run the script.  It does get authenticated to the server and says cmd.exe has started but i don't see anything running on the server.  Any help would be appericated.

psexec.exe \\Destination server name or IP  -u \domain\username -u password -i -d cmd \c '\\ Destination server IP\the path.ps1"

The script says following

The system can not find the file specified.
Conntecting to server ip  stating psexecsvc on server ip connecting with psexec service on server ip and starting cmd on server IP
cmd started on sever ip with process ID 1764.
Avatar of Qlemo
Qlemo
Flag of Germany image

1. You should start PS script with PowerShell, not cmd.exe
2. You should start the local script you just copied that moment on the target, not using UNC.
3. PowerShell script execution has to be allowed on the target.

You can use this code to accomplish all that:
Get-Content 'C:\Scripts\Servers.txt' | % {
  copy-item 'c:\Scripts\Script.ps1' "\\$_\c$"
  psExec \\$_ -u domain\username -u password -i -d powershell -ExecutionPolicy RemoteSigned -File C:\Script.ps1
}

Open in new window

But instead of using psExec you should try to use Remoting via PowerShell directly.
Avatar of Nilay Joshi
Nilay Joshi

ASKER

I just tried your script but it seems to not running anything. I don't see my .ps1 script running. It is in the correct folder. Also, The .txt file has list of servers and i would like the psexec to look at that list of .txt file as well is there any command?  i know @ file does it .  also in copy item can i point the .txt file as well or it has to be \\?
You can't provide a list of locations with copy-item, hence the loop. And since we need a loop, we can also run psExec there instead of using @.

Your text file just contains the serer name on each line? You should see some output in any case, either errors if files or locations are missing / wrong, or psExec output. You can insert following code at line 2:
write-host "Processing server $_"

Open in new window

Thank you for your input but now i have another questions is that when we did following command:

copy-item 'C:\scripts\script.ps1'  "\\servername\c$"  instead of this i would like " List of server names.txt\c$. I am quiet new to this scripting thing not a programmer by any means and this $_ sign i dont know what it does?? should i include it or is that indication for me to put an ip address or name of sever.  

To rephrase my question how do i associated the .txt file that has sever names so that my .ps1 file gets copied to all those server on C$.  

Also, psexec still doesn't work, see the updated script below:

Get-Content 'c:\scripts\IE11deployment.txt' | % {
    Write-Host "processing Server $_"
    copy-item 'C:\deployment\ie11\ie11_install.ps1' "\\serverIP or name \c$ (Here I want the .txt file)
    psexec @file C:\scripts\IE11deployment.txt -u domain\username -p password -i -d powershell.exe -executionpolicy Remotesigned -file C:\IE11_install.ps1
}
"Still doesn't work" is not helpful. Do you see output now?

$_ is "the current object", for example in a loop (and % { } is a foreach-object loop).
Here it should represent the current server name taken from a complete line of the text file. No leading or trailing spaces, no other "columns" in the text file, please.

I have explained it already: We go into a loop, because copy-item cannot copy against multiple targets.
I do see the output from the text file with the server name that i put in the text file.  I just dont see the .ps1 script that i want to run.  I may have to adjust things in psexec.  We can't use invoke command because wmi is blocked and not allowed to use on our environment since it will be change process.
With this (cleaned up) code:
Get-Content 'c:\scripts\IE11deployment.txt' | % {
    Write-Host "processing Server $_"
    copy-item 'C:\deployment\ie11\ie11_install.ps1' "\\$_\c$"
    psexec \\$_ -u domain\username -p password -i -d powershell.exe -executionpolicy Remotesigned -file C:\IE11_install.ps1
}

Open in new window

do you see the file c:\IE11_install.ps1 on the target machine?
Also, remove -the -d from psexec to see more details. This will leave psexec connected until the remote PS script has finished, and you might also see some error message.
Always run the batch file in a PS prompt you opened manually, so you can see the result.
I modified the script it does copy the files that i want to the server but the psexec is failing saying couldn't access the server. I have \\server IP in the text file.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What is the solution here?
it seem to be working thank you so much. I am running into couple other issues but that's on our end.   I really want to learn scripting but I just get bored but soon will get there with focus and wanting to learn