1

I am trying to edit a file on multiple remote machines. I have a command that works locally, but when I try to invoke the command on a remote PC using administrator credentials I am getting an error.

This works locally:

(get-content -Path 'C:\Program Files (x86)\Wycom\WyReceipts\WyReceiptsOverrides.ini') -replace ";End of WyReceipts Settings File",";Local Receipt Archive Directory
LocalPDFArchiveDirectory = C:\Program Files (x86)\WyCom\WyReceipts\LocalReceiptArchive

;End of WyReceipts Settings File" | Out-File -FilePath 'C:\Program Files (x86)\Wycom\WyReceipts\WyReceiptsOverrides.ini'

But when I try this i get errors:

Import-Module ActiveDirectory
$Credentials = Get-Credential
$Credentials.Password | ConvertFrom-SecureString | Set-Content C:\temp\password.txt
$Username = $Credentials.Username
$Password = Get-Content “C:\temp\password.txt” | ConvertTo-SecureString
$Credentials = New-Object System.Management.Automation.PSCredential $Username,$Password
Get-ADComputer -filter 'Name -like "ess-*-Tel*" -or Name -like "ess-*tel*" -and Enabled -eq "True"' | select "Name" | Out-File -FilePath "c:\Temp\WycomPCs.txt"
$computers = get-content -Path "c:\Temp\WycomPCs.txt"

ForEach ($computer in $computers){
Invoke-Command -Credential $Credentials -ScriptBlock {
(get-content -Path 'C:\Program Files (x86)\Wycom\WyReceipts\WyReceiptsOverrides.ini') -replace ";End of WyReceipts Settings File",";Local Receipt Archive Directory
LocalPDFArchiveDirectory = C:\Program Files (x86)\WyCom\WyReceipts\LocalReceiptArchive

;End of WyReceipts Settings File" | Out-File -FilePath 'C:\Program Files (x86)\Wycom\WyReceipts\WyReceiptsOverrides.ini'
}}

Remove-Item -Path "C:\temp\password.txt"

Here is the error message I am receiving:

Invoke-Command : Parameter set cannot be resolved using the specified named parameters.
At c:\mystuff\Scripts\WycomFix.ps1:11 char:1
+ Invoke-Command -Credential $Credentials -ScriptBlock {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand

Is there a better way to go about this? Thanks in advance! -Bart (powershell novice, but getting better!)

  • Quick note: I thought maybe the out-file having the "name" header and trailing spaces may be causing issues, so i trimmed up the file, and commented out the Get-ADComputer line, but I still got the same error. – BartHurley0283 May 22 '23 at 20:08

1 Answers1

1
  • You're missing a -ComputerName $computer argument in your Invoke-Command calls.

    • Omitting -ComputerName would make the calls local ones, but using -Credential is then not supported - and resulted in the error you saw; in other words: if you use -Credential, you must also use -ComputerName.[1]
  • -ComputerName accepts an array of computers to target in parallel, which is much faster than targeting them in sequence, as in your foreach loop-based attempt.

Therefore, replace your foreach statement with this:

Invoke-Command -ComputerName $computers -Credential $Credentials -ScriptBlock {

  (Get-Content -Path 'C:\Program Files (x86)\Wycom\WyReceipts\WyReceiptsOverrides.ini') -replace ";End of WyReceipts Settings File",";Local Receipt Archive Directory
LocalPDFArchiveDirectory = C:\Program Files (x86)\WyCom\WyReceipts\LocalReceiptArchive

;End of WyReceipts Settings File" | Out-File -FilePath 'C:\Program Files (x86)\Wycom\WyReceipts\WyReceiptsOverrides.ini'

}

[1] Or one of the other parameters that target other computers, either remote ones or VMs. These requirements are enforced via parameter sets.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1. Would I need to define $computers differently to be an array? 2. Would the above example eliminate the ForEach loop entirely? Thanks! – BartHurley0283 May 22 '23 at 20:32
  • @BartHurley0283: Re 1. No, you can use `$computers` as-is, because when you capture `Get-Content` output, you automatically get an _array of lines_. Re 2. Yes, the above replaces your `foreach` loop (I've edited the answer to make that clear.) – mklement0 May 22 '23 at 20:37
  • That gave me a different error this time: Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. + Invoke-Command -ComputerName $computers -Credential $Credentials -Scr ... + ~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand – BartHurley0283 May 22 '23 at 20:40
  • @BartHurley0283 that implies that your `$computers` variable is either `$null`, the empty string or - if it is an array of values - _contains_ such entries. To rule out the latter case, try `-ComputerName (@($computers) -notlike '')` – mklement0 May 22 '23 at 20:44
  • That worked! Thank you so much. After reading the last comment I realized that when i trimmed up the file i didn't check for blank lines at the end of the list. Turns out there were a few there so the null error makes total sense! Thanks again as this was starting to baffle me. I'm fairly new to powershell scripting, but the more I do it the more uses I'm finding for it. I find myself approaching every problem now with the how can I fix this with powershell mindset. – BartHurley0283 May 22 '23 at 20:56
  • One other quick question if you have a sec. I initially tried to eliminate the text list altogether by defining `$computers = Get-ADComputer -filter 'Name -like "ess-*-Tel*" -or Name -like "ess-*tel*" -and Enabled -eq "True"' | select "Name"` but this didn't work so I resorted to the txt file output and get-content. Is there a better way to eliminate the text file source list? – BartHurley0283 May 22 '23 at 21:00
  • Glad to hear it, @BartHurley0283; my pleasure. PowerShell deserves the "power" part of its name, and time spent learning is well worth it, the occasional warts notwithstanding. As for your follow-up question: use `select -ExpandProperty Name` or `(...).Name` - see [this post](https://stackoverflow.com/q/48807857/45375) for details. – mklement0 May 22 '23 at 21:02
  • Sent you a coffee for your help. You rock! – BartHurley0283 May 22 '23 at 21:14