Share via

New Lines with PowerShell

joaomanoelc 171 Reputation points
2020-10-30T18:13:07.4+00:00

I have this script that adds a line at the end of the file, but it is incomplete.

that part of the $1 line has not been added

$myServers = "server1","server2","server3","server4"
Invoke-Command -ComputerName $myServers -ScriptBlock {
Add-Content "C:\Program Files\Zabbix Agent\zabbix_agentd.conf" "`nUserParameter=windowscsvfs.discovery[*],powershell -NoProfile -ExecutionPolicy Bypass -File C:\Program Files\Zabbix Agent\win_clustersv.ps1 $1" }

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Rich Matheisen 48,116 Reputation points
2020-10-30T18:43:02.12+00:00

You're correct, it hasn't been added because it doesn't exist. It's being interpreted as a variable name and even if you define it outside the scriptblock the variable won't exist on the machine that's executing the scriptblock. You'll have to pass the variable as a parameter into the scriptblock.

On the other hand, if your intention was to have a literal "$1" written to the file, then escape the "$" character in the scriptblock.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.