0

I'm trying to read a .csv with columns CompanyName,UserCount and edit an .rdp file. The output should create multiple .rdp file, while changing 3 lines in the .rdp itself and the name.

So far I have this:

$testRDP = (Get-Content C:\Users\Administrator.PT\Desktop\Test.rdp)
$remoteAPP = @()
$rdpName = @()
$companyNames=Import-Csv C:\Users\Administrator.PT\Desktop\CompanyNames.csv
foreach ($name in $companyNames) {
    for ($i = 1; $i -le $name.UserCount; $i++) {
        $remoteAPP = "RemoteApp" + $name.CompanyName + ".address.com"
        $rdpName = $name.CompanyName + $i + ".rdp"
        $aliasRDP = $name.CompanyName + $i
        ForEach-Object {$testRDP -replace 'remoteapp.address.com', $remoteAPP}
        ForEach-Object {$testRDP -replace 'user', $aliasRDP} | 
Out-File C:\Users\Administrator.PT\Desktop\$rdpName
        }
}

Running this replaces the 'user' with $aliasRDP but 'remoteapp.address.com' stays the same. I'm guessing that my pipeline is wrong. If I can clarify something better, let me know.

Thank you.

3
  • The first ForEachObject isn't piped anywhere. Where does the output go? Commented May 23, 2015 at 11:54
  • @WalterMitty Moreover, they both doesn't have anything piped in them :) Commented May 23, 2015 at 12:25
  • I think the second foreach-object is piped to the out-file. Commented May 23, 2015 at 13:13

1 Answer 1

3

In fact you don't need ForEach-Object at all:

$testRDP = (Get-Content C:\Users\Administrator.PT\Desktop\Test.rdp)
$remoteAPP = @()
$rdpName = @()
$companyNames=Import-Csv C:\Users\Administrator.PT\Desktop\CompanyNames.csv
foreach ($name in $companyNames) {
    for ($i = 1; $i -le $name.UserCount; $i++) {
        $remoteAPP = "RemoteApp" + $name.CompanyName + ".address.com"
        $rdpName = $name.CompanyName + $i + ".rdp"
        $aliasRDP = $name.CompanyName + $i
        $testRDP -replace 'remoteapp.address.com', $remoteAPP -replace 'user', $aliasRDP | 
Out-File C:\Users\Administrator.PT\Desktop\$rdpName
        }
}

Here is version that uses regex to match and replace strings. It's more safe, because original script will replace matches anywhere in file and that can be fatal.

$testRDP = (Get-Content -Path 'C:\Users\Administrator.PT\Desktop\Test.rdp')
$companyNames = Import-Csv -Path 'C:\Users\Administrator.PT\Desktop\CompanyNames.csv'
foreach ($name in $companyNames) {
    for ($i = 1; $i -le $name.UserCount; $i++) {
            $remoteAPP = 'RemoteApp' + $name.CompanyName + '.address.com'
            $rdpName = $name.CompanyName + $i + '.rdp'
            $aliasRDP = $name.CompanyName + $i
            $testRDP -replace '^(full address:s:).*$', "`$1$remoteAPP" -replace '^(username:s:).*$', "`$1$aliasRDP" |
                Out-File -FilePath "C:\Users\Administrator.PT\Desktop\$rdpName"
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.