I’m attempting to search and replace the word HOSTNAME and replace it with {hostname as uppercase} in a template file downloaded to each endpoint using a powershell script. In the example below, this fails on the line containing {hostname as uppercase}. Looking for suggestions on other ways to accomplish this or do I just have syntax wrong?
createfile until __EOF
Get-Content “D:\Apps\Cybermation\ESP System Agent R7\agentparm.new”) |
Foreach-Object {$_ -replace “HOSTNAME”, “{hostname as uppercase}”} |
Set-Content “D:\Apps\Cybermation\ESP System Agent R7\agentparm.txt”
__EOF
move __createfile “D:\Apps\Cybermation\ESP System Agent R7\updatehost.ps1”
rundetached “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe &D:\Apps\Cybermation\ESP System Agent R7\updatehost.ps1”
That’s correct! If you don’t want the curly braces treated as a special character (e.g. Relevance substitution) you will ha to escape it. You can do that by doubling up on only the first one. For instance:
Forgot about that part… I just went through this a a couple weeks ago. It is an issue with 64/32 bit systems. You have to set powershell to unrestricted in both. And this is where you run into problems on 64bit machines. Here is a little more info for your reading pleasure:
This sounds exactly like the issue I had. I am no powershell expert but I muddled my way through once I realized there were 2 different powershells and that the ExecutionPolicy had to be set to unrestricted.
More details:
Try running the following commands at the command line:
I originally only had the 32 bit version set to unrestricted. That was why I added the piece that was capturing the “c:\test.txt” so I could see what the output from running the ps1 was.
Sorry for fumbling through this… It is all coming back slowly.
Of course the first part of the script as I have it above won’t work. You can’t script powershell until you change it from the default “restricted”. And I was trying to script it to change it… The Execution policy can simply be changed in the registry. I just tested this one against a x64 server. See if this helps:
//Set to Unrestricted
//32 bit
if {name of operating system as lowercase contains “win” and architecture of operating system = “x86”}
Wowzers. Any reason it can’t or shouldn’t be done with PowerShell’s -ExecutionPolicy parameter? That seems like an easier way to change the policy temporarily…