Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, July 13, 2017 1:37 PM
Hi,
I need assistance with a powershell script that will get the local computername, and then add that value to a specific section in a specific XML file.
My powershell knowledge is unfortunately very limited.
Any assistance would be greatly appreciated.
All replies (7)
Thursday, July 13, 2017 3:18 PM ✅Answered | 1 vote
Should be $env:COMPUTERNAME there is not HOSTNAME environment variable
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Thursday, July 13, 2017 1:42 PM | 1 vote
The first thing to do is learn PowerShell. The second thing is to learn XML
$xml.section.section.hostname = $env:COMPUTERNAME
Of course you will have to learn enough to understand how to use that. There is really no other way to answer such a vague question.
\(ツ)_/
Thursday, July 13, 2017 3:07 PM
I actually just want to search for a word in a file and replace it with the hostname. I got this far:
#using simple text replacement
$con = Get-Content .\test.xml
$con | % { $_.Replace("computername", "$env:hostname") } | Set-Content .\test.xml
I am obv just missing something at the $env:hostname part. If i add any static word in there it works.
Thursday, July 13, 2017 3:20 PM
Should be $env:COMPUTERNAME there is not HOSTNAME environment variable
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Thank you! Works perfect - I think jvr should learn powershell....
Thursday, July 13, 2017 3:24 PM
Should be $env:COMPUTERNAME there is not HOSTNAME environment variable
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Thank you! Works perfect - I think jvr should learn powershell....
Sorry for the typo.
\(ツ)_/
Thursday, July 13, 2017 3:40 PM
Should be $env:COMPUTERNAME there is not HOSTNAME environment variable
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Thank you! Works perfect - I think jvr should learn powershell....
jrv knows what he is talking about, typos and mishaps do happen here and there
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Thursday, July 13, 2017 3:42 PM
Especially when I am the one doing the typing.
\(ツ)_/