Scripting Powershell

I have the following line of code in a larger script which works beautifully.

intReturn = objShell.Run("cmd /k powershell Rename-Computer -ComputerName " & txt_oldname.Value & " -NewName " & sNewName & " -DomainCredential MyDomain\" & txt_admname.Value & " -Force -PassThru")

Open in new window


There is one slight tweak I would like to acomplish but not sure how/possible? I have a variable that stores the password as well " & txt_admpass.Value & " What would the code look like if I worked this in so that it would not ask for the password again? Thanks!
LVL 1
jbyrd1981Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
If I am not wrong -DomainCredential parameter PSCredential object. Refer the following article to get details on creating PSCredential object..

http://blogs.msdn.com/b/koteshb/archive/2010/02/13/powershell-creating-a-pscredential-object.aspx
0
jbyrd1981Author Commented:
Trying this...

intReturn = objShell.Run("cmd /k powershell $mycreds=New-Object System.Management.Automation.PSCredential (" & txt_admname.Value & "," & txt_admpass.Value & ") Rename-Computer -ComputerName " & txt_oldname.Value & " -NewName " & sNewName & " -PSCredential -Force -PassThru -Restart")

Open in new window


Get this...

At line:1 char:75
+ $mycreds=New-Object System.Management.Automation.PSCredential
(domain\user,password ...
+                                                                           ~
Missing argument in parameter list.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx
   ception
    + FullyQualifiedErrorId : MissingArgument
0
J-LCommented:
I think the only thing missing is a semicolon between the two commands, right before Rename-Computer, no?

intReturn = objShell.Run("cmd /k powershell $mycreds=New-Object System.Management.Automation.PSCredential (" & txt_admname.Value & "," & txt_admpass.Value & "); Rename-Computer -ComputerName " & txt_oldname.Value & " -NewName " & sNewName & " -PSCredential -Force -PassThru -Restart")

Open in new window

0
Hey MSSPs! What's your total cost of ownership?

WEBINAR: Managed security service providers often deploy & manage products from a variety of solution vendors. But is this really the best approach when it comes to saving time AND money? Join us on Aug. 15th to learn how you can improve your total cost of ownership today!

jbyrd1981Author Commented:
Same error as above with the semicolon. :(
0
SubsunCommented:
There is no parameter called -PSCredential for Rename-Computer, it should be -DomainCredential and the PSCredential object created should be assigned.. So the PowerShell command should look something like this..

$secpasswd = ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)
Rename-Computer -ComputerName Oldname -NewName NewName -DomainCredential $mycreds -Force -PassThru -Restart

Open in new window

0
jbyrd1981Author Commented:
Ok... I think I have it figured out with the assistance of these posts... Just not sure how to fix it. Here is the section. The problem is the underlined part. I basically pass this information from a text box. When I use the value hardcoded I am good. Would seem if it would allow another set of quoes around the underlined section I would be good but it throws an error.  

$mycreds = New-Object System.Management.Automation.PSCredential (Domain\" & txt_admname.Value & ", $secpasswd)

Also, I think I will have a problem with this section.

$secpasswd = ConvertTo-SecureString " & txt_admpass.Value & " -AsPlainText -Force

Open in new window


This too is passed from a text box. I think it is going to have problems if the password contains characters like )(... in the password. Any way around that?
0
J-LCommented:
$mycreds = New-Object System.Management.Automation.PSCredential (Domain\" & txt_admname.Value & ", $secpasswd)

Open in new window

is not syntactically correct.
If you check http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences, you'll see all there is to know to using quotes (single or double) and the escape character (backtick `) in Powershell.

$mycreds = New-Object System.Management.Automation.PSCredential ("$Domain\$($txt_admname.Value)", $secpasswd)

Open in new window

should do the trick, if $Domain is a string variable and $txt_admname is an object you created earlier.
0
jbyrd1981Author Commented:
I am just not having any luck. I can use this in a powershell window with success executing each one one by one. Yet when I place in my script it looks pretty much like below except I sub the password and username from what is gathered from the text box input from the script. That way still fails of course.

$secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force

Open in new window

$mycreds = New-Object System.Management.Automation.PSCredential ("domain\myacct", $secpasswd)

Open in new window

Rename-Computer -ComputerName xyz -NewName zyx -DomainCredential $mycreds -Force -PassThru -Restart

Open in new window

HasSucceeded OldComputerName           NewComputerName
------------ ---------------           ---------------
True         xyz                    zyx
0
jbyrd1981Author Commented:
At line:1 char:40
+ $secpasswd = ConvertTo-SecureString Pa)(*ssword -AsPlainText -Force;
$mycreds ...
+                                        ~
Unexpected token ')' in expression or statement.
At line:1 char:71
+ $secpasswd = ConvertTo-SecureString Pa)(*ssword -AsPlainText -Force;
$mycreds ...
+                                                                       ~
Missing closing ')' in expression.
At line:1 char:149
+ ... al (domain\name, $secpasswd); Rename-Computer -ComputerName old -NewName new ...
+                    ~
Missing argument in parameter list.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx
   ception
    + FullyQualifiedErrorId : UnexpectedToken
0
J-LCommented:
Jbyrd, can you please post the full code you're trying to run?
0
jbyrd1981Author Commented:
I can substitute the variables for actual credentials and computer names and it still does not work. The code below is what got the error directly above.

intReturn = objShell.Run("powershell -Noexit $secpasswd = ConvertTo-SecureString " & txt_admpass.Value & " -AsPlainText -Force; $mycreds = New-Object System.Management.Automation.PSCredential (Domain\" & txt_admname.Value & ", $secpasswd); Rename-Computer -ComputerName " & txt_oldname.Value & " -NewName " & txt_newname.Value & " -DomainCredential $mycreds -Force -PassThru -Restart")

Open in new window

0
jbyrd1981Author Commented:
I got it!!! Had to put single quotes in a couple of places. Boy! This was rough! Maybe I am on my way to be a powershell Guru.

intReturn = objShell.Run("powershell -Noexit $secpasswd = ConvertTo-SecureString '" & txt_admpass.Value & "' -AsPlainText -Force; $mycreds = New-Object System.Management.Automation.PSCredential ('Domain\" & txt_admname.Value & "', $secpasswd); Rename-Computer -ComputerName " & txt_oldname.Value & " -NewName " & txt_newname.Value & " -DomainCredential $mycreds -Force -PassThru -Restart")

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jbyrd1981Author Commented:
It worked!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.