Ok here is the thing. I tried to make a PowerShell script to create some folders and to change a couple of strings in a .txt file.

But as you can guess it is not working. See my configuration below:

[string]$CustomerName = Read-host "Enter the company name of the customer (e.g. Companyname)"
write-host ""
[string]$Hostname = Read-host "Enter the FQDN of the customers machine (e.g. HV00.domain.local)"
write-host ""
[string]$Logon = Read-host "Enter te logon credentials of the customers machine (e.g. domain\user)"
write-host ""
[string]$Password = Read-host "Enter the password of the customers machine"
write-host ""

cd C:\Monitor\Logs
mkdir .\$CustomerName
cd C:\Monitor\Logs\$CustomerName
mkdir .\$Hostname
cd $Home

cd C:\Monitor\Scripts\CustomScript
mkdir .\$CustomerName
cd $Home

$InputFile = "C:\Monitor\Scripts\BaseScript\BaseScript.txt"
$OutputFile = "C:\Monitor\Scripts\CustomScript\$CustomerName\$CustomerName.txt"

This is not working

(Get-Content $InputFile) | ForEach-Object { $_ 
-replace "hostname", "$Hostname" `
-replace "username@domainname", "$Logon" `
-replace "password", "$Password" } `
-replace "C:\Monitor\Logs\customername\hostname\", "C:\Monitor\Logs\$Customername\$Hostname\" 
} | Set-Content $OutputFile

This is working, but I can only replace one string with it

(Get-Content $InputFile) -replace "hostname", "$Hostname" > $OutputFile

Does anybody have any idea?

  • Break down what isn't working into their own parts. Please indicate what part of "what isn't working" works and what doesn't work. Basic debugging 101. – Ramhound Sep 10 '14 at 13:55

It would be handy if you would paste the error you're getting as well. But you have one closing curly bracket too much in your for-loop's code, which is probably it:

-replace "password", "$Password" } `  <=== this one

btw, you can shorten the amount of subfolder creation code by creating the whole tree at once, which PowerShell can handle perfectly

mkdir c:\foo\bar\this\will\all\be\created\in\one\statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.