Powershell remote script execution

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.

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.