# Powershell remote script execution

**URL:** <https://forums.powershell.org/t/powershell-remote-script-execution/23171>\
**Category:** PowerShell Help\
**Created:** [November 17, 2023, 8:07am UTC](https://forums.powershell.org/t/powershell-remote-script-execution/23171 "2023-11-17T08:07:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@Rahul](https://forums.powershell.org/u/Rahul)\
**Post date:** [November 17, 2023, 8:07am UTC](https://forums.powershell.org/t/powershell-remote-script-execution/23171/1 "2023-11-17T08:07:45Z")

</div>

Hello,

(Please ignore the typos)

My requirement is that I need to install and configure one software on multiple servers more than 500 servers. I have powershell script which needs 5 parameters to install it on one machine.

To run this on multiple remote server I am creating one more script to iterate through and using something like below -

Foreach ($server in $serverlist)  
{  
$s= new-pssession $server  
Invoke-command -session $s -scriptblock { param ( $arg1, $arg2, $arg3, $arg4)

Start-job -scriptblock {param ( $arg1, $arg2, $arg3, $arg4)

— do some stuff here

$mycmd = invoke-sqlcmd -query “exec master…xp\_cmdshell 'cd c:\mydir & c:\windows\system32\windowspowershell\v1.0\powershell.exe .\myscript.ps1 `"$arg1`” `"$arg2`" `"$arg3`" “`"$arg4`”'" -Serverinstance $server -Darabase “master”

— do some stuff here

} -ArgumentList $arg1, $arg2, $arg3, $arg3  
} -ArgumentList $arg1, $arg2, $arg3, $arg3  
}

Note:

1. Installation of software can be done by any account but configuration must happen using SQL server service account hence I am using here invoke- sqlcmd

2. Problem is i am not able to run this and it’s not working .

3. Also, How can I track and perform looging of each command and variable?  
Currently I am using below for logging after every important command inside the loop.

Creating log file with timestamp on host server from where I am running script. Then

$file= -join("Microsoft.Powershell.core.filesystem::, $myhost, "", “C$\mydir\abc.log”)

$Message = “Some message”

$Message | Out-File -Filepath $file -Appen

Any help here is much appreciated.

---

<div class="post-metadata">

**Author:** ![adrimus](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/adrimus/32/4925_2.png) [@adrimus](https://forums.powershell.org/u/adrimus)\
**Post date:** [November 17, 2023, 9:31am UTC](https://forums.powershell.org/t/powershell-remote-script-execution/23171/2 "2023-11-17T09:31:27Z")

</div>

For logging you could use start-transcript, that way it will output anything that would appear if you run script with the console. So you could use write-output “Message here” and this would appear in transcript file.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/powershell-remote-script-execution/23171/3 "2024-05-16T20:14:34Z")

</div>


