running a script with paramters with credential of domain account

Welcome Forums General PowerShell Q&A running a script with paramters with credential of domain account

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
3 years, 6 months ago.

  • Author
    Posts
  • #25987

    Participant
    Points: 0
    Rank: Member

    I have a script called CreateClass.ps1 and has 3 parameters -Name , -Grade and -Count . It's automated script to run, so it runs as Administrator( local account), How can that script be run under the credential of the domain account for example SchDomain\Student with password Password123?

    Thanks.

  • #26040

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    You'd either need to use Start-Process to start a new PowerShell.exe process using the alternate credential, or schedule the script using Task Scheduler to run under the alternate credential. Or, use RunAs.exe to launch the script using an alternate credential. But there's no way to do it from within the same PowerShell session.

  • #26090

    Participant
    Points: 0
    Rank: Member

    Thanks for the ideas and options, do you have any sample how to do it for the start-process with arguments and domain credential. I tried this but I have the problem with the parameters to include them. Thanks again.

  • #26139

    Participant
    Points: 26
    Rank: Member

    It would be useful if you can show us the code of your script. Then we can advise you better.

  • #26351

    Participant
    Points: 0
    Rank: Member

    Here what I have , I have the value for both parameters or arguments in the file , not sure where to pass it ,:

    invoke-command -ComputerName Myserver -filepath c:\scripts\oucreate2.ps1 -argumentlist ClusterName, OUDetail -Credential (New-Object System.Management.Automation.PSCredential('test\Myuser',(ConvertTo-SecureString Password123HJ67 -AsPlainText -Force)))

    the value of ClusterName is ClstWINCLS and the OUDetail is 'OU=TestServers,DC=test,DC=com'

    I put an equal sign after the argument list it's erroring out, not sure how to embedded in scriptblock.
    Any help is really appreciated.

  • #26358

    Participant
    Points: 60
    Rank: Member

    Have you tried the $using:variable alternative for these variables?

    This allows you to access variables outside of the scriptblock without the need to pass them in as arguments in the invoke-command.

    It's more cosmetic, but I'd also define the credential object before using it as a -Credential parameter value. Not a big fan of calculations in parameter blocks.

    e.g.

    $secpasswd = ConvertTo-SecureString "MyPasswd" -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    Then use -Credential $Credential in the Invoke-Command

  • #26364

    Participant
    Points: 26
    Rank: Member

    Ok, we only see part of the code. We would like to help you, but can you post the complete code of oucreate2.ps1 here? We can then maybe write code which achieves what you want.

The topic ‘running a script with paramters with credential of domain account’ is closed to new replies.