Clicky

Hey

For example.:

Write-host "Hello Word"
Add-XXXX "Hello"

How to i run the Add-XXXX as another user? (I would like to run only the line... not the hole PS)

Mike

asked 12/12/2011 08:01

mikeydk's gravatar image

mikeydk ♦♦


1 Answers:
The Invoke-Command cmdlet allows you to do single line executions specifying the user account of your choosing:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
PS C:> Get-Help Invoke-Command -Parameter Credential

-Credential <PSCredential>
    Specifies a user account that has permission to perform this action. The default is the current user.

    Type a user name, such as "User01" or "Domain01User01", or enter a variable that contains a PSCredential object, s
    uch as one generated by the Get-Credential cmdlet. When you type a user name, you will be prompted for a password.

    Required?                    false
    Position?                    named
    Default value
    Accept pipeline input?       true (ByPropertyName)
    Accept wildcard characters?  false

So, in your case, you could have the bulk of your script under one credential, then, when needed, call Invoke-Command -Scriptblock {} -Credential and pass the credential you need.  Also, search for how to start credential passwords in physical files.  This can full automate whatever process you're using.  Just realize that the AsSecureString feature is not very secure and any one with a modicum of Powershell experience can crack this with one command.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
PS C:> Get-Help Invoke-Command -Parameter Scriptblock

-ScriptBlock <scriptblock>
    Specifies the commands to run. Enclose the commands in curly braces ( { } ) to create a script block. This paramete
    r is required.

    By default, any variables in the command are evaluated on the remote computer. To include local variables in the co
    mmand, use the ArgumentList parameter.

    Required?                    true
    Position?                    1
    Default value
    Accept pipeline input?       false
    Accept wildcard characters?  false
link

answered

wls3's gravatar image

wls3

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×25

Asked: 12/12/2011 08:01

Seen: 180 times

Last updated: 12/13/2011 04:17