Merging Remove-Computer, New-Object, and ConverTo-SecureString

Hey all, this is my first post here, so thank you in advance for any assistance. I’m trying to merge a few commands into one but am having trouble. Is this even possible to merge all these into one command? I’ve tried braces, but it errored out. Here’s the working commands:

$securePass = ConvertTo-SecureString "$DomainPassword" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("$DNS\$DomainAdmin", $securePass)
Remove-Computer -Credential $cred -Force -Passthru -Verbose -Restart

I tried this way, too, but this errored out:

Remove-Computer -Credential {New-Object System.Management.Automation.PSCredential ("$DNS\$DomainAdmin", {ConvertTo-SecureString "$DomainPassword" -AsPlainText -Force})} -Force -Passthru -Verbose -Restart

This was the error I got:

Remove-Computer : Cannot process argument transformation on parameter 'UnjoinDomainCredential'. userName
At line:1 char:29
+ ... -Credential {New-Object System.Management.Automation.PSCredential ("$ ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Computer], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Microsoft.PowerShell.Commands.RemoveComputerCommand

Any suggestions would be great. Thanks so much!
~ David

David,
Welcome to the forum. :wave:t4:

Why?

If you get errors you should share them here completely. Errors are an important language feature as they tell you what’s wrong and sometimes even what you should change.

And BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Instead of curly braces you should use parenthesis! :point_up_2:t4: :wink:

Thanks Olaf! That was the trick, the parenthesis worked!! Thanks so much!

~ David

OK. I’m glad it helped.

May I ask again why you want to have this in one command anyway? It makes it harder to read and in case harder to maintain wihtout improving the actual function or speed.

I’m trying to make this work in with an RMM script and it’s a pain in the butt to try to get it all done with multiple lines because the RMM program kills the value of the variables from line to line, so I have to find a way to make it all work in one line. I’m working on trying to get about 80-ish machines seamlessly removed from a domain, their profiles converted (using ForensIT User Migration Wizard), and the joined to Azure AD, all through this RMM. It’s been a bit of a challenge, and this one-liner was the last struggle.

May I ask the name of that RMM? I’d recomend to (re-)evaluate the RMM if it does not properly supports PowerShell. Can’t you use scripts instead?

Yup. We’re using Kaseya, but we’re currently waiting for a call back from ConnectWise because we’re looking to look into another solution. We’ve had a lot of other issues with Kaseya scripting in general.

Ah. I see. OK, thanks. So I know what to avoid if someone asks me what to recommend. :wink: :smiling_face:

Exactly!!! Thanks again! :smiley: