Copy-item powershell and Psexec powershell question


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.
Nilay JoshiSystems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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.
Nilay JoshiSystems AdministratorAuthor Commented:
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 \\?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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

IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Nilay JoshiSystems AdministratorAuthor Commented:
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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
"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.
Nilay JoshiSystems AdministratorAuthor Commented:
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.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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.
Nilay JoshiSystems AdministratorAuthor Commented:
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.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
To make sure there is no issue with psexec, I've tested this:
"" | % { psexec \\$_ cmd.exe }

Open in new window

and it works just fine. It opens a remote command prompt on the local machine, but would do the same for any other IP. Please try that line with one of the failing IPs.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
What is the solution here?
Nilay JoshiSystems AdministratorAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2008

From novice to tech pro — start learning today.